How to fix the width of columns in the table ? Last Updated : 09 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Fixing the width of columns in a table involves setting a specific width for each column to maintain consistent sizing. This can be achieved using CSS properties like width on <th> or <td> elements, or using the <colgroup> and <col> tags to control column widths.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 percentages.If the content in the column is large, it will overflow.Example 1: In this example, the first column's width is fixed, while others adjust based on screen size. If the fixed width is insufficient, the first column may overflow and behave like the others. 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: Example 2: In this example, we creates a centered table with a fixed-width first column and dynamic-width other columns. The table has alternating row colors, and headers are styled with a different background color. 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%". Comment More infoAdvertise with us S SHUBHAMSINGH10 Follow Improve Article Tags : Web Technologies HTML HTML-Questions Similar Reads How to Create Table in HTML? HTML tables are used for organizing and displaying data in a structured format on web pages. Tables are commonly used for product information, presenting data analytics, or designing a pricing comparison chart. Elements of HTML TableTable ElementsDescription<table>The <table> element def 3 min read How to group the body content in a table using HTML5 ? In this article, group the body content in an <table> element in a document. It is used to define a standard cell in an HTML table. Syntax: <tbody> ... </tbody> Example 1: In this example, we use some HTML tags and make the group the body content in a table. html <!DOCTYPE html 3 min read How create table without using <table> tag ? HyperText Markup Language (HTML) is the standard markup language used to create web pages. HTML allows table creation using the <table> tag. However, tables can also be created in HTML without using <table> tag with the help of Cascading Style Sheet (CSS). CSS is a mechanism for adding s 3 min read What is the rule attribute in HTML Table ? This is an HTML attribute, that is used to specify which parts of the inside borders should be visible. The HTML rule attribute is applicable on table tags. The <table> rules attribute is not supported by HTML 5. Syntax: <table rules="value"> Attributes: This attribute accepts 5 values a 1 min read Why should we avoid use of tables for layout in HTML ? In this article, we will learn why we should avoid using tables for layout in HTML. A website can be divided into various sections comprising of header, menus, content, and footer based on which there are many different layout designs available for developers. Different layouts can be created by usi 4 min read How to fix the height of rows in the table? Fixing the height of rows in a table ensures that all rows have a uniform appearance, preventing them from resizing dynamically based on their content. This approach is useful for maintaining a consistent presentation, especially when dealing with tabular data that needs to be displayed neatly.Here 3 min read How to set the number of rows a table cell should span in HTML ? In this article, we will show how multiple rows are spanned by a table header cell. The task can be done by using the rowspan attribute while using the <th> tag. This property is used to merge one or more cells as well as increase the height of the single header cell automatically. Syntax: 2 min read How to set fixed width for <td> in a table ? The requirement of a table is very normal in the process of designing a web page. HTML provides the <table> tag to construct the table and to define rows and columns <tr> and <td> tags respectively are used. By default, the dimensions of the rows and columns in a table are adjusted 5 min read How to merge table cells in HTML ? The purpose of this article is to explore the method of merging table cells in HTML using the rowspan and colspan attributes. By utilizing rowspan, multiple cells in a row can be merged or combined, while colspan enables the merging of cells in a column within an HTML table. This technique proves es 2 min read How to Create Time-Table Schedule using HTML? A time table or schedule is essential for organizing tasks, events, or classes. Weâll create a basic time-table layout using HTML. A Table is an arrangement of rows and columns. Anyone can create a table by knowing the basics of HTML(HyperText Markup Language). In HTML we can use the <table> t 3 min read Like