
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to center align text in table cells in HTML?
We use the CSS property text-align, to center align text in table cells. We use inline, internal style sheet for text alignment in table cells.
The text-align property of CSS sets the alignment of the content in <th> and <td>.
By default, the content of <th> are center-aligned and the content of <td> are left-aligned. We can override the attribute by other to change the alignment.
We can also change the align-value to left and right.
Syntax
Following is the syntax to center align text in tag of the table.
<th style="text-align: center">Table header...</th>
Following is the syntax to center align text in <td> tag of the table.
<td style="text-align: center">Table header...</td>
Example 1
Given below is the example to center align text in table cells in HTML.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> table,tr,th,td { border:1px solid black; } </style> </head> <body> <table style="width: 50%"> <caption style="text-align: center; ">Employees</caption> <tr> <th style="text-align: center">EmployeeName</th> <th style="text-align: center">EmployeeId</th> </tr> <tr> <td style="text-align: center">Yadav</td> <td style="text-align: center">022</td> </tr> <tr> <td style="text-align: center">Abdul</td> <td style="text-align: center">025</td> </tr> <tr> <td style="text-align: center">Jason</td> <td style="text-align: center">208</td> </tr> </table> </body> </html>
Following is the output for the above example program.
Example 2
Let us see another example for this −
<!DOCTYPE html> <html> <head> <style> table, td, th { border: 1px solid black; width: 300px; } </style> </head> <body> <h1>Authors</h1> <table> <tr> <th>Alchemist</th> <th>Rich Dad Poor Dad</th> </tr> <tr> <td style="text-align:center">Paulo Coelho</td> <td style="text-align:center">Robert Kiyosaki</td> </tr> </table> </body> </html>
- Related Articles
- How to center text in HTML?
- How to make a div center align in HTML?
- How to center text across multiple cells in Excel?
- Center image using text-align center with CSS?
- How to align text content into center using JavaScript?
- How to merge table cells in HTML?
- How to Convert HTML to Text in Cells in Excel?
- Center one and right/left align other flexbox element in HTML
- How to center the contents of an HTML table?
- How to align block elements to the center?
- Align items to center not working in SAPUI5
- How to center align the items of a JComboBox in Java?
- How to center align component using BoxLayout with Java?
- How to align flexbox container into center using JavaScript?
- How to Align Navbar Items to Center using Bootstrap 4?

Advertisements