HTML - <td> Tag



In an HTML table, the <td> tag designates a common data cell. It must be used as a child element of <tr>, which designates a table row. The <th> tag is used to define a header cell. The <td> tag may contain text, a form, an image, a table, etc. By default, the content inside it is positioned to the left.

There are exactly the same amount of cells in each row of the table as there are in the longest row. If there are less cells in a row, the browser will automatically fill the row, adding empty cells at the end of it.

Each data cell must be added as a separate <td> element for presenting tabular data in tables.

Syntax

Following is the syntax for HTML <td> −

<table>
   <tr>
      <td>
      ......
      </td>
   </tr>
</table>

Example

Let's consider the following example, where we are going to use the <td> tag.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 2px solid #5DADE2;
      }
   </style>
<body>
   <table>
      <tr>
         <td>1</td>
         <td>2</td>
      </tr>
      <tr>
         <td>3</td>
         <td>4</td>
      </tr>
      <tr>
         <td>5</td>
         <td>6</td>
      </tr>
   </table>
</body>
</html>

On running the above code, the output window will pop up, displaying the table on the webpage.

Example

Considering another scenario where we are going to apply the background-color to the <td> tag.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #A569BD;
      }
   </style>
<body>
   <table>
      <tr>
         <th>Stundent</th>
         <th>Age</th>
      </tr>
      <tr>
         <td style="background-color:#BFC9CA">Ram</td>
         <td style="background-color:#F9E79F ">23</td>
      </tr>
      <tr>
         <td style="background-color:#BFC9CA ">Raju</td>
         <td style="background-color:#F9E79F ">22</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 data that comes under the <td> tag displayed on the webpage.

Example

In the following example, we are going to align the <td> tag to the center using the CSS.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #DE3163;
      }
   </style>
<body>
   <table style="width:100%">
      <tr>
         <th>Cars</th>
         <th>Country</th>
      </tr>
      <tr>
         <td>Toyota</td>
         <td style="text-align:center">Japan</td>
      </tr>
      <tr>
         <td>Nissan</td>
         <td style="text-align:center">France</td>
      </tr>
      <tr>
         <td>Kia</td>
         <td style="text-align:center">South Korea</td>
      </tr>
   </table>
</body>
</html>

On running the above code, the output window will pop up, displaying the data that comes under the <td> tag displayed at the center inside the table.

Example

Following is an example where we are going to adjust the width of a table cell using CSS.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #8E44AD;
      }
   </style>
<body>
   <table style="width:100%">
      <tr>
         <th>Student</th>
         <th>Department</th>
      </tr>
      <tr>
         <td style="width:40%">Ram</td>
         <td style="width:60%">Information Technology</td>
      </tr>
      <tr>
         <td>Rahul</td>
         <td>Voice Process</td>
      </tr>
      <tr>
         <td>Maya</td>
         <td>Non-Voice Process</td>
      </tr>
   </table>
</body>
</html>

When we execute the above code, it will generate an output consisting of the table of different width displayed on the webpage.

html_tags_reference.htm
Advertisements