html - 在表格单元格 html 中垂直和水平居中所有图像的正确方法

标签 html css alignment vertical-alignment

我有一个带有 html 页面的表格(它将是一个产品页面)。

每行的第一个单元格有一个图像但没有文本(这是一个固定大小),我需要将图像垂直和水平居中。列名的第一行显然没有图像。会有很多行,每行在第一个单元格中都有不同的图像,我想将“中心 V&H”应用于所有行。

在一周的时间里,我每天都花几个小时的时间试图找到适合我的代码的答案,我已经搜索并尝试了其他帖子中的所有建议,但它打败了我,因此在这里问。

我目前正在尝试 table img {align: center; vealign: center;} 但这不起作用。我也尝试了所有可以在网上找到的不同变体,但没有成功。

另外我正在使用,因为会有多个表格,每个表格都有不同的列宽。

我不是专家所以请客气,但我正在学习..

<style>
table {width:100%;}
table, th, td {border: 1px solid black; border-collapse: collapse;}
table tr:nth-child(even) {background-color: #ebebeb; color: black}
table tr:nth-child(odd) {background-color: #ffffff; color: black}
table th {background-color: black; color: white; font-weight: bold;}
table img {align: center; vealign: center;}
th, td {padding: 5px; text-align: left;}

</style>
<body>

<table id="t01">
  <col width="10%"></col>
  <col width="20%"></col>
  <col width="30%"></col>
  <col width="20%"></col>
  <col width="20%"></col>
<tr>
    <th>Heading 1</th>
    <th>Heading 2</th>  
    <th>Heading 3</th>
    <th>Heading 4</th>
    <th>Heading 5</th>
</tr>
<tr>
    <td ><img src="http://imageshack.com/a/img661/6216/lOGXW9.jpg" /></td>
    <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br />Nam dolor odio, lacinia in ... <br>Pellentesque ac enim vitae lorem aliquet pretium.        </td>
    <td>More Text</td>
    <td>Yet More Text</td>
    <td>Even More Text</td>
</tr>
<tr>
<td ><img src="http://imageshack.com/a/img661/6216/lOGXW9.jpg" /></td>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br />Nam dolor odio, lacinia in ... <br>Pellentesque ac enim vitae lorem aliquet pretium.</td>
<td>More Text</td>
<td>Yet More Text</td>
<td>Even More Text</td>
</tr>

</tbody>
</table>

</body>

最佳答案

我认为您希望表格单元格具有默认的 text-align: leftvertical-align: top

由于一行中的第一个 td 元素是 tr 元素的第一个子元素,您可以使用 first-child 选择器来应用 text-align: centervertical-align: middle 定位图像。

引用:http://www.w3.org/wiki/CSS/Selectors/pseudo-classes/:first-child

table {
  width: 100%;
}
table,
th,
td {
  border: 1px solid black;
  border-collapse: collapse;
}
table tr:nth-child(even) {
  background-color: #ebebeb;
  color: black
}
table tr:nth-child(odd) {
  background-color: #ffffff;
  color: black
}
table th {
  background-color: black;
  color: white;
  font-weight: bold;
}
th,
td {
  padding: 5px;
  text-align: left;
}
td {
  vertical-align: top;
}
td:first-child {
  vertical-align: middle;
  text-align: center;
}
<table id="t01">
  <col width="10%"></col>
  <col width="20%"></col>
  <col width="30%"></col>
  <col width="20%"></col>
  <col width="20%"></col>
  <tr>
    <th>Heading 1</th>
    <th>Heading 2</th>
    <th>Heading 3</th>
    <th>Heading 4</th>
    <th>Heading 5</th>
  </tr>
  <tr>
    <td>
      <img src="http://imageshack.com/a/img661/6216/lOGXW9.jpg" />
    </td>
    <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      <br />Nam dolor odio, lacinia in ...
      <br>Pellentesque ac enim vitae lorem aliquet pretium.</td>
    <td>More Text</td>
    <td>Yet More Text</td>
    <td>Even More Text</td>
  </tr>
  <tr>
    <td>
      <img src="http://placehold.it/50x70" />
    </td>
    <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      <br />Nam dolor odio, lacinia in ...
      <br>Pellentesque ac enim vitae lorem aliquet pretium.</td>
    <td>More Text</td>
    <td>Yet More Text</td>
    <td>Even More Text</td>
  </tr>

  </tbody>
</table>

关于html - 在表格单元格 html 中垂直和水平居中所有图像的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28016292/

相关文章:

html - 在文本框下对齐说明div标签

html - IE8 html图像对齐

html - html和css中的图像间距问题

html - 使用 CSS 的不完整圆圈,中间有分隔符和文本

html - 在绝对位置下方放置内容?

css - tr 之间的空间,以 tr 为连续 block

javascript - 在使用 css 转换问题之前用 jquery 定义 css 属性

javascript - 如何向水平滚动条添加或删除类?

html - 创建可伸缩的 flex child

html - 如何在单个母 block 中为不同的 block 定义不同的 "text-align"?