Account
Categories

HTML Table Attributes


Definition:

HTML Table Attributes are used inside the <table> tag to control the appearance, spacing, width, and styling of tables.

There are some listed table attributes as follows:

1. border
2. cellpadding
3. cellspacing
4. width
5. style

Syntax:

<table 
    border="1"
    cellpadding="5"
    cellspacing="0"
    width="70%"
    style="margin:auto;">

    Table Content

</table>

border → It is used in the <table> tag to add a border around the table and its cells. It creates a visible border for the table.

Example:

<table border="2">
  <tr>
    <th>Column 1</th>
    <th>Column 2</th>
  </tr>

  <tr>
    <td>A</td>
    <td>B</td>
  </tr>

  <tr>
    <td>C</td>
    <td>D</td>
  </tr>
</table>
Column 1 Column 2
A B
C D

cellpadding → It is used in the <table> tag to create space between the cell content and its border. It makes the cell content look more spaced and readable.

Example:

<table border="2" cellpadding="15">
  <tr>
    <th>Column 1</th>
    <th>Column 2</th>
  </tr>
  <tr>
    <td>A</td>
    <td>B</td>
  </tr>
  <tr>
    <td>C</td>
    <td>D</td>
  </tr>
</table>
Column 1 Column 2
A B
C D

cellspacing → It is used in the <table> tag to add space between table cells. It creates gaps between each cell.

Example:

<table border="2" cellspacing="10">
  <tr>
    <th>Column 1</th>
    <th>Column 2</th>
  </tr>
  <tr>
    <td>A</td>
    <td>B</td>
  </tr>
  <tr>
    <td>C</td>
    <td>D</td>
  </tr>
</table>
Column 1 Column 2
A B
C D

width → If you set the "width" attribute in the <table> tag, it sets the width of a table.

Example:

<table border="2" width="80%">
  <tr>
    <th>Column 1</th>
    <th>Column 2</th>
  </tr>
  <tr>
    <td>A</td>
    <td>B</td>
  </tr>
  <tr>
    <td>C</td>
    <td>D</td>
  </tr>
</table>
Column 1 Column 2
A B
C D

style → It is used to add CSS styling to a table, such as alignment (left, right, center), colors, and design.

Example:

<table border="2" style="width:200px; text-align:center;">
  <tr>
    <th>Column 1</th>
    <th>Column 2</th>
  
  <tr>
    <td>A</td>
    <td>B</td>
  
  <tr>
    <td>C</td>
    <td>D</td>
  
</table>
Column 1 Column 2
A B
C D