How To Set Fixed Width For In A Table ? - GeeksforGeeks

Skip to content geeksforgeeks
  • Courses
    • DSA Courses
    • Programming Languages
  • Tutorials
    • Python Tutorial
      • Python Loops and Control Flow
    • Java
      • Java Interview Questions
      • Java Quiz
      • Advance Java
    • Programming Languages
    • System Design
    • Interview Corner
    • Computer Science Subjects
    • DevOps
    • Linux
    • Software Testing
    • Databases
    • Android
    • Excel
    • Mathematics
  • DSA
    • Data Structures
    • Algorithms
      • Analysis of Algorithms
      • Searching Algorithms
      • Sorting Algorithms
    • Practice
      • Company Wise Coding Practice
      • Practice Problems Difficulty Wise
      • Language Wise Coding Practice
      • Curated DSA Lists
    • Company Wise SDE Sheets
    • DSA Cheat Sheets
    • Puzzles
  • Data Science
    • Data Science Packages
    • Data Visualization
    • Data Analysis
  • Web Tech
    • Web Development Using Python
      • Django
      • Flask
    • Cheat Sheets
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App How to set fixed width for <td> in a table ? Last Updated : 19 May, 2022 Summarize Comments Improve Suggest changes Like Article Like Save Share Report Follow

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 by the browser automatically in a way that fits the contents. However, there can be situations where the width of the columns are needed to be fixed. There are multiple ways to fix the width for <td> tag. Some of them are mentioned below:

  • Using width attribute: The <td> tag has width attribute to control the width of a particular column. By assigning a numeric value to this attribute between 0 to 100 in terms of percentage(or you can use pixel format). We can restrict the column width up to that much percentage of the table’s total width. The width attribute is invalid and has been disapproved, thus it is no longer supported by HTML5.

HTML

<!DOCTYPE html> <html> <head> <title> Set up fixed width for </title> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } </style> </head> <body> <h1 style="color: #00cc00;"> GeeksforGeeks </h1> <!-- Making the table responsive --> <div style="overflow-x: auto;"> <!-- Adding table in the web page --> <table width="50%"> <tr> <th>GfG Courses</th> <th>Description</th> </tr> <tr> <td width="40%"> DS and Algo Foundation </td> <td width="60%"> Master the basics of Data Structures and Algorithms to solve complex problems efficiently. </td> </tr> <tr> <td width="40%">Placement 100</td> <td width="60%"> This course will guide you for placement with theory,lecture videos, weekly assignments, contests and doubt assistance. </td> </tr> <tr> <td width="40%"> Amazon SDE Test Series </td> <td width="60%"> Test your skill & give the final touch to your preparation before applying for product based against like Amazon, Microsoft, etc. </td> </tr> </table> </div> </body> </html>

  • Using CSS: The Cascading Style Sheet(CSS) is widely used to decorate the web pages which are of vast sizes. By using CSS, the styling of HTML elements is easy to modify. To fix the width of td tag the nth-child CSS is used to set the property of specific columns(determined by the value of n) in each row of the table.

HTML

<!DOCTYPE html> <html> <head> <title>Set up fixed width for <td></title> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } table { width: 50%; } // Fixing width of first // column of each row td:nth-child(1) { width: 40%; } // Fixing width of second // column of each row td:nth-child(2) { width: 60%; } </style> </head> <body> <h1 style="color: #00cc00;"> GeeksforGeeks </h1> <!-- Making the table responsive --> <div style="overflow-x: auto;"> <!-- Adding table in the web page --> <table> <tr> <th>GfG Courses</th> <th>Description</th> </tr> <tr> <td>DS and Algo Foundation</td> <td> Master the basics of Data Structures and Algorithms to solve complex problems efficiently. </td> </tr> <tr> <td>Placement 100</td> <td> Test your skill & give the final touch to your preparation before applying for product based against like Amazon, Microsoft, etc. </td> </tr> <tr> <td>Amazon SDE Test Series</td> <td> Test your skill & give the final touch to your preparation before applying for product based against like Amazon, Microsoft, etc. </td> </tr> </table> </div> </body> </html>

  • Using col tag and fixing the table layout property: If the same kind of column property is to be followed in every row of a table then using col tag to define the properties of the column is a great idea. If the col tag is written in the HTML document for the first time and its attributes are set, those all property refers to the first column of each row of the table inside which it is mentioned. Similarly using col tag for the second time and defining its attributes make its impact on the second column of each row of the table. Moreover, to adjust the long texts inside the columns CSS property table-layout:fixed; is used. Below is the implementation.

HTML

<!DOCTYPE html> <html> <head> <title>Set up fixed width for</title> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } table { width: 50%; } table.fixed { table-layout: fixed; } table.fixed td { overflow: hidden; } </style> </head> <body> <h1 style="color: #00cc00;"> GeeksforGeeks </h1> <!-- Making the table responsive --> <div style="overflow-x: auto;"> <!-- Adding table in the web page --> <table> <!-- Assigning width of first column of each row as 40% --> <col style="width: 40%;" /> <!-- Assigning width of second column of each row as 60% --> <col style="width: 60%;" /> <tr> <th>GfG Courses</th> <th>Description</th> </tr> <tr> <td>DS and Algo Foundation</td> <td> Master the basics of Data Structures and Algorithms to solve complex problems efficiently. </td> </tr> <tr> <td>Placement 100</td> <td> This course will guide you for placement with theory,lecture videos, weekly assignments, contests and doubt assistance. </td> </tr> <tr> <td>Amazon SDE Test Series</td> <td> Test your skill & give the final touch to your preparation before applying for product based against like Amazon, Microsoft, etc. </td> </tr> </table> </div> </body> </html>

  • Output: The output will be same for every method.

Image of a table having fixed width of columns as 40% width of first column and 60% width of second column

HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

Comment More info Next Article How to merge table cells in HTML ?

R

RISHU_MISHRA Follow Improve

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 Elements Description <table> The <table> element 4 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. C/C++ Code &lt;!DOC 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
  • How to Create Nested tables within tables in HTML ? HTML tables are a great way to organize content into rows and columns. But what if you need to place a table inside another table? This is called nested tables, and HTML allows you to do this easily. Nested tables are useful when you want to create more complex layouts inside your main table structu 3 min read
  • How to use tables to structure forms ? Creating an HTML form is an important aspect when creating a website. Forms are a method of interacting with the users of the website. Thus it becomes necessary that the forms must be properly aligned and attractive when implemented on a website. The process of simply adding an HTML form to a webpag 3 min read
  • How to fix the width of columns in the table ? 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 3 min read
  • How to center contents of an HTML table ? To center content in an HTML table, add text-align: center; CSS for each cell or use align="center" it in the <td> tags. This keeps text in the middle. Syntax text-align: left;Property Value Values Description initial It is the default value. inherit It sets the value to the parent's element p 1 min read
  • How to fetch data from JSON file and display in HTML table using jQuery ? The task is to fetch data from the given JSON file and convert data into an HTML table. Approach: We have a JSON file containing data in the form of an array of objects. In our code, we are using jQuery to complete our task. The jQuery code uses getJSON() method to fetch the data from the file's loc 3 min read
  • How to Perform Real Time Search and Filter on HTML table? Real-time search and filter on an HTML table lets users quickly find rows by typing in a search box. The table updates instantly, showing only rows that match the search text. To perform a real-time search and filter on an HTML table, add a search box. Use JavaScript to check the input and hide rows 3 min read
  • How to use header & footer in HTML table ? The <thead> and <tfoot> tags are used to separate the table into header and footer, which becomes useful when we are dealing with tables having a large amount of data. In this article, we would be discussing how to use the header and footer tags in HTML. For that purpose, we have created 3 min read
  • How to apply border inside a table ? Applying a border inside a table involves setting border properties to table elements. Use CSS to specify border width, style, and color. Apply borders to table rows, cells, or specific elements using CSS selectors like table, tr, td, or their respective classes or IDs. There are some common ways to 4 min read
  • What are Cell Padding & Cell Spacing in HTML Table ? In this article, we will discuss the cellpadding and cellspacing properties in HTML. These properties are used with the table tag for defining the spacing and padding of a table. Cellpadding: This property specifies the space between the border of a table cell and its contents (i.e) it defines the w 2 min read
  • HTML Table Padding and Spacing HTML Table Padding & Spacing is used to control the space within and around table cells in HTML. Padding is the space inside a table cell, while spacing (or border-spacing) is the space between the outer borders of adjacent cells. Table Cell PaddingTable Cell Padding refers to the space between 2 min read
  • How to create Table with 100% width, with Vertical Scroll inside Table body in HTML? To create a table with 100% width and a vertical scroll inside the table body in HTML, use the overflow-y property. Set the <tbody> display to block to enable scrolling while adjusting the <thead> to maintain the layout. This method creates a scrollable table for effectively displaying l 2 min read
  • How to Reduce Space Between Two Columns in HTML Table ? In HTML, we can create tables with multiple columns to display data in an organized manner. Sometimes, we may want to reduce the space between two adjacent columns in a table. We can achieve this by applying CSS properties to the table elements. In this article, we will see the different approaches 3 min read
  • Can I Add Multiple <tbody> Elements in Same Table in HTML ? Yes, we can add multiple <tbody> tags in a same table in HTML. The <tbody> tag defines the body content (a set of rows) of an HTML table creating a separate semantic block in it. The <tbody> tag is used along with the <thead> and the <tfoot> tags, which specify the head 2 min read
  • How to add table footer in HTML ? To add a table footer on an HTML table you can use the HTML tfoot tag. The footer is required when the developer wants to show the conclusion or the result of the table data in a single row. Syntax: <tfoot> // Table footer contents... </tfoot> Below example will illustrate the concept of 1 min read
  • What are the HTML tags used to display the data in the tabular form ? In this article, we will know the HTML tags that can be used to display the data in tabular form. A table is a representation of data in rows and columns, that helps to organize the complex structured data. Tables are widely used in communication, research, and data analysis. For instance, if we nee 4 min read
  • How to add tooltip to HTML table cell without using JavaScript ? Given an HTML table and the task is to add a tooltip to the table cell without using JavaScript. There are two methods to solve this problem which are discussed below: Approach 1: Create a HTML table. Add title attribute (title = "someTitle") to the table cell to add tooltip. Example 1: This example 2 min read
Article Tags :
  • How To
  • HTML
  • Web Technologies
  • HTML-Questions
  • HTML-Tags
+1 More Like three90RightbarBannerImg Explore More We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It ! Lightbox Improvement Suggest changes Suggest Changes Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal. geeksforgeeks-suggest-icon Create Improvement Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all. geeksforgeeks-improvement-icon Suggest Changes min 4 words, max CharLimit:2000

What kind of Experience do you want to share?

Interview Experiences Admission Experiences Career Journeys Work Experiences Campus Experiences Competitive Exam Experiences

Từ khóa » Html Col Max Width