How to fix the width of columns in the table ? - GeeksforGeeks

31 Aug, 2020
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
<!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> 
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
<!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>
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:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
CSS-Misc
HTML-Misc
CSS
HTML
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to create footer to stay at the bottom of a Web page?
Types of CSS (Cascading Style Sheet)
Create a Responsive Navbar using ReactJS
Design a web page using HTML and CSS
How to position a div at the bottom of its container using CSS?
How to set the default value for an HTML <select> element ?
How to set input type date in dd-mm-yyyy format using HTML ?
How to Insert Form Data into Database using PHP ?
Hide or show elements in HTML using display property
REST API (Introduction)