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.

Syntax:

<td width ="px | %">

Approach:

  • 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.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        table {
            margin-left: auto;
            margin-right: auto;
            font-size: 20px;
            height: 100%;
            table-layout: fixed;
        }
  
        td {
            border: 1px solid black;
            text-align: center;
            padding: 10px;
        }
  
        tr:nth-child(even) {
            background-color: #00cf45;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1>GeeksforGeeks</h1>
  
        <h2>
            To fix the width of 
            columns in the table
        </h2>
  
        <table>
            <tr style="color:white; 
                background-color:black;">
                <td width="100px">Col1</td>
                <td>Col2</td>
                <td>Col3</td>
            </tr>
            <tr>
                <td>
                    Width of this column remains 
                    same on varying screen-size
                </td>
                <td>
                    Width of this column changes 
                    on varying screen-size
                </td>
                <td>
                    Width of this column also changes 
                    on varying screen-size
                </td>
            </tr>
            <tr>
                <td>Geek1</td>
                <td>Geek2</td>
                <td>Geek3</td>
            </tr>
            <tr>
                <td>Geek4</td>
                <td>Geek5</td>
                <td>Geek6</td>
            </tr>
            <tr>
                <td>Geek7</td>
                <td>Geek8</td>
                <td>Geek9</td>
            </tr>
        </table>
    </center>
</body>
  
</html

chevron_right


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.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        table {
            margin-left: auto;
            margin-right: auto;
            font-size: 20px;
            height: 100%;
            table-layout: fixed;
        }
  
        td {
            border: 1px solid black;
            text-align: center;
            padding: 10px;
        }
  
        tr:nth-child(even) {
            background-color: #00cf45;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1>GeeksforGeeks</h1>
  
        <h2>
            To fix the width of
            columns in the table
        </h2>
  
        <table>
            <tr style="color:white; 
                background-color:black;">
                <td width="50%">Col1</td>
                <td>Col2</td>
                <td>Col3</td>
            </tr>
            <tr>
                <td>
                    Width of this column remains 
                    same on varying screen-size
                </td>
                <td>
                    Width of this column changes 
                    on varying screen-size
                </td>
                <td>
                    Width of this column also changes 
                    on varying screen-size
                </td>
            </tr>
            <tr>
                <td>Geek1</td>
                <td>Geek2</td>
                <td>Geek3</td>
            </tr>
            <tr>
                <td>Geek4</td>
                <td>Geek5</td>
                <td>Geek6</td>
            </tr>
            <tr>
                <td>Geek7</td>
                <td>Geek8</td>
                <td>Geek9</td>
            </tr>
        </table>
    </center>
</body>
  
</html>

chevron_right


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:

full-stack-img




My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.