How to set fixed width for td in a table ?


HTML tables are a crucial element of web development. They are used to organize and display data in a structured format. The HTML tables allow web developers to arrange data into rows and columns of cells.

HTML tables are created using the <table> tag which consists of several components such as <tr>, <td>, and <th>. Each of these components has its own unique properties.

Understanding the <td> Element in HTML Tables

The <td> tag element defines a data cell in an HTML table. It is used to display data in the table, such as text, images, or links. Each <td> element is enclosed within a <tr> element, which represents the row in the table.

Syntax

Following is the syntax to set <td> width in HTML.

<td style="width: 20px; >content</td>

This code will set the width of a <td> element to 20px.

The Importance of Setting Fixed Width for <td> Elements

It is important that the table looks consistent and easy to read when it displays on the web page. We can easily achieve this by setting a fixed width for the <td> elements in the table. With this, we ensure that each column in the table is the same width, which makes it easier for users to find the information.

Different Methods to Set Fixed Width for <td> Elements

Here are a few common methods to set fixed width for <td> elements in HTML tables.

1. Using the Width Attribute

The HTML style element contains a width attribute. To set cell width we can place these types of attributes with specified values with pixels inside the <td> tag. For example −

<td width="100px">Data</td>

Example 1

Here is an example to set the width of <td> tag using the width attribute.

<!DOCTYPE html>
<html>
<body>
   <h3>Set the width of <td> element using the width attribute </h2>
   <table border = "1px">
      <tr>
         <td width="100px">Content 1</td>
         <td width="150px">Content 2</td>
      </tr>
      <tr>
         <td>Content 3</td>
         <td>Content 4</td>
      </tr>
      <tr>
         <td>Content 5</td>
         <td>Content 6</td>
      </tr>
   </table>
</body>
</html>

2. Using CSS

By using CSS, we can set the fixed width for <td> element. For doing this, we can use the width property. For example −

td {
   width: 100px;
}

This code will set the width of a <td> element to 100px;

Example 2

Here is an example to set the width of <td> tag using CSS.

<!DOCTYPE html>
<html>
<head>
   <style>
      td {
         width: 100px;
      }
   </style>
</head>
<body>
   <h3>Set the width of <td> element using CSS</h3>
   <table border = "1px">
      <tr>
         <td>Content 1</td>
         <td>Content 2</td>
      </tr>
      <tr>
         <td>Content 3</td>
         <td>Content 4</td>
      </tr>
      <tr>
         <td>Content 5</td>
         <td>Content 6</td>
      </tr>
   </table>
</body>
</html>

3. Using the table-layout Property

We can also set fixed width for <td> elements by using the table-layout property. By setting the table-layout property to fixed, we can ensure that each <td> element in the table has the same width. For example −

table {
   table-layout: fixed;
}
td {
   width: 150px;
}

This code will set the width of a <td> element to 150px;

Example 3

Here is an example to set the width of <td> tag using the table-layout Property.

<!DOCTYPE html>
<html>
<head>
   <style>
      h1, h3 {
         text-align: center;
      }
      table {
         width: 100%;
         table-layout: fixed;
         border-collapse: collapse;
      }
            
      th, td {
         padding: 5px;
         text-align: left;
         border: 1px solid;
      }
            
      td {
         width: 150px;
      }
   </style>
</head>
<body>
   <h3>Fixed <td> Width using the table-layout Property</h3>
   <table>
      <thead>
         <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Gender</th>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td>Ram Kumar</td>
            <td>35</td>
            <td>Male</td>
         </tr>
         <tr>
            <td>Kavita</td>
            <td>28</td>
            <td>Female</td>
         </tr>
         <tr>
            <td>Bob Johnson</td>
            <td>42</td>
            <td>Male</td>
         </tr>
      </tbody>
   </table>
</body>
</html>

Conclusion

In this article, we have discussed several ways to set fixed width, such as width attribute, CSS, and the table-layout property. Setting fixed width for <td> elements in HTML tables is important to ensure that the table looks consistent and is easy to read.

Updated on: 13-Apr-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements