HTML table tag

What is the HTML <table></table> element used for?

Tables provide a better structure to show data in organize way. Through tables you can define structure of your webpage. And through tables we can show data in grid format in webpages. The <table> tag in HTML is used to create and display data in a tabular format, consisting of rows and columns. It helps organize information in a structured way, making it easier for users to read and understand. Tables consist with rows and columns. In table cells you can include text, images, links, lists and even another table too. In HTML a table starts with <table> and ends with </table>. Within table tag, for creating rows you can use TR tag. Rows start with <tr> and end with </tr> tag. Within TR tag, we write <th> tag for table's heading and we can write <td> tag for table data. Example


<table>
  <tr>
    <th>row1,col1</th>
    <th> row1,col2</th>
    <th> row1,col3</th>
  </tr>
  <tr>
    <td> row2,col1</td>
    <td> row2,col2</td>
    <td> row2,col3</td>
  </tr>
  <tr>
    <td> row3,col1</td>
    <td> row3,col2</td>
    <td> row3,col3</td>
  </tr> 
</table>
Output

row1,col1 row1,col2 row1,col3
row2,col1 row2,col2 row2,col3
row3,col1 row3,col2 row3,col3
Table tag's attribute
Attribute Description
Align We can specify the horizontal alignment of a table to control its position on a webpage. Values are left, right and center. If you do not set align attribute of table then default alignment of remain is left. Example is mentioned below:
Align="left" Or Align="center" Or Align="right"
Bleedleft Set alignment at the left window border.
Bleedright Set alignment at the right window border.
Border We can define the thickness of a table's border using the border attribute. If you do not specify border then table appear without border. Example is mentioned below:
Border="2"
Cellpadding You can set callpadding in pixels. Cellpadding provides space between cells of table. Example is mentioned below:
Cellpadding="2"
Cellspacing You can set Cellspacing in pixels. Cellspacing provides space around the data in a cell. Example is mentioned below:
Cellspacing="2"
Bgcolor The bgcolor attribute allows us to apply a background color to an HTML table. Example is mentioned below:
bgrolor="blue"
Width You can set width of table. You can set width either in pixel or in percentage. Example is mentioned below:
width="200" or width="100%"
Height You can set height of table. You can set height either in pixel or in percentage. Example is mentioned below:
height="200" or height="100%"
Table element example with attributes

<table border="2" cellspacing="5" cellpadding="5" height="100" 
width="300" bgcolor="orange" align="center">
  <tr>
    <th>row1,col1</th>
    <th> row1,col2</th>
    <th> row1,col3</th>
  </tr>
  <tr>
    <td> row2,col1</td>
    <td> row2,col2</td>
    <td> row2,col3</td>
  </tr>
  <tr>
    <td> row3,col1</td>
    <td> row3,col2</td>
    <td> row3,col3</td>
  </tr> 
</table>
Output

row1,col1 row1,col2 row1,col3
row2,col1 row2,col2 row2,col3
row3,col1 row3,col2 row3,col3
TR tag <tr></tr> Tr tag describes table row. It works like a container to table cells we define <td> or <th> within <tr></tr> tag. Example of <tr></tr> tag:

<tr>
    <th>content here</th>
    <th>content here</th>
  </tr>
  <tr>
    <td>content here</td>
    <td>content here</td>
  </tr>
TR tag's attributes
Attribute Description
Valign We can set vertical alignment of the table row with valign attribute. Values are top, middle and bottom. If you do not set valign attribute of table then default alignment of remain top. Example is mentioned below:
Valign="top" Or Valign="middle" Or Valign="bottom"
Bgcolor The bgcolor attribute allows us to apply a specific background color to any row in an HTML table. Example is mentioned below:
bgrolor="blue"
Width You can set width of table row. You can set width either in pixel or in percentage. Example is mentioned below:
width="200" or width="100%"
Height You can set height of table row. You can set height either in pixel or in percentage. Example is mentioned below:
height="200" or height="100%"
Td and TH tags The <th> tag represents a table header and is used to define heading cells in a table. The <td> tag represents table data and is used to insert content into individual table cells. Example

  <th>content here</th>
  <th>content here</th>
  <td>content here</td>
  <td>content here</td>
TD and TH tag's attributes
Attribute Description
Align The align attribute is used to control the horizontal positioning of content within an HTML element. If you do not set align attribute then default alignment remain left. Example is mentioned below:
Align="left" Or Align="center" Or Align="right"
Bgcolor The bgcolor attribute allows us to apply a specific background color to any row in an HTML table. Example is mentioned below:
bgrolor="blue"
Width You can set width with width attribute. You can set width either in pixel or in percentage. Example is mentioned below:
width="200" or width="100%"
Height You can set height with height attribute. You can set height either in pixel or in percentage. Example is mentioned below:
height="200" or height="100%"
Colspan Through this attribute you can merge more then one columns into one column. Example is mentioned below:
colspan="2"
Rowspan Through this attribute you can merge more then one rows into one rows. Example is mentioned below:
rowspan="2"
Nowrap Norwap attribute prevent the browser from wrapping the contents of a table cells.

FAQ 1. Which attribute is used to set the background color of a table? The bgcolor attribute is used to specify the background color of an HTML table. 2. How can we align a table horizontally on a webpage? The align attribute can be used to position a table to the left, right, or center. 3. What is the purpose of the cellpadding attribute? The cellpadding attribute specifies the space between the content of a table cell and its border.