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

Skip to content geeksforgeeks
  • Tutorials
    • Python Tutorial
      • Python Data Types
      • Python Loops and Control Flow
      • Python Data Structures
      • Python Exercises
    • Java
      • Java Programming Language
        • OOPs Concepts
      • Java Collections
      • Java Programs
      • Java Interview Questions
      • Java Quiz
      • Advance Java
    • Programming Languages
    • System Design
      • System Design Tutorial
    • Interview Corner
    • Computer Science Subjects
    • DevOps
    • Linux
    • Software Testing
    • Databases
    • Android
    • Excel
    • Mathematics
  • DSA
    • Data Structures
      • Linked List
      • Tree
    • 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
  • Courses
    • DSA Courses
    • Programming Languages
  • 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 Create Table in HTML?
  • How to group the body content in a table using HTML5 ?
  • How create table without using <table> tag ?
  • What is the rule attribute in HTML Table ?
  • Why should we avoid use of tables for layout in HTML ?
  • How to fix the height of rows in the table?
  • How to set the number of rows a table cell should span in HTML ?
  • How to set fixed width for <td> in a table ?
  • How to merge table cells in HTML ?
  • How to Create Time-Table Schedule using HTML ?
  • How to Create Nested tables within tables in HTML ?
  • How to use tables to structure forms ?
  • How to fix the width of columns in the table ?
  • How to center contents of an HTML table ?
  • How to fetch data from JSON file and display in HTML table using jQuery ?
  • How to perform a real time search and filter on a HTML table?
  • How to use header & footer in HTML table ?
  • How to apply border inside a table ?
  • What are Cell Padding & Cell Spacing in HTML Table ?
  • HTML Table Padding and Spacing
  • How to create Table with 100% width, with Vertical Scroll inside Table body in HTML?
  • How to Reduce Space Between Two Columns in HTML Table ?
  • Can I Add Multiple <tbody> Elements in Same Table in HTML ?
  • How to add table footer in HTML ?
  • What are the HTML tags used to display the data in the tabular form ?
  • How to add tooltip to HTML table cell without using JavaScript ?
How to set fixed width for <td> in a table ? Last Updated : 19 May, 2022 Improve Improve Like Article Like Save Share Report

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.

R

RISHU_MISHRA Follow Improve Previous Article How to set the number of rows a table cell should span in HTML ? Next Article How to merge table cells in HTML ?

Please Login to comment...

Similar Reads

How to Set the Fixed Width of a Span Element in CSS ? Setting a fixed width of a span element is useful when we need to control the size of a specific inline element. The span element is mostly used in styling specific parts of text and other inline elements. There are various approaches to setting a fixed width of a span element in CSS including, width property, max-width property, and min-width prop 4 min read How to Set a Fixed Width Column with CSS Flexbox ? When working with CSS flexbox, setting a fixed width for a column can be achieved by using various approaches to override the default behavior of flex items, which typically grow and shrink to fill available space. Table of Content Using the flex propertyUsing the min-width and max-width propertiesCombining flex-basis with flex-grow and flex-shrink 4 min read React.js BluePrint Navbar Component Fixed width React.js Blueprint is a front-end UI toolkit. It is very optimized and popular for building interfaces that are complex data-dense for desktop applications. The theReact.js Blueprint Navbar Component acts as a navigator at the top of a website and it allows the user to provide an easy way to navigate. To set a fixed with of the theReact.js Blueprin 3 min read Blaze UI Input Group Fixed Width Blaze UI is a free open source UI Toolkit that provides a great structure for building websites quickly with a scalable and maintainable foundation. All components in Blaze UI are developed mobile-first and rely solely on native browser features, not a separate library or framework. It helps us to create a scalable and responsive website fast and e 2 min read How to wrap or break long text/word in a fixed width span ? In this article, we will see how to wrap long text/word in a fixed-width span. The word-wrap property in CSS is used to break long words and wrap them into the next line. It defines whether to break words when the content exceeds the boundaries of its container. Syntax: word-wrap: normal|break-word|initial|inherit; Example 1: The following code dem 1 min read Fixed Width Design In the early days of the world wide web, the majority of people out there were using desktop computers. These days the web is available on laptops, phones, tablets, cars, etc. People expect that the website will look good on every device. Responsive design makes this possible. Responsive web design isn't the first approach to building websites. Web 5 min read How to design Responsive card-deck with fixed width in Bootstrap? To design a responsive card deck with a fixed width in Bootstrap, utilize a grid layout of cards. Apply custom CSS to set a fixed width for each card, ensuring they maintain consistent dimensions across various screen sizes. Utilize Bootstrap's responsive utilities to adjust card spacing and layout as needed for different viewport sizes. ApproachBe 3 min read Which table property specifies the width that should appear between table cells in CSS ? In this article, we will see how to specify the width that should appear between table cells. CSS provides the border-spacing property that can be used to set the distance between the borders of neighboring cells in the Table. This property works only when the border-collapse property is set to no-collapse separate. Syntax: border-spacing: length|i 2 min read How to create Table with 100% width, with Vertical Scroll inside Table body in HTML? To add a vertical scroll bar inside the table body, use the overflow-y property. By changing the display property of tbody to block, we can display the block-level element. However, when changing the display property of tbody, we must also change the property of thead to prevent the table layout from breaking. ApproachApply the class scroll down to 2 min read HTML width/height Attribute vs CSS width/height Property In HTML 5, few elements follow the width and height attributes and maximum elements do not follow this attribute. Like img, iframe, canvas, and svg kind of elements follow the width and height attributes but div, span, article and section type of elements don't follow them.The width and height attributes are affected in img, svg tags, those are wea 3 min read View More Articles Article Tags :
  • HTML-Questions
  • HTML-Tags
  • How To
  • HTML
  • Web Technologies
+2 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 Please go through our recently updated Improvement Guidelines before submitting any improvements. This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab. You will be notified via email once the article is available for improvement. Thank you for your valuable feedback! Suggest changes Please go through our recently updated Improvement Guidelines before submitting any improvements. 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 Suggestion[CharLimit:2000] Create Improvement

What kind of Experience do you want to share?

Interview Experiences Admission Experiences Career Journeys Work Experiences Campus Experiences Competitive Exam Experiences Can't choose a topic to write? click here for suggested topics Write and publish your own Article

Từ khóa » Html Table Fixed Column Width Not Working