简体   繁体   English

不能让DIV的高度相等

[英]Can't get DIV's equal in height

As simple as it normally is, for some reason I can't seem to get the same column heights for three divs in a row without using min-height or jQuery. 通常很简单,出于某种原因,我似乎无法在不使用min-height或jQuery的情况下连续获得三个div的相同列高度。

I am trying to use display:table on the main div and display:table-cell on the child's. 我试图在主div上使用display:tabledisplay:table-cell child display:table-cell display:table上的display:table-cell I have no clue what is going wrong. 我不知道出了什么问题。 I also tried other solutions, eg display:inline-block; 我也尝试过其他解决方案,例如display:inline-block; and :before { content:"" }; :before { content:"" }; as suggested in several other posts. 正如其他几个帖子所建议的那样

<div class="row">

<!-- 3 times in a row, other two blocks cut here for overview on SO -->

<div class="columns small-12 medium-4 large-4">
<div class="highlighted-contentblock">
    <a href="#" target="_blank">
        <div class="contentheader">
            <div class="sequence">
                1
            </div>
            <h2>
                Lorem ipsum
            </h2>
        </div>
        Lorem
    </a>
</div>  
</div>
</div>

Fiddle 小提琴

 .large-4 {
    width: 33.33333%;
display:table; }


.highlighted-contentblock
{
    background:gray;
    display:table-cell;
}

Fiddle 小提琴

The Problem 问题

It looks like the original issue was trying to apply table-cell to the class highlighted-contentblock instead of columns. 看起来原始问题是尝试将table-cell应用于highlight-contentblock而不是列。 The columns class is the first child of your row class and table-cell should be applied there. columns类是行类的第一个子类,应该在那里应用table-cell。

Columns

You had display table on your columns (applied on the highlighted-contentblock class). 您的列上有显示表(应用于highlight-contentblock类)。 I replaced that with table-cell applied to the columns class. 我将table-cell替换为应用于columns类的替换。

Spacing 间距

Float removes margin so I removed that and used border spacing on your table instead. Float删除了边距,因此我将其移除并在桌面上使用了边框间距。

Support 支持

This works but not in IE 7. CSS tricks has more info . 这有效,但在IE 7中没有.CSS技巧有更多信息

Click run code snippet below to see it in action. 点击下面的运行代码段,查看其实际效果。

 /* Foundation replacement */ .row { padding-bottom:10px; max-width:980px; margin:0 auto; display:table; border-spacing:0.9375rem; } .columns { box-sizing:border-box; display:table-cell; background:gray; } .large-4 { width: 33.33333%; } 
 <div class="row"> <div class="columns small-12 medium-4 large-4"> <div class="highlighted-contentblock"> <a href="#" target="_blank"> <div class="contentheader"> <div class="sequence"> 1 </div> <h2> Lorem ipsum </h2> </div> Lorem </a> </div> </div> <div class="columns small-12 medium-4 large-4" data-equalizer-watch=""> <div class="highlighted-contentblock"> <a target="_blank" href=#> <div class="contentheader"> <div class="sequence"> X </div> <h2> Lorem ipsum <br/> Lorem ipsum </h2> </div> Lorem ipsum</a> </div> </div> <div class="columns small-12 medium-4 large-4" data-equalizer-watch=""> <div class="highlighted-contentblock"> <a target="_blank" href=#> <div class="contentheader"> <div class="sequence"> 3 </div> <h2> Lorem ipsum </h2> </div> Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</a> </div> </div> </div> 

Your html is a bit messy to be honest. 说实话,你的HTML有点乱。 You want to be following this simple structure: 你想要遵循这个简单的结构:

<div class="row">
    <div class="col">Content</div>
    <div class="col">Content</div>
    <div class="col">Content</div>
</div>

And then set a few simple CSS rules 然后设置一些简单的CSS规则

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

The crucial thing here is setting the width of your table cells. 这里至关重要的是设置表格单元格的宽度。

I have updated your fiddle to show this in action http://jsfiddle.net/eaqr1b17/5/ 我已经更新你的小提琴,以显示这个行动http://jsfiddle.net/eaqr1b17/5/

With floated element, the table/table-cell rendering is a bit difficult to render correctly... 使用浮动元素,表/表格单元格渲染有点难以正确渲染...

If you can, remove the float element, and display them side by side with the table-cell. 如果可以,删除浮动元素,并将其与表格单元格并排显示。

The second problem of your code is that you have a background on the child element of the one that is rendered with a table-cell. 代码的第二个问题是你有一个用table-cell渲染的子元素的背景。 So the height of all .column elements will be the same, but you won't see it with the background-color, because that property is setted to the child element that has its own height... 所以所有.column元素的高度都是相同的,但你不会用background-color看到它,因为该属性被设置为具有自己高度的子元素...

Here is an update of your fiddle without the float and with a border to show you that the height is correctly setted, but you don't see it because your bg-color is not applied to the right element : 这是你的小提琴的更新没有浮动和边框,以显示高度是否正确设置,但你没有看到它,因为你的bg颜色没有应用于正确的元素:

http://jsfiddle.net/eaqr1b17/6/ http://jsfiddle.net/eaqr1b17/6/

/* Foundation replacement */
.row
{
    padding-bottom:10px;  
    display:table;
    border: 1px solid red;
 }
.columns {
    padding-left: 0.9375rem;
    padding-right: 0.9375rem;
    box-sizing: border-box;
    display: table-cell;
    border: 1px solid green;
}
.large-4 {
    width: 33.33333%; 
}
.highlighted-contentblock
{
    background:lightgrey;
}

For your markup you have do to it like this: 对于你的标记,你可以这样做:

http://jsfiddle.net/rvos3hx3/ http://jsfiddle.net/rvos3hx3/

/* Foundation replacement */
 .row {
    padding-bottom:10px;
    max-width:980px;
    margin:0 auto;
    display:table;
}
.columns {
    position: relative;
    padding-left: 0.9375rem;
    padding-right: 0.9375rem;
    box-sizing:border-box;
    background:gray;
    display:table-cell;
}
.large-4 {
    width: 33.33333%;
}

I think this is what you need: 我想这就是你需要的:

HTML: HTML:

<div class="wrap">
<div class="row">
    <div class="cell">
    Lorem    
    </div>
    <div class="cell">
    Lorem impsum more text
    </div>
    <div class="cell">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    </div>
</div>

And for the css: 而对于CSS:

.wrap{
    overflow:hidden;
    width:250px;
    display: table;
    border-collapse: collapse;
}

.row {
    display: table-row;
}
.cell{
    width: 33.33%;
    display: table-cell; 
    background-color: #0f0;
}

if you want to see it running click here Fiddle 如果你想看到它运行,请点击这里小提琴

Here is a jsfiddle : http://jsfiddle.net/nrnLbv14/ 这是一个jsfiddle: http//jsfiddle.net/nrnLbv14/

HTML : HTML:

<div class="container">
    <div class="row">
        <div class="column">content 1</div>
        <div class="column">content 2</div>
        <div class="column">content 3</div>
    </div>
</div>

CSS : CSS:

.container {
    display:table;
    /* just styling to show it works */
    border-collapse: separate;
    border-spacing: 10px;
    background-color: gray;
}
.row {
    display:table-row;
}
.row .column {
    display:table-cell;
    background-color: #fff;
    width: 33.33%; // If you want all 3 columns equal width
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM