【问题标题】:How to make two separate columns the same length? [duplicate]如何使两个单独的列长度相同? [复制]
【发布时间】:2014-04-29 21:05:39
【问题描述】:

我有两列,一列向左浮动,另一列向右浮动。

内容通常会有不同的长度,但内容较少的内容总是需要与内容较多的内容大小相同。

这是当前发生的情况的图像:

有没有办法(不固定)来确保它们保持相同的高度?

HTML

 <div class="leftCol">
  <div class="singleArrow"></div>
      <div class="sectionBlock">
        <div class="sectionBlockContentNarrow"> 
                    SOME TEXT
        </div>
      </div>
  </div>
  <div class="rightCol">
  <div class="singleArrow"></div>
      <div class="sectionBlock">
        <div class="sectionBlockContentNarrow"> 
                    SOME TEXT
        </div>
      </div>
  </div>

CSS:

.singleArrow{
    margin:0 auto;
    width:50px;
    height:23px;
    background-image:url('../images/downarrow-lrg.png');
    background-repeat:no-repeat;
    margin-top:20px;
    margin-bottom:20px;
}

.leftCol{
    float:left;
    width:300px;
}

.rightCol{
    float:right;
    width:300px;
}
.sectionBlock {
    position: relative;
    overflow: hidden;
    box-sizing: border-box;
    -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;    /* Firefox, other Gecko */
    box-sizing: border-box;         /* Opera/IE 8+ */
    width: 100%;
    min-height: 40px;
    background-color: rgb(246,246,246);
    margin-top: 20px;
    background-image: url('../images/bar.png');
    background-repeat: repeat-x;
    padding: 4px 0 0 10px;
    padding-bottom:20px;
    }

.sectionBlockContent{
    padding: 30px 20px 10px 30px;

}
.sectionBlockContentNarrow{
    padding: 30px 10px 10px 10px;

}

【问题讨论】:

  • 请提供 HTML 和当前 CSS。
  • @Paulie_D 不幸的是,我无法使该方法起作用,它似乎使它忽略了宽度设置并填充到 100%。

标签: css


【解决方案1】:

您可以选择 2 种变体

1.) JS(恕我直言)

$(window).load(function() {
    //call the equalize height function
    equalHeight($("div.left, div.middle, div.right"));

    //equalize function
    function equalHeight(group) {
        tallest = 0;
        group.each(function() {
            thisHeight = $(this).height();
            if(thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }
}); 

2.) 背景造型(老派)

您需要从 PSD 中剪切 1 px bg 并将 bg 放入列容器(repeat-y)

3.) 表(老派) HTML

 <div class="container">
        <div class="child">...</div>
        <div class="child">...</div>
        <div class="child">...</div>
    </div>

CSS

.container {
    display: table;
}

.child {
    display: table-cell;
    width: 33.33%;
}

【讨论】:

  • 谢谢,表格方法是我考虑的方法
  • @joey,这是我向你求婚的第一个 :(
  • 我不认为显示表是老派的,它是自 1998 年以来的标准,并且仅受 IE8 支持,它与 HTML 表毫无关系。 list-item 和 table 是可用于显示的值,并且是 li 和 td 的默认显示,例如 bloc 用于 div。 :)
  • 如果说我不喜欢使用表格可能是错误的,因为它与IE7不兼容。但我同意“老派”不是我观点的恰当描述
【解决方案2】:

你有几个选择不使用float

  1. display:table; 在父母身上,display:table-cell 在孩子身上。 DEMO
  2. display:flex 在父级上。 DEMO
  3. 还有老式但仍然可靠的仿柱技术:DEMO
  4. 还有其他一些棘手的DEMO这个涉及浮动元素,或者没有伪元素,同样的溢出隐藏技术DEMO

【讨论】:

  • Flex 不适合,因为它也匹配宽度。必须设置宽度。
  • Flex 是新技术,几乎不支持主流浏览器
  • 选项不是必须做的选项:)
  • @Joey thnks 给我一些关于我向您展示的不涉及 javascript 的不同技术的反馈(不要对 flex 感到困惑,有一天您会喜欢它的;))
猜你喜欢
  • 2023-01-22
  • 1970-01-01
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多