Home >Web Front-end >CSS Tutorial >How to Center Align Content in HTML Table Cells?
How to Center Align Content in HTML Table
When creating HTML tables, it is often desirable to have the contents of each cell centered both horizontally and vertically. This can enhance the readability and aesthetics of the table.
To horizontally center the text in table cells, you can use CSS with the text-align property. The following example demonstrates this:
<code class="css">td { text-align: center; }</code>
For vertical alignment, the vertical-align property can be used. This code snippet centers the text vertically in the cells:
<code class="css">td { vertical-align: middle; }</code>
You can also use inline style attributes to center the text. This can be more convenient if you only need to apply the alignment to specific cells:
<code class="html"><td style="text-align: center; vertical-align: middle;">Text</td></code>
An example with inline style attributes:
<code class="html"><table> <tr> <td style="text-align: center; vertical-align: middle;">Text</td> <td style="text-align: center; vertical-align: middle;">Text</td> </tr> </table></code>
Using CSS or inline styles, you can easily center align the contents of your HTML table cells, providing a more professional and organized appearance.
The above is the detailed content of How to Center Align Content in HTML Table Cells?. For more information, please follow other related articles on the PHP Chinese website!