【问题标题】:3 div's middle div take up remaining width3 div 的中间 div 占用剩余宽度
【发布时间】:2013-03-09 11:45:56
【问题描述】:

好吧,伙计们,我一直在为这个而烦恼。

我有三个 div;左,中,右。全部 100% 高度。左右 div 的宽度固定为 150px。现在我想让中间的 div 占据剩余的空间。

示例:Here

CSS:

#left {
    height:100%;
    width:150px;
    float:left;
    background:red;
    z-index:999;
}
#middle {
    height:100%;
    width:100%;
    background:yellow;
    margin-left:-150px;
    margin-right:-150px;
}
#right {
    float:right;
    height:100%;
    width:150px;
    background:red;
    z-index:998;
}

【问题讨论】:

  • 只用谷歌搜索 css 布局,你会在其中一个页面上发现非常相似的东西,如果不一样的话,这是很常见的问题。

标签: html css


【解决方案1】:

使用display: table:

#container {
    display: table;
    width: 100%;
}

#left, #middle, #right {
    display: table-cell;
}

#left, #right {
    width: 150px;
}

#container 是您的父元素,如 in

<div id="container">
    <div id="left"></div>
    <div id="middle"></div>
    <div id="right"></div>
</div>

这是Fiddle。

【讨论】:

  • 我如何做到这一点,但垂直?
  • @RodrigoRuiz:其实我真的不明白你的意思,因为在大多数情况下这会自动发生。无论如何:你应该发布一个新问题。
【解决方案2】:

检查这个similar answer

HTML

<div class = "container">
    <div class = "fixed">
        I'm 150px wide! Glee is awesome!
    </div>
    <div class = "fluid">
        I'm fluid! Glee is awesome!
    </div>
    <div class = "fixed">        
        I'm 150px wide! Glee is awesome!
    </div>
</div>

CSS

html, body {
    height: 100%;
    font-family: 'Arial', 'Helvetica', Sans-Serif;
}
.container {
    display: table;
    width: 100%;
    height: 100%;
}
.container > div {
    display: table-cell;
    text-align: center;
}
.fixed {
    width: 150px;
    background: rgb(34, 177, 77);
    color: white;
}
.fluid {
    background: rgb(0, 162, 232);
}

DEMO

【讨论】:

    【解决方案3】:

    好的,使用flex 和基于this answer

    #parent {
      display: flex;
    }
    #left {
      width:150px;
      background-color:#ff0000;
    }
    #middle {
      flex: 1 1 auto;
      background-color:#00ff00;
    }
    #right {
      width: 150px;
      background-color:#0000ff;
    }
    <div id="parent">
      <div id="left">left</div>
      <div id="middle">middle</div>
      <div id="right">right</div>
    </div>

    如果左右 div 的宽度也可变,这实际上是有效的。

    【讨论】:

      【解决方案4】:

      如果你不想使用表格单元格并且不想给你的外框一个固定的宽度(而是让它们的宽度由它们的内容决定,你可以使用溢出隐藏和浮动方法)

      这种方法的坏处是你必须坚持在你的 div 上隐藏溢出,而且你必须以向后的方式排列你的 div(见下文)

      DEMO

      HTML

      <div class='container'>
      
          <div class='third-box'>Third</div>
      
          <div class='first-second'>
              <div class='first-box'>First</div>
              <div class='second-box'>Second</div>
          </div>
      
      </div>
      

      CSS

      .container {
           width: 400px;
      }
      
      .first-second {
          overflow: hidden;
      }
      
      .first-box {
          float: left;
          background-color: coral;
      }
      
      .second-box {
          overflow: hidden;
          background-color: aquamarine;
      }
      
      .third-box {
          float: right;
          background-color: salmon;
      }
      

      【讨论】:

        【解决方案5】:

        我用它来为侧面的项目设置一个灵活的宽度。在列出的项目中使用它可以获得一个侧面有 2 个按钮的进度条。

        ul {
            display: table;
            width: 100%;
        }
        
        ul > li {
            display: table-cell;
        }
        
        ul > li:nth-child(2) {
            min-width: 100%;
        }
        

        【讨论】:

          猜你喜欢
          • 2013-05-07
          • 2019-03-18
          • 2016-09-14
          • 2011-12-20
          • 1970-01-01
          • 2018-10-23
          • 2013-07-31
          • 2012-08-17
          • 1970-01-01
          相关资源
          最近更新 更多