- HTML Tutorial
- HTML - Home
- HTML - Introduction
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - <th> Tag
HTML <th> tag designates the header cell for HTML tables. It needs to be used as a child element of <tr>, which is itself stated in the <table> tag.
The <td> tag is utilised to define a standard data cell. Although the browser presents the header cell as bold and centred by default, you can modify this behaviour using CSS properties. The <th> tag can contain any HTML element that can be used in the HTML document's body including text, images, forms, links, and more. Depending on the size of its contents, the table's size is automatically modified.
Syntax
<th> .... </th>
Attribute
HTML th tag supports Global and Event attributes of HTML. Accepted some specific attributes as well which are listed bellow.
Attribute | Value | Description |
---|---|---|
abbr | text | Specifies an abbreviated of the content in a header cell |
colspan | number | Specifies the number of columns. |
headers | header_id | Specifies one or more header cells a cell is related to. |
rowspan | number | Specifies the number of rows a cell should take. |
scope | col colgroup row rowgroup |
Specifies whether a header cell is a header for a column, row, or group of columns or rows. |
Examples of HTML th Tag
Bellow examples will illustrate the usage of th tag. Where, when and how to use th tag.
Creating Two Header Cells
Let's consider the following example, where we are going to create the table using the two header cells.
<!DOCTYPE html> <html> <style> table, th, td { border: 2px solid #D2B4DE; } </style> <body> <table> <tr> <th>Name</th> <th>Age</th> </tr> <tr> <td>Maya</td> <td>23</td> </tr> <tr> <td>Ravi</td> <td>22</td> </tr> <tr> <td>Ram</td> <td>21</td> </tr> </table> </body> </html>
Adding Background Color on Header Cells
Considering another scenario where we are going to apply the background-color to the header cells.
<!DOCTYPE html> <html> <style> table, th, td { border: 2px solid #5B2C6F; } </style> <body> <table> <tr> <th style="background-color:#D6EAF8 ">Students</th> <th style="background-color:#D5F5E3">Marks</th> </tr> <tr> <td>Akhil</td> <td>95</td> </tr> <tr> <td>Ravi</td> <td>96</td> </tr> <tr> <td>Raju</td> <td>99</td> </tr> </table> </body> </html>
Aligning Header Cell Content
In the following example, we are going to align the <th> to the right using the CSS.
<!DOCTYPE html> <html> <style> table, th, td { border: 1.5px solid #DE3163; } </style> <body> <table style="width:100%"> <tr> <th style="text-align:right">Car</th> <th style="text-align:right">Model</th> </tr> <tr> <td>RS7</td> <td>2023</td> </tr> <tr> <td>AUDI</td> <td>2017</td> </tr> <tr> <td>BENZ</td> <td>2014</td> </tr> </table> </body> </html>
Setting Width of Header Cell
Following is an example where we are going to set the width of the <th> tag using CSS.
<!DOCTYPE html> <html> <style> table, th, td { border: 2px solid #BB8FCE; } </style> <body> <table style="width:50%"> <tr> <th style="width:25%">Name</th> <th style="width:25%">Countrty</th> </tr> <tr> <td>Ram</td> <td>India</td> </tr> <tr> <td>Maya</td> <td>Greece</td> </tr> <tr> <td>Mamboo</td> <td>Nigeria</td> </tr> </table> </body> </html>
Supported Browsers
Tag | |||||
---|---|---|---|---|---|
th | Yes | Yes | Yes | Yes | Yes |