
- 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 set horizontal header for a table?
Tables in HTML can have horizontal header and vertical header. For the horizontal header, we need to set all <th> inside a single <tr> tag.
Syntax
Following is the syntax to set horizontal header for a table −
<tr> <th> horizontal header1… </th> <th> horizontal header2… </th> </tr>
Example 1
Now let us see an example program to set horizontal header for a table.
<!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; padding: 20px; } </style> </head> <body> <h2>Tables in HTML</h2> <table style="width: 100%"> <tr> <th >First Name </th> <th>Job role</th> </tr> <tr> <td ></td> <td ></td> </tr> <tr> <td ></td> <td ></td> </tr> <tr> <td></td> <td></td> </tr> </table> </body> </html>
On executing the above program, it will display a table with horizontal headers First Name and Job role.
Example 2
Now, in this second example we are trying to create another example with 3 rows with horizontal headers “Name”, “Age” and, “Technology” −
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; width: 100px; height: 50px; } </style> </head> <body> <h1>Employee Details</h1> <table> <tr> <th>Name</th> <th>Age</th> <th>Technology</th> </tr> <tr> <td>Amit</td> <td>27</td> <td>Database</td> </tr> <tr> <td>Sachin</td> <td>34</td> <td>Marketing</td> </tr> </table> </body> </html>
Following is the output for the above example program.
Example 3
We can also set vertical header for a table. Given below is an example to create vertical header for a table.
<!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; padding: 20px; } </style> </head> <body> <table style="width: 100%"> <tr> <th>First Name </th> <td></td> <td></td> </tr> <tr> <th>Last Name</th> <td ></td> <td></td> </tr> <tr> <th>Job role</th> <td ></td> <td></td> </tr> </table> </body> </html>
Following is the output for the above example program.
- Related Articles
- How to set Row Header View for JScrollPane in Java?
- In HTML how to create table header?
- How to create table header in HTML?
- How to change header background color of a table in Java
- How to set horizontal skew of a canvas circle using Fabric.js?
- How to add header item for Listview in Android?
- How to set horizontal gap between elements in a GridLayout with Java?
- How to set the horizontal scale factor of a Circle using FabricJS?
- How to set the horizontal scale factor of a Triangle using FabricJS?
- How to set the horizontal scale factor of a Rectangle using FabricJS?
- How to set the horizontal alignment of text with JavaScript?
- How to serialize a cookie name-value pair into a Set Cookie header string in JavaScript?
- MySQL query to set different combinations for values in a table?
- How can we show/hide the table header of a JTable in Java?
- How to set the horizontal alignment of an element with JavaScript?

Advertisements