在表格单元中居中文本 [英] Center text in table cell

查看:133
本文介绍了在表格单元中居中文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到我的问题的答案。我有一个两行两列的表格(如下面显示的代码),我如何居中对齐特定单元格中的文本。我想在一个或两个单元格中居中对齐文本 - 不是所有的单元格。

 < div style =text-align:center;> 
< tbody>
< tr>
< td>单元格1< / td>
< td>单元格2< / td>
< / tr>
< tr>
< td>单元格3< / td>
< td>单元格4< / td>
< / tr>
< / tbody>
< / table>
< / div>


解决方案

我会推荐使用CSS。你应该创建一个CSS规则来强制执行定位,例如:

  .ui-helper-center {
text -居中对齐;
}

然后添加 ui-helper-center 类到您希望控制对齐的表格单元中:

 < td class = UI辅助中心>内容< / TD> 






编辑:自这个答案被接受了,我觉得有必要编辑出在评论中引起火焰战争的部分,并且不要宣传贫穷和过时的做法。

Gabe的回答,了解如何将CSS规则包含到您的页面中。


I can't seem to find the answer to my issue. I have a table with two rows and two columns (like the code shown below), how do I center align the text in specific cells. I would like to center align the text in one or two of the cells - not all the cells.

<div style="text-align: center;">
    <table style="margin: 0px auto;" border="0">
        <tbody>
            <tr>
                <td>Cell 1</td>
                <td>Cell 2</td>
            </tr>
            <tr>
                <td>Cell 3</td>
                <td>Cell 4</td>
            </tr>
        </tbody>
    </table>
</div>

解决方案

I would recommend using CSS for this. You should create a CSS rule to enforce the centering, for example:

.ui-helper-center {
    text-align: center;
}

And then add the ui-helper-center class to the table cells for which you wish to control the alignment:

<td class="ui-helper-center">Content</td>


EDIT: Since this answer was accepted, I felt obligated to edit out the parts that caused a flame-war in the comments, and to not promote poor and outdated practices.

See Gabe's answer for how to include the CSS rule into your page.

这篇关于在表格单元中居中文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆