How to Center Text in HTML Table Cells?

DDD
Release: 2024-10-31 15:31:10
Original
620 people have browsed it

How to Center Text in HTML Table Cells?

How to Center Contents in an HTML Table Horizontally and Vertically

Centering text horizontally and vertically within the cells of an HTML table can enhance the visual appeal and readability of the page. To accomplish this, there are two methods to consider: CSS and inline style attributes.

CSS Method:

  1. Define a style block in the section of the HTML document.
  2. Add the following CSS:
<code class="css">td {
    text-align: center;
    vertical-align: middle;
}</code>
Copy after login

Inline Style Attribute Method:

  1. Use the style attribute directly within the tags.
  2. Set the text-align and vertical-align properties to "center" for each table cell.

Here is an example that demonstrates both methods:

<code class="html"><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></code>
Copy after login

In the above example, both tables will display with centered text content in all cells. The CSS method can be beneficial if you want to apply the alignment to multiple tables or reuse the stylesheet across different pages. On the other hand, inline style attributes allow for more specific control over individual cells.

The above is the detailed content of How to Center Text in HTML Table Cells?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template