How to fix the width of columns in the table ?
We have already seen the article, How to fix the height of rows in the table.
The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.
<td width ="px | %">
- The width attribute is used to set the width of a column. It is added in the <td> tag.
- The width should be specified according to the content, either in pixels or percentage.
- If the content in the column is large, it will overflow.
Example 1: In this example, the width of the first column is fixed, but the width of the other columns are not fixed. So, when we change the size of the screen, the width of the first column remains the same, but the width of the other column changes according to the content. However, if the specified width is not sufficiently large according to the content, this may not work, and the first column will overflow and behave similarly to the other column.
Output: Run the following code and change the screen-size to see the difference between the columns.
- Before changing the screen-size:
- After changing the screen-size:
Example 2: In this example, <td width =”%”> is used.
Output: The width of the column is set to “50%” , so even after changing the screen size, the width of the first column remains “50%”.
- Before changing screen-size:
- After changing screen-size: