标签在表格中垂直居中

发布于 2024-12-04 04:57:04 字数 1076 浏览 0 评论 0原文

我正在尝试创建一个包含 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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦初启 2024-12-11 04:57:04

这段代码将生成一个 160x160 大小的正方形。您可以根据自己的意愿调整宽度和高度。

CSS:

.groupbox, .groupbox .label-container {
  width: 160px;
  height: 160px;
}
.groupbox {
  position: relative;
}
.groupbox .label-container {
  display: table;
  position: absolute;
}
.groupbox .A, .groupbox .B, .groupbox .C, .groupbox .D{
  width: 50%;
  height: 50%;
  background-position: center;
  position: absolute;
  background-repeat: no-repeat;
}
.groupbox .A{background-image:url("A.jpg");top:0;left:0;}
.groupbox .B{background-image:url("B.jpg");top:0;right:0;}
.groupbox .C{background-image:url("C.jpg");bottom:0;left:0;}
.groupbox .D{background-image:url("D.jpg");bottom:0;right:0;}

.groupbox .label {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

HTML:

<div class="groupbox">
  <div class="A"></div><div class="B"></div>
  <div class="C"></div><div class="D"></div>
  <div class="label-container">
    <div class="label">Any text<br />With multiple lines</div>
  </div>
</div>

最终编辑

请记住,背景应该是背景。文本不应该因为背景而变得难以阅读。要实现此目的,您可以创建一个 1x1 透明图像:

.groupbox .label-container {
    background:url("transparent.png") repeat transparent;
}

您还可以使用 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:

.groupbox, .groupbox .label-container {
  width: 160px;
  height: 160px;
}
.groupbox {
  position: relative;
}
.groupbox .label-container {
  display: table;
  position: absolute;
}
.groupbox .A, .groupbox .B, .groupbox .C, .groupbox .D{
  width: 50%;
  height: 50%;
  background-position: center;
  position: absolute;
  background-repeat: no-repeat;
}
.groupbox .A{background-image:url("A.jpg");top:0;left:0;}
.groupbox .B{background-image:url("B.jpg");top:0;right:0;}
.groupbox .C{background-image:url("C.jpg");bottom:0;left:0;}
.groupbox .D{background-image:url("D.jpg");bottom:0;right:0;}

.groupbox .label {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

HTML:

<div class="groupbox">
  <div class="A"></div><div class="B"></div>
  <div class="C"></div><div class="D"></div>
  <div class="label-container">
    <div class="label">Any text<br />With multiple lines</div>
  </div>
</div>

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:

.groupbox .label-container {
    background:url("transparent.png") repeat transparent;
}

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+.

双手揣兜 2024-12-11 04:57:04

好的,根据更新的图表,这是一个需要知道整个盒子的高度的解决方案。

这是实际操作: http://jsfiddle.net/raySU/

CSS:

  *{margin:0;padding:0;}
  #wrap{width:200px; height:200px; position:relative;}
  #wrap #images{position:absolute;}
  #wrap #images img{float:left;}
  #wrap #label{position:absolute; width:200px; height:200px;}
  #wrap #label table{width:100%; height:100%;}
  #wrap #label p{background-color:#eee;}

HTML:

<div id="wrap">

  <div id="images">
    <img src="http://placekitten.com/100" width="100" height="100" />
    <img src="http://placekitten.com/100" width="100" height="100" />
    <img src="http://placekitten.com/100" width="100" height="100" />
    <img src="http://placekitten.com/100" width="100" height="100" />
  </div>

  <div id="label">
    <table cellspacing="0" cellpadding="0">
      <tr>
        <td valign="center">
          <p>This is my label here isnt it great yes it is</p>
        </td>
      </tr>
    </table>
  </div>

</div>

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:

  *{margin:0;padding:0;}
  #wrap{width:200px; height:200px; position:relative;}
  #wrap #images{position:absolute;}
  #wrap #images img{float:left;}
  #wrap #label{position:absolute; width:200px; height:200px;}
  #wrap #label table{width:100%; height:100%;}
  #wrap #label p{background-color:#eee;}

HTML:

<div id="wrap">

  <div id="images">
    <img src="http://placekitten.com/100" width="100" height="100" />
    <img src="http://placekitten.com/100" width="100" height="100" />
    <img src="http://placekitten.com/100" width="100" height="100" />
    <img src="http://placekitten.com/100" width="100" height="100" />
  </div>

  <div id="label">
    <table cellspacing="0" cellpadding="0">
      <tr>
        <td valign="center">
          <p>This is my label here isnt it great yes it is</p>
        </td>
      </tr>
    </table>
  </div>

</div>
满意归宿 2024-12-11 04:57:04

如果你在桌子周围添加一个容器,你应该能够很容易地做到这一点。

<div class="container">
  <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>
  <p class="label">Your text here</p>
</div>

.container { 
  position: relative;
  float: left; }
.label { 
  position: absolute;
  top: 50%;
  right: 0;
  height: 18px; /* fixed height */
  margin: -9px 0 0; }

If you add a container around your table, you should be able to do this pretty easily.

<div class="container">
  <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>
  <p class="label">Your text here</p>
</div>

.container { 
  position: relative;
  float: left; }
.label { 
  position: absolute;
  top: 50%;
  right: 0;
  height: 18px; /* fixed height */
  margin: -9px 0 0; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文