【问题标题】:Div with fixed with and div with flexible width next to each other具有固定宽度的 div 和具有灵活宽度的 div 彼此相邻
【发布时间】:2016-02-02 11:13:17
【问题描述】:

在一个左右填充 2% 的容器中,我有两个 div 框。左边的 div 框有 200px 的固定宽度和 60px 的固定边距。我希望正确的 div 调整其宽度,浏览器窗口越小/越大。如何实现红色框的宽度始终(与浏览器宽度无关)填充到容器的右填充开始,而蓝色 div 保持其 200 像素?

JSFIDDLE:http://jsfiddle.net/3vhrst19/3/

HTML:

<div id="container">
    <div id="fixed-width"></div>

    <div id="flexible-width"></div>
</div>

CSS:

#container {
    float: left;
    width: 100%;
    padding: 50px;
    background: lightgrey;
}

#fixed-width {
    float: left;
    width: 200px;
    height: 500px;
    margin-right: 60px;
    background: blue;
}

#flexible-width {
    float: left;
    width: 500px; /* my goal is that the width always fills up independent of browser width */
    height: 500px;
    background: red;
}

【问题讨论】:

    标签: html css responsive-design


    【解决方案1】:

    使用calc去除100%宽度的固定宽度和边距宽度

    #container {
      float: left;
      width: 100%;
      padding: 50px;
      background: lightgrey;
    }
    #fixed-width {
      float: left;
      width: 200px;
      height: 500px;
      margin-right: 60px;
      background: blue;
    }
    #flexible-width {
      float: left;
      max-width: 500px;
      /* my goal is that the width always fills up independent of browser width */
      width: calc(100% - 260px); /* Use calc to remove the fixed width and margin width from the 100% width */
      height: 500px;
      background: red;
    }
    <div id="container">
      <div id="fixed-width"></div>
    
      <div id="flexible-width"></div>
    </div>

    【讨论】:

      【解决方案2】:

      这很容易通过flexbox实现:

      #container {
        display: flex;
        width: 100%;
        padding: 50px;
        background: lightgrey;
        box-sizing: border-box; /* used so the padding will be inline and not extend the 100% width */
      }
      

      响应式元素用flex-grow 填充剩余空间:

      #flexible-width {
        flex: 1; /* my goal is that the width always fills up independent of browser width */
        height: 500px;
        background: red;
      }
      

      请注意,我删除了您的所有 floats,因为在此示例中没有必要。

      JSFiddle

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-25
        • 2011-10-13
        • 1970-01-01
        • 1970-01-01
        • 2014-11-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多