[英]How can I prevent this div in my table from increasing the cell height?
I have an example of the problem here: http://jsfiddle.net/s6t3p/ 我在这里有一个问题的例子: http : //jsfiddle.net/s6t3p/
I have the following markup: 我有以下标记:
<h1>Just Text</h1>
<table>
<tr>
<td class='cell'>Some Text</td>
<td class='cell'>More Text</td>
</tr>
</table>
<h1>With Icon Cell</h1>
<table>
<tr>
<td class='cell'>Some Text</td>
<td class='cell-ico'><div class='ico'></div></td>
<td class='cell'>More Text</td>
</tr>
</table>
And the following CSS: 以及以下CSS:
table,td,tr
{
margin: 0; padding: 0;
border: none; outline: none;
font-size: 100%; font: inherit;
vertical-align: baseline;
}
table { border-collapse: collapse; border-spacing: 0;}
.cell
{
border:solid 1px #DDD; padding:0 5px; text-align:left;
line-height:27px;
}
.ico {width:24px; height:24px; background:#05F;}
I wish for each cell to be 27px in height. 我希望每个单元格的高度为27px。 This is the case for the 1st table.
第一个表就是这种情况。 But the second table which contains a cell with a 24px div has a height of 32px.
但是第二个表格包含一个div值为24px的单元格,其高度为32px。 I don't understand why.
我不明白为什么。 It seems like a 24x24 div should fit inside fine without increasing the cell height.
似乎24x24 div应该适合内部精细而不增加像元高度。
Can anyone explain why this is happening and how to fix it so that I can keep the cells at a height of 27px? 谁能解释为什么会这样以及如何解决它,以便我可以将单元格的高度保持在27px? I am currently testing with FireFox 29.
我目前正在使用FireFox 29进行测试。
Thanks for any help. 谢谢你的帮助。
If you are looking to display tabular data , then this is the way to go :) 如果您希望显示表格数据 ,那么这就是方法:)
The behaviour is caused by vertical-align: baseline;
该行为是由
vertical-align: baseline;
引起的vertical-align: baseline;
. 。
Just reset the vertical-align
for .cell-ico
and provide a set height. 只需重置
.cell-ico
的vertical-align
并提供设置的高度即可。 Give .ico
a height of 100%. 将
.ico
的高度设置为100%。
.cell-ico {
height: 24px;
vertical-align: top;
}
.ico {
width:24px;
height: 100%;
background:#05F;
display: block;
}
I'd suggest you use div elements with their respective display attributes set to either table
, table-row,
table-cell
, etc. Instead of actual tables. 我建议您使用div元素,并将它们各自的显示属性设置为
table
, table-row,
table-cell
等。而不是实际的表。 The use of tables as a layout techique is discouraged since HTML4 . 从HTML4开始,不建议将表格用作布局技术。 This approach may be a little bulky, but for layout purposes it'll save you some trouble dealing with css.
这种方法可能有点笨重,但是出于布局目的,它将为您节省一些处理CSS的麻烦。
Check this jsfiddle for example : 检查此jsfiddle例如 :
HTML : HTML :
<div id="Table">
<div class="row">
<div class="cell">Lorem ipsum</div>
<div class="cell">Lorem ipsum</div>
</div>
<div class="row">
<div class="cell">Lorem ipsum</div>
<div class="cell">Lorem ipsum</div>
</div>
</div>
<br/>
<br/>
<div id="Table">
<div class="row">
<div class="cell2"><div class="oddDiv">im inside a div</div></div>
<div class="cell2">Lorem ipsum</div>
</div>
<div class="row">
<div class="cell2">Lorem ipsum</div>
<div class="cell2">Lorem ipsum</div>
</div>
</div>
CSS : CSS :
#Table {
display: table;
width: 200px;
border-collapse: collapse;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
border: 1px solid blue;
height: 27px;
}
.cell2 {
display: table-cell;
border: 1px solid red;
height: 24px;
}
.oddDiv {
height: 24px;
width: 24px;
overflow: hidden;
}
Update, regarding semantics : 关于语义的更新 :
It's true that from a developer's point of view there's no difference between a table markup element and a generic container like a div with the appearance of a table in terms of layout and even code comprehension. 确实,从开发人员的角度来看,表标记元素和div之类的通用容器之间在表的外观和代码理解方面都没有区别。 But, there's no way for an accessibility tool, say, an screen reader, to discern between a bunch of div elements with different id's, unless the tool was proprietary or there was some kind of convention for special id's to use for this kind of purposes (which I doubt).
但是,无障碍工具(例如屏幕阅读器)无法辨别一堆具有不同ID的div元素,除非该工具是专有的,或者有某种特殊的约定用于特殊目的以用于此类目的(我对此表示怀疑)。 So for layout purposes divs are the way to go, but if semantics are a concern, the use of tables is the best solution.
因此,出于布局目的,div是最好的解决方法,但是如果要考虑语义,则使用表是最好的解决方案。
try to add this inside your CSS 尝试将其添加到您的CSS中
td{
vertical-align:middle;
height:27px;
}
Looks like vertical-align: baseline; 看起来像垂直对齐:基线; applied to all tags table, tr, td is messing it up.
应用于所有标签表,tr,td将其弄乱了。 Remove it and try it out.
删除并尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.