Html 如何在html表格行中居中对齐文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8986260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:44:24  来源:igfitidea点击:

How to align center the text in html table row?

htmlcss

提问by Rajagopal ?

I am using an HTML <table>and I want to align the text of <td>to the center in each cell.

我正在使用 HTML <table>,我想将 的文本与<td>每个单元格的中心对齐。

How do I center align the text horizontally and vertically?

如何水平和垂直居中对齐文本?

回答by David Laberge

Here is an example with CSS and inline styleattributes:

这是一个带有 CSS 和内联style属性的示例:

td 
{
    height: 50px; 
    width: 50px;
}

#cssTable td 
{
    text-align: center; 
    vertical-align: middle;
}
<table border="1">
    <tr>
        <td style="text-align: center; vertical-align: middle;">Text</td>
        <td style="text-align: center; vertical-align: middle;">Text</td>
    </tr>
</table>

<table border="1" id="cssTable">
    <tr>
        <td>Text</td>
        <td>Text</td>
    </tr>
</table>

http://jsfiddle.net/j2h3xo9k/

http://jsfiddle.net/j2h3xo9k/

EDIT: The valignattribute is deprecated in HTML5 and should not be used.

编辑:该valign属性在 HTML5 中已弃用,不应使用。

回答by Carsten

The CSS to center text in your tdelements is

使td元素中的文本居中的 CSS是

td {
  text-align: center;
  vertical-align: middle;
}

回答by Boris

Try to put this in your CSS file.

试着把它放在你的 CSS 文件中。

td {
    text-align: center;
    vertical-align: middle;
}

回答by Hayden Thring

long hand inline example:

长手内联示例:

<td style='text-align:center;vertical-align:middle'></td> 

shorthand css example:

速记 css 示例:

td{
 text-align:center;
 vertical-align:middle;
}

回答by Remi

<td align="center" valign="center">textgoeshere</td>

<td align="center" valign="center">textgoeshere</td>

Is the only correct answer imho, since your working with tables which is old functionality most common used for e-mail formatting. So your best bet is to not use just style but inline style and known table tags.

恕我直言,这是唯一正确的答案,因为您使用的表格是最常用于电子邮件格式的旧功能。所以最好的办法是不要只使用样式,而是使用内联样式和已知的表格标签。

回答by Fernando

Selector > child:

选择器 > 子项:

.text-center-row>th,
.text-center-row>td {
  text-align: center;
}
<table border="1" width='500px'>
  <tr class="text-center-row">
    <th>Text</th>
    <th>Text</th>
    <th>Text</th>
    <th>Text</th>
  </tr>
  <tr>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
  </tr>
  <tr class="text-center-row">
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
    <td>Text</td>
  </tr>
</table>

回答by Balaswamy Vaddeman

<td align="center"valign="center">textgoeshere</td>

<td align="center"valign="center">textgoeshere</td>

more on valign

更多关于 valign