HTML - Tables

HTML table tag is used to display data in tabular form (row * column). There can be many columns in a row.

The HTML tables are created using the <table> tag in which the <tr> tag is used to create table rows and <td> tag is used to create data cells. The elements under <td> are regular and left aligned by default.

Example :-

Result :-
Row 1, Column 1 Row 1, Column 2
Row 2, Column 1 Row 2, Column 2

Table Heading

Table heading can be defined using <th> tag. This tag will be put to replace <td> tag, which is used to represent actual data cell. Normally you will put your top row as table heading as shown below, otherwise you can use <th> element in any row. Headings, which are defined in <th> tag are centered and bold by default.

Example :-

Result :-
HTML Table Header
Name Salary
Smith 5000
John 7000

Cellpadding and Cellspacing Attributes

There are two attributes called cellpadding and cellspacing which you will use to adjust the white space in your table cells. The cellspacing attribute defines space between table cells, while cellpadding represents the distance between cell borders and the content within a cell.

Example :-

Result :-
HTML Table Cellpadding
Name Salary
Ramesh Raman 5000
Shabbir Hussein 7000

Colspan And Rowspan Attributes

You will use colspan attribute if you want to merge two or more columns into a single column. Similar way you will use rowspan if you want to merge two or more rows.

Example :-

Result :-
HTML Table Colspan/Rowspan
Column 1 Column 2 Column 3
Row 1 Cell 1 Row 1 Cell 2 Row 1 Cell 3
Row 2 Cell 2 Row 2 Cell 3
Row 3 Cell 1

Table Height and Width

You can set a table width and height using width and height attributes. You can specify table width or height in terms of pixels or in terms of percentage of available screen area.You can set a table width and height using width and height attributes. You can specify table width or height in terms of pixels or in terms of percentage of available screen area.

Example :-

Result :-
HTML Table Width/Height
Row 1, Column 1 Row 1, Column 2
Row 2, Column 1 Row 2, Column 2

Table Header,Body, And Footer

Tables can be divided into three portions - a header, a body, and a foot. The head and foot are rather similar to headers and footers in a word-processed document that remain the same for every page, while the body is the main content holder of the table.

  • <thead>   to create a separate table header.
  • <tbody>   to indicate the main body of the table.
  • <tfoot>   to create a separate table footer.
  • Example :-

    Result :-
    HTML Table
    This is the head of the table
    This is the foot of the table
    Cell 1 Cell 2 Cell 3 Cell 4