- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- 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 - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- 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 Code Builder
- 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
- 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
In an HTML table, the <th> tag designates the header cell. 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
Following is the syntax for HTML <th> −
<th> text </th>
Example
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>
On running the above code, the output window will pop up, displaying the table applied with two header cells on the webpage.
Example
Considering another scenario where we are going to apply thw 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>
When we execute the above code, it will generate an output consisting of the table along with a background color added to the header cells displayed on the webpage.
Example
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>
On running the above code, the output window will pop up, displaying the data that comes under the <th> tag displayed to the right inside the table.
Example
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>
When we execute the above code, it will generate an output consisting of the table mentioned with a width using CSS applied to the <th> displayed on the webpage.
To Continue Learning Please Login
Login with Google