我认为这是 IE9/10 特有的问题。我正在尝试创建一个灵活的行布局,如下所示:
|----------------------------------|
| FIXED HEIGHT |
|----------------------------------|
| FIXED HEIGHT, SOMETIMES HIDDEN|
|----------------------------------|
| |
| FLEXIBLE HEIGHT |
| |
|----------------------------------|
| FIXED HEIGHT |
|----------------------------------|
我还希望它的外部容器具有适合浏览器视口(viewport)的灵活高度和宽度。
我可能天真地认为,既然我只支持 Chrome、Firefox、Safari 和 IE9/10,那么 display: table
, display: table-row
, display: table-cell
可能是个好方法,所以我想出了:
jsFiddle:http://jsfiddle.net/Nugps/2/
HTML:
<div class="stage">
<div class="table">
<div class="row">
<div class="cell">
<div class="content1">one</div>
</div>
</div>
<div class="row">
<div class="cell">
<div class="content1">two</div>
</div>
</div>
<div class="row flexible">
<div class="cell">
<div class="content2">three</div>
</div>
</div>
<div class="row">
<div class="cell">
<div class="content1">four</div>
</div>
</div>
</div>
</div>
CSS:
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
.stage {
position: absolute;
top: 15px;
right: 15px;
bottom: 15px;
left: 15px;
}
.table {
display: table;
height: 100%;
width: 100%;
background: lightblue;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
border: 3px solid red;
height: 50px;
}
.row.flexible {
height: 100%;
}
.row.flexible .cell {
height: 100%;
}
.content2 {
background: lightgreen;
height: 100%; /* setting this in IE causes the content to be 100% of the table rather than the table cell height */
}
如果你在 Chrome 中打开这个 jsFiddle,一切都如预期的那样。但是在 IE10 中,我怀疑 IE9 也一样,高度被搞乱了(.content2 高度是表格的 100% 而不是单元格)。
除了使用 javascript 手动设置内容的高度之外,是否有好的解决方法?或者我可以使用更好的 CSS 布局吗?由于 IE9,我不能使用较新的 flexbox。
最佳答案
我想知道 content2 类是否真的需要样式,您能不能只将背景颜色添加到 .row.flexible .cell 并一起摆脱 .content2 样式。
这是我的解决方案: http://cdpn.io/Ewrsa
它在 IE9/10 中都有效
关于html - 在 IE9/10 中将表格单元格内部 div 的高度设置为表格单元格高度的 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18960910/