Home > Web Front-end > CSS Tutorial > How to Center Align HTML Table Contents?

How to Center Align HTML Table Contents?

Mary-Kate Olsen
Release: 2024-11-02 18:24:02
Original
962 people have browsed it

How to Center Align HTML Table Contents?

Center Align HTML Table Contents

When working with HTML tables, it may be necessary to align the contents of cells vertically and horizontally for a more visually appealing result. Here's how to achieve this:

Centering Text Horizontally and Vertically

  • Inline Style Attributes: Use the text-align property to center the text horizontally, and vertical-align to align it vertically. For example:
<code class="html"><td style="text-align: center; vertical-align: middle;">Text</td></code>
Copy after login
  • CSS Rules: Define CSS rules to center the text in all table cells:
<code class="css">td {
    text-align: center;
    vertical-align: middle;
}</code>
Copy after login

Below is an example demonstrating both approaches:

<code class="html"><table>
<tr>
    <td style="text-align: center; vertical-align: middle;">Centered Cell</td>
    <td style="text-align: center; vertical-align: middle;">Centered Cell</td>
</tr>
</table>

<table border="1" id="cssTable">
<tr>
    <td>Centered Cell</td>
    <td>Centered Cell</td>
</tr>
</table></code>
Copy after login
<code class="css">#cssTable td {
    text-align: center;
    vertical-align: middle;
}</code>
Copy after login

These techniques allow you to easily center the contents of your HTML tables, resulting in a more user-friendly and visually appealing interface.

The above is the detailed content of How to Center Align HTML Table Contents?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template