如何使标题在表格单元格中居中

发布于 2024-12-26 03:41:11 字数 1188 浏览 3 评论 0原文

如何居中

我想将文本“信息”居中。尝试了align =“center”但没有工作

我的代码正确吗?

<table border="1" width="100%" cellspacing="10">
<tr>
    <th>column1</th>
    <th>column2</th>
    <th bgcolor="#4F81BD"><strong><font size="5" color="white" face="calibri" align="center">Information</font><strong></th>
  </tr>
</table>

[编辑] 这是我根据您的回答构建的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Title</title>

<style type="text/css">
.centerText{
       text-align: center;}
</style>

</head>
<table border="0" width="100%" cellspacing="10">
<tr>
    <th></th>
    <th></th>
    <th class="centerText" bgcolor="#4F81BD"><strong><font size="5" color="white" face="calibri" >Information</font><strong></th>
  </tr>
    </table>
    </body>
    </html>

这是处理这个问题的正确方法吗?

how to center the

I would like to center the text "Information". Tried the align="center" but didn't work

Is my code correct?

<table border="1" width="100%" cellspacing="10">
<tr>
    <th>column1</th>
    <th>column2</th>
    <th bgcolor="#4F81BD"><strong><font size="5" color="white" face="calibri" align="center">Information</font><strong></th>
  </tr>
</table>

[EDIT] Here is the code I have built thanks to your answsers.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Title</title>

<style type="text/css">
.centerText{
       text-align: center;}
</style>

</head>
<table border="0" width="100%" cellspacing="10">
<tr>
    <th></th>
    <th></th>
    <th class="centerText" bgcolor="#4F81BD"><strong><font size="5" color="white" face="calibri" >Information</font><strong></th>
  </tr>
    </table>
    </body>
    </html>

Is this the right way to handle this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

睫毛上残留的泪 2025-01-02 03:41:11

在您的代码中,您将 align="center" 放入 strong 内的 font 标记。所有这些元素都是内联的,并不适合所有单元格宽度。要将其居中对齐,您需要将此类属性放置到 th 标记中,或将 stringfont 标记的显示更改为 display :block 以适应所有宽度。

最简单的方法是在 CSS 中:

 table tr th { text-align:center; }

PS 更好的方法是将所有内联样式移动到 CSS 文件中。

In your code you putting align="center" to the font tag inside of the strong. All of these elements are inline and doesn't fits all the cell width. To align it on center you need to put such attribute to the th tag or change displaying of the string and font tags to display:block to fit all the width.

The simplest way to do this is in CSS:

 table tr th { text-align:center; }

P.S. Better way is to move all of your inline style to CSS files.

只有一腔孤勇 2025-01-02 03:41:11

使用 text-align:center;

请参阅jsfiddle 上的实时示例

[编辑] 请注意,您应该使用其他用户指出的类或元素在 CSS 中实现这些样式

Use text-align:center;

see this live example on jsfiddle

[EDIT] Note that you should implement these styles within your CSS with classes or elements as pointed out by fellow users

_畞蕅 2025-01-02 03:41:11

尝试使用CSS。

table th {text-align:center;}

Try using CSS.

table th {text-align:center;}
无法回应 2025-01-02 03:41:11
  1. 标签已弃用 它的使用被认为是一个非常糟糕的主意。请改用 CSS。
  2. 仅使用 标记来使文本变为粗体也不是特别有用,尤其是在 元素内。 已经表明文本很重要,因为它是表格列的标题。在许多浏览器中,文本已在 中变为粗体
  3. 用于居中文本的 CSS 样式为:text-align: center
  1. The <font> tag is deprecated and it's use is considered a very bad idea. Use CSS instead.
  2. Using the <strong> tag just to get the text to be bold is also not particularly useful, especially inside a <th> element. The <th> already indicates that the text matters because it's the header of a table column. In many browsers, the text is already made bold in <th>s
  3. The CSS style used to center the text is: text-align: center
掩耳倾听 2025-01-02 03:41:11

尝试这个

<th bgcolor="#4F81BD" align="center"><strong><font size="5" color="white" face="calibri" >Information</font><strong></th>

Trying this

<th bgcolor="#4F81BD" align="center"><strong><font size="5" color="white" face="calibri" >Information</font><strong></th>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文