我正在使用 table
布局,尝试将 table-cell
的子元素高度设置为其容器的 100%。为什么会出现此问题?如何解决?
我还尝试过将 table-cell 的高度设置为 100%,但没有成功。
我想仅将图像对齐到容器底部。在表格中如何实现这一点?
每个表格单元格中的文本数量是动态的。
Fiddle: http://jsfiddle.net/3u3gpf2n/1/
我正在使用 table
布局,尝试将 table-cell
的子元素高度设置为其容器的 100%。为什么会出现此问题?如何解决?
我还尝试过将 table-cell 的高度设置为 100%,但没有成功。
我想仅将图像对齐到容器底部。在表格中如何实现这一点?
每个表格单元格中的文本数量是动态的。
Fiddle: http://jsfiddle.net/3u3gpf2n/1/
看起来你想创建一个3列2行的表格,但只使用单行以保持你的标记语言的语义化。如果没有flexbox,将图片分配到底部将需要一些CSS技巧。
一个例子,应该符合你的要求:(1)跨浏览器,包括IE兼容性(2)允许在框中使用任意文本:
*{
padding: 0;
margin: 0;
}
#container{
width: 500px;
margin: 0 auto;
}
ul{
list-style-type:none;
display: inline-table;
}
li{
position: relative;
/* image width / (image height * column count) */
padding-bottom: 11.764705882%;
display: table-cell;
background: red;
/* 100 / column count */
width: 33.333%;
}
a{
color: white;
}
img{
width: 100%;
position: absolute;
bottom: 0;
}
<div id="container">
<ul>
<li><a href><div class="header">Xiao Huang</div>
<img src="http://3door.com/sites/default/files/styles/ablum_306_108/public/special/xiao_huang_ren_fu_ben_.jpg?itok=cPnwgTyV" /></a></li>
<li><a href><div class="header">Xiao Huang</div>
<img src="http://3door.com/sites/default/files/styles/ablum_306_108/public/special/xiao_huang_ren_fu_ben_.jpg?itok=cPnwgTyV" /></a></li>
<li><a href><div class="header">Xiao Huang Xiao Huang Xiao Haung</div>
<img src="http://3door.com/sites/default/files/styles/ablum_306_108/public/special/xiao_huang_ren_fu_ben_.jpg?itok=cPnwgTyV" /></a></li>
</ul>
</div>
image width / (image height * column count)
的计算方法.. 真让人头疼。如果图像高度不同,那就有点麻烦了,不是吗? - MortenMouldertable-cell
的子元素高度设置为其容器的 100%。为什么会失败,如何解决?table-cell
的高度设置为 100%,但没有成功。body
和根元素 html
)指定高度。height
property
percentage
指定百分比高度。百分比是相对于生成的框的包含块的高度计算的。如果未明确指定包含块的高度且此元素未绝对定位,则该值计算为“auto”。我想仅将图像与容器底部对齐,如何在表格显示中做到这一点?尽管vertical-align非常适用于将元素对齐到表格单元格中,但在这种情况下,由于每个图像都有一个不得移动的兄弟元素,因此该属性是无用的。但是,可以使用绝对定位来移动图像。请尝试以下操作:
li{
display: table-cell;
background: red;
height: 100%;
position: relative; /* establish nearest positioned ancestor for abs. positioning */
width: 33.33%; /* new */
}
img{
width: 100%;
position: absolute; /* new */
bottom: 0; /* new */
}
演示: http://jsfiddle.net/sedetcc6/1/
注:
*{
height: 50px;
}
你将会得到你所期望的结果:http://jsfiddle.net/asimplepeter/3u3gpf2n/10/
如果文字的数量成为了问题,你可以给你的.header
类设置overflow: auto
。
display: table-cell
,这样它们就会被强制具有相同的高度,然后您可以定位所有在锚点内的元素,甚至可以使用背景图像代替 <img />
你的代码和显示属性,如“table”和“table-cell”,都是正确的。
希望这回答了您所有的疑问。
编辑:由于设置边距会扩展表格高度,因此您可以使用以下CSS属性作为替代方法。
transform: translateY(Y);
transform-origin: left top;
li{
position: relative;
display: table-cell;
background: red;
height: 100%;
vertical-align: bottom;
}
.header{
width: 100%;
position: absolute;
display: block;
color: white;
background: blue;
}