HTML HTML Tutorial HTML Forms HTML Graphics HTML Media HTML APIs HTML Tags



HTML Tag: td

The HTML tag td is used to define a cell in an HTML table. It is one of the most commonly used tags in HTML and is essential for creating tables on web pages. In this article, we will discuss the td tag in detail, including its syntax, attributes, and examples.

Syntax

The td tag is an empty tag, which means it does not require a closing tag. Its syntax is as follows:

<td> content </td>

The content inside the td tag can be any HTML element, such as text, images, links, or other HTML tags.

Attributes

The td tag has several attributes that can be used to modify its behavior and appearance. The most commonly used attributes are:

  • colspan: Specifies the number of columns a cell should span.
  • rowspan: Specifies the number of rows a cell should span.
  • align: Specifies the horizontal alignment of the content within the cell.
  • valign: Specifies the vertical alignment of the content within the cell.
  • width: Specifies the width of the cell.
  • height: Specifies the height of the cell.
  • bgcolor: Specifies the background color of the cell.
  • border: Specifies the border width of the cell.

Examples

Let's take a look at some examples of how the td tag can be used in HTML tables:

Example 1: Basic Table

In this example, we will create a basic HTML table with two rows and two columns:

<table>
  <tr>
    <td>Row 1, Column 1</td>
    <td>Row 1, Column 2</td>
  </tr>
  <tr>
    <td>Row 2, Column 1</td>
    <td>Row 2, Column 2</td>
  </tr>
</table>
Row 1, Column 1 Row 1, Column 2
Row 2, Column 1 Row 2, Column 2

Example 2: Spanning Rows and Columns

In this example, we will create a table with a cell that spans two rows and two columns:

<table>
  <tr>
    <td>Row 1, Column 1</td>
    <td colspan="2">Row 1, Column 2 and 3</td>
  </tr>
  <tr>
    <td>Row 2, Column 1</td>
    <td>Row 2, Column 2</td>
    <td>Row 2, Column 3</td>
  </tr>
</table>
Row 1, Column 1 Row 1, Column 2 and 3
Row 2, Column 1 Row 2, Column 2 Row 2, Column 3

Example 3: Cell Alignment and Styling

In this example, we will create a table with cells that are aligned and styled:

<table>
  <tr>
    <td align="center" width="100" height="50" bgcolor="#ff0000" border="1">Cell 1</td>
    <td align="right" width="50" height="100" bgcolor="#00ff00" border="1">Cell 2</td>
  </tr>
  <tr>
    <td align="left" width="75" height="75" bgcolor="#0000ff" border="1">Cell 3</td>
    <td align="center" width="125" height="125" bgcolor="#ffff00" border="1">Cell 4</td>
  </tr>
</table>
Cell 1 Cell 2
Cell 3 Cell 4

Conclusion

The td tag is an essential HTML tag for creating tables on web pages. It allows you to define cells and customize their behavior and appearance using various attributes. By using the examples provided in this article, you can create tables that are both functional and visually appealing.

References

Activity