【问题标题】:2 column div layout: right column with fixed width, left fluid2列div布局:固定宽度的右列,左流体
【发布时间】:2011-07-08 22:12:51
【问题描述】:

我的要求很简单:2 列,其中右侧的大小固定。不幸的是,我在 stackoverflow 和 Google 上都找不到有效的解决方案。如果我在自己的上下文中实施,那么其中描述的每个解决方案都会失败。目前的解决方案是:

div.container {
    position: fixed;
    float: left;
    top: 100px;
    width: 100%;
    clear: both;
}

#content {
    margin-right: 265px;
}

#right {
    float: right;
    width: 225px;
    margin-left: -225px;
}

#right, #content {
    height: 1%; /* fixed for IE, although doesn't seem to work */
    padding: 20px;
}
<div class="container">
    <div id="content">
        fooburg content
    </div>
    <div id="right">
        test right
    </div>
</div>

我得到以下代码:

|----------------------- -------|
| fooburg content  |            |
|-------------------------------|
|                  | test right | 
|----------------------- -------|

请指教。非常感谢!

【问题讨论】:

    标签: css html


    【解决方案1】:

    删除左列的浮动。

    在 HTML 代码中,右列需要在左列之前。

    如果右边有一个浮点数(和一个宽度),如果左列没有一个宽度并且没有浮点数,它将是灵活的:)

    同时将overflow: hidden 和一些高度(可以是自动)应用到外部 div,以便它包围两个内部 div。

    最后,在左列添加width: autooverflow: hidden,这使得左列独立于右列(例如,如果您调整了浏览器窗口的大小,而右列接触了左列,如果没有这些属性,左列将围绕右列运行,而有了这些属性,它将保留在其空间中。

    示例 HTML:

    <div class="container">
        <div class="right">
            right content fixed width
        </div>
        <div class="left">
            left content flexible width
        </div>
    </div>
    

    CSS:

    .container {
       height: auto;
       overflow: hidden;
    }
    
    .right {
        width: 180px;
        float: right;
        background: #aafed6;
    }
    
    .left {
        float: none; /* not needed, just for clarification */
        background: #e8f6fe;
        /* the next props are meant to keep this block independent from the other floated one */
        width: auto;
        overflow: hidden;
    }​​
    

    此处示例:http://jsfiddle.net/jackJoe/fxWg7/

    【讨论】:

    • @Mir A clear: both inside 任何列都不会影响外部浮动。这不是“脆弱的”,除非您将清除放置在列的同一级别在列之间,如果将其放在最后不会造成任何伤害。
    • 我会考虑使用亚当的例子。我认为在 html 标记中将右列放在左列之前不是一个好主意。
    • @Danny_Joris 我同意。此外,如果您使用媒体查询,现在很难将右列低于左列
    • 对于那些对其工作原理感到好奇的人,可以在这里找到解释:stackoverflow.com/questions/25475822/…
    • 我想知道是否有办法让右列在左后,以便正确堆叠(不使用 flexbox)
    【解决方案2】:

    http://www.alistapart.com/articles/negativemargins/,这正是你需要的(example 4 那里)。

    <div id="container">
        <div id="content">
            <h1>content</h1>
            <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  Phasellus varius eleifend tellus. Suspendisse potenti. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nulla facilisi. Sed wisi lectus, placerat nec, mollis quis, posuere eget, arcu.</p>
            <p class="last">Donec euismod. Praesent mauris mi, adipiscing non, mollis eget, adipiscing ac, erat. Integer nonummy mauris sit amet metus. In adipiscing, ligula ultrices dictum vehicula, eros turpis lacinia libero, sed aliquet urna diam sed tellus. Etiam semper sapien eget metus.</p>
        </div>
    </div>
    
    <div id="sidebar">
        <h1>sidebar</h1>
        <ul>
            <li>link one</li>
            <li>link two</li>
        </ul>
    </div>
    

    #container {
        width: 100%;
        background: #f1f2ea url(background.gif) repeat-y right;
        float: left;
        margin-right: -200px;
    }
    #content {
        background: #f1f2ea;
        margin-right: 200px;
    }
    #sidebar {
        width: 200px;
        float: right;
    

    【讨论】:

    • 神奇、简单的解决方案,并且保持正确的 HTML 顺序!
    • 我不知道这件事。我怎么不知道这件事。完美的!我一直在尝试完成整个“流畅输入,固定宽度的搜索按钮”,显然源顺序在这里非常重要。这钉它。谢谢!
    • 我喜欢这个解决方案,因为在移动断点时间到来时,右栏/侧边栏将出现在左栏内容下方而不是上方。
    • 我无法使用此方法将正确的列移至顶部。
    【解决方案3】:

    嘿,你可以做的是对两个容器应用一个固定宽度,然后使用另一个 div 类,其中 clear:both,比如

    div#left {
    
    width: 600px;
    float: left;
    }
    
    div#right {
    
    width: 240px;
    float: right;
    
    }
    
    div.clear {
    
    clear:both;
    
    }
    

    在左右容器下放置一个清晰的 div。

    【讨论】:

      【解决方案4】:

      最好避免将右列放在左列之前,只需使用负的右边距。

      并通过包含@media 设置来“响应”,以便在窄屏幕上右列位于左下方。

      <div style="background: #f1f2ea;">
        <div id="container">
          <div id="content">
              <strong>Column 1 - content</strong>
          </div>
        </div>
        <div id="sidebar">
          <strong>Column 2 - sidebar</strong>
        </div>
      <div style="clear:both"></div>
      

      <style type="text/css">
      #container {
          margin-right: -300px;
          float:left;
          width:100%;
      }
      #content {
          margin-right: 320px; /* 20px added for center margin */
      }
      #sidebar {
          width:300px;
          float:left
      }
      @media (max-width: 480px) {
          #container {
              margin-right:0px;
              margin-bottom:20px;
          }
          #content {
              margin-right:0px;
              width:100%;
          }
          #sidebar {
              clear:left;
          }
      }
      </style>
      

      【讨论】:

      • 很好的解决方案。在 HTML 中保持右下角对于博客等布局至关重要,因为左侧有更重要的内容。
      • 优秀的答案!这是 Codepen 上的一个工作示例:codepen.io/martinkrulltott/pen/yNxezM
      【解决方案5】:

      我已经简化了:我已经编辑了 jackjoe 的答案。我认为不需要高度自动等。

      CSS:

      #container {
      position: relative;
      margin:0 auto;
      width: 1000px;
      background: #C63;
      padding: 10px;
      }
      
      #leftCol {
      background: #e8f6fe;
      width: auto;
      }
      
      #rightCol {
      float:right;
      width:30%;
      background: #aafed6;
      }
      
      .box {
      position:relative;
      clear:both;
      background:#F39;
       }
      </style>
      

      HTML:

      <div id="container">
      
        <div id="rightCol"> 
         <p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
      
        <p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
       </div>
      
       <div id="leftCol">
      
         <p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
      
        <p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
        <p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
      

      Lorem ipsum dolor sit amet,consectetuer adipiscing elit。不同的菜豆。 Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend。

      </div>
      
      </div>
      
      <div class="box">
        <p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
      
        <p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
        <p>Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Phasellus varius eleifend. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Phasellus varius eleifend.</p>
      </div>
      

      【讨论】:

      • 原问题想要一个固定大小的右列。
      【解决方案6】:

      这是一个通用的、按 HTML 源代码排序的解决方案,其中:

      • 源顺序的第一列是流动的
      • 源顺序中的第二列是固定的
        • 此列可以使用 CSS 向左或向右浮动

      固定/右侧第二列

      #wrapper {
        margin-right: 200px;
      }
      #content {
        float: left;
        width: 100%;
        background-color: powderblue;
      }
      #sidebar {
        float: right;
        width: 200px;
        margin-right: -200px;
        background-color: palevioletred;
      }
      #cleared {
        clear: both;
      }
      <div id="wrapper">
        <div id="content">Column 1 (fluid)</div>
        <div id="sidebar">Column 2 (fixed)</div>
        <div id="cleared"></div>
      </div>

      固定/左侧第二列

      #wrapper {
        margin-left: 200px;
      }
      #content {
        float: right;
        width: 100%;
        background-color: powderblue;
      }
      #sidebar {
        float: left;
        width: 200px;
        margin-left: -200px;
        background-color: palevioletred;
      }
      #cleared {
        clear: both;
      }
      <div id="wrapper">
        <div id="content">Column 1 (fluid)</div>
        <div id="sidebar">Column 2 (fixed)</div>
        <div id="cleared"></div>
      </div>

      替代解决方案是使用display: table-cell;这会导致等高的列。

      【讨论】:

      • 右侧的第二列不起作用。如果左列有完整的文本,您的右列将显示为新行。
      • 您是否尝试过放置更多内容并调整大小。刚刚测试了你的代码,没有用。
      • @TomSawyer 我不确定你在说什么。这是我尝试添加更多内容:jsfiddle.net/salman/mva6cnxLjsfiddle.net/salman/mva6cnxL/1。完美运行。
      • 正是我搜索的内容。谢谢
      【解决方案7】:

      最简单、最灵活的解决方案到目前为止使用table display

      HTML,左 div 在前,右 div 在后……我们从左到右读写,所以从右到左放置 div 没有任何意义

      <div class="container">
          <div class="left">
              left content flexible width
          </div>
          <div class="right">
              right content fixed width
          </div>
      </div>
      

      CSS:

      .container {
        display: table;
        width: 100%;
      }
      
      .left {
        display: table-cell;
        width: (whatever you want: 100%, 150px, auto)
      }​​
      
      .right {
        display: table-cell;
        width: (whatever you want: 100%, 150px, auto)
      }
      

      案例示例:

      // One div is 150px fixed width ; the other takes the rest of the width
      .left {width: 150px} .right {width: 100%}
      
      // One div is auto to its inner width ; the other takes the rest of the width
      .left {width: 100%} .right {width: auto}
      

      【讨论】:

      • 很好,很好,谢谢。有时,当 flexbox 不是一个可行的替代方案时,表格是有时间和地点的。而不是在没有正确堆叠的 DOM 中放置正确的内容..
      • 我喜欢这是一个“干净”的解决方案。但是,将 div 置于表格单元格模式的唯一问题是您不妨使用 Tables & Tds。你最终会失去溢出滚动等功能。
      • 这是不公平的,因为这个解决方案至少在语义上是正确的并且对简单的 RWD 技术友好,而使用 tabletds 肯定不是!
      • 如果列变窄,此方法很容易允许媒体查询删除常规 div 的表。干净整洁。我喜欢。
      【解决方案8】:

      我想提出一个尚未提及的解决方案:使用 CSS3 的 calc() 混合 %px 单位。 calc() 现在有 excellent support,它允许快速构建相当复杂的布局。

      这是下面代码的JSFiddle 链接。

      HTML:

      <div class="sidebar">
        sidebar fixed width
      </div>
      <div class="content">
        content flexible width
      </div>
      

      CSS:

      .sidebar {
          width: 180px;
          float: right;
          background: green;
      }
      
      .content {
          width: calc(100% - 180px);
          background: orange;
      }
      

      这是另一个JSFiddle,展示了这个概念应用于更复杂的布局。我在这里使用了 SCSS,因为它的变量允许灵活和自我描述的代码,但如果“硬编码”值不是问题,布局可以很容易地在纯 CSS 中重新创建。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-08
        • 1970-01-01
        • 2011-08-04
        • 1970-01-01
        • 2014-02-18
        • 2012-01-05
        • 2015-06-17
        相关资源
        最近更新 更多