表格单元格中的文本居中
我似乎找不到我的问题的答案。我有一个包含两行和两列的表格(如下面所示的代码),如何将特定单元格中的文本居中对齐。我想将一两个单元格中的文本居中对齐 - 不是所有单元格。
<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 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议为此使用 CSS。您应该创建一个 CSS 规则来强制居中,例如:
然后将
ui-helper-center
类添加到您希望控制对齐的表格单元格:编辑:既然这个答案被接受了,我觉得有义务编辑掉评论中引起激烈争论的部分,并且不提倡不良和过时的做法。
请参阅 Gabe 的回答,了解如何将 CSS 规则包含到您的页面中。
I would recommend using CSS for this. You should create a CSS rule to enforce the centering, for example:
And then add the
ui-helper-center
class to the table cells for which you wish to control the alignment: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.
简单一点怎么样(请注意,为类名想出一个更好的名称,这只是一个例子):
示例 这里
您可以将
css
放在单独的文件中,建议这样做。在我的示例中,我创建了一个名为
styles.css
的文件,并将我的css
规则放入其中。然后将其包含在 html 文档的
部分中,如下所示:
另一种方法,不创建单独的 css 文件,根本不推荐...
在 html 文档的
中创建
块。然后把你的规则放在那里。
How about simply (Please note, come up with a better name for the class name this is simply an example):
Example here
You can place the
css
in a separate file, which is recommended.In my example, I created a file called
styles.css
and placed mycss
rules in it.Then include it in the html document in the
<head>
section as follows:The alternative, not creating a seperate css file, not recommended at all...
Create
<style>
block in your<head>
in the html document. Then just place your rules there.