标签在表格中垂直居中
我正在尝试创建一个包含 4 个小图像和一个垂直居中标签的布局,标签可以有多行。标签位于图像之上并遮盖图像。标签将动态切换。
我已经使用 div 或表格完成了此操作,但遇到了同样的问题。这是我能得到的最接近的表格版本:
p.label
{
position: absolute;
background: black;
color:white;
font-weight: bold;
text-align:center;
bottom:50%;
margin:0;
word-wrap: break-word;
}
table.groupbox
{
position:relative;
}
<table class="groupbox" cellspacing=0 cellpadding=0>
<tr>
<td><p class="label">two line<br>label</p>
<img src="A.jpg" height=80 width=80></td>
<td><img src="B.jpg" height=80 width=80></td>
</tr>
<tr>
<td><img src="C.jpg" height=80 width=80></td>
<td><img src="D.jpg" height=80 width=80></td>
</tr>
</table>
这正确地使标签跨越整个表格,但它将标签的底部边缘放在垂直中心。如果我将标签的边距更改为如下所示:
margin: 0 0 -20px 0;
我可以手动将标签居中,但它不适用于不同尺寸的标签。
两个答案
Nathan Manousos 和 Rob W 的答案似乎都很有效。
I'm trying to create a layout with 4 small images and a label centered vertically, the label could have multiple lines. The label sits on top of and obscures the images. The label will be dynamically toggled.
I have done this with divs or tables but run into the same problem. Here is my table version as close as I can get:
p.label
{
position: absolute;
background: black;
color:white;
font-weight: bold;
text-align:center;
bottom:50%;
margin:0;
word-wrap: break-word;
}
table.groupbox
{
position:relative;
}
<table class="groupbox" cellspacing=0 cellpadding=0>
<tr>
<td><p class="label">two line<br>label</p>
<img src="A.jpg" height=80 width=80></td>
<td><img src="B.jpg" height=80 width=80></td>
</tr>
<tr>
<td><img src="C.jpg" height=80 width=80></td>
<td><img src="D.jpg" height=80 width=80></td>
</tr>
</table>
This correctly has the label spanning the full table, but it puts the bottom edge of the label at the vertical center. If I change the label's margin to something like this:
margin: 0 0 -20px 0;
I can hand center the label, but it won't work for different size labels.
Two Answers
The answers by Nathan Manousos and Rob W both seem to work well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这段代码将生成一个 160x160 大小的正方形。您可以根据自己的意愿调整宽度和高度。
CSS:
HTML:
最终编辑
请记住,背景应该是背景。文本不应该因为背景而变得难以阅读。要实现此目的,您可以创建一个 1x1 透明图像:
您还可以使用
background-color:rgba(1,1,1,0.2)
,其中 0.2 是透明度级别(0=不可见,1=完全可见)。此 CSS 属性适用于 FF 3+、Chrome、Safari 3+、Opera 10+ 和 IE 9+。This piece of code will produce a square with a 160x160 size. You can adjust the width and height to your wishes.
CSS:
HTML:
Final edit
Keep in mind that background should be a background. The text should not become unreadable because of the background. To accomplish this, you can create a 1x1 transparent image:
You can also use
background-color:rgba(1,1,1,0.2)
where 0.2 is the transparency level (0=invisible, 1=fully visible). This CSS attrbute is available for FF 3+, Chrome, Safari 3+, Opera 10+ and IE 9+.好的,根据更新的图表,这是一个需要知道整个盒子的高度的解决方案。
这是实际操作: http://jsfiddle.net/raySU/
CSS:
HTML:
Okay, based on the updated diagram, here is a solution that requires knowing the height of the whole box.
Here it is in action: http://jsfiddle.net/raySU/
CSS:
HTML:
如果你在桌子周围添加一个容器,你应该能够很容易地做到这一点。
If you add a container around your table, you should be able to do this pretty easily.