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.

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 ?

Similar Reads

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 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 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 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 large datasets, enhancing user experience with an H 2 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 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 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 Tailwind CSS - Fixed Width Not Working With Other Element In Flex Tailwind CSS's utility-first CSS classes make it easier to create responsive layouts. Nevertheless, when developers attempt to put a fixed width on one element within a flex container, they frequently discover that the element behaves strangely when it interacts with other flexible elements. The way that Flexbox divides space between child items us 3 min read How to Wrap or Break long Text/word in a Fixed Width Span ? Wrapping or breaking long text/words in a fixed-width span means controlling how text flows within a limited container. Using CSS properties like word-wrap: break-word; or overflow-wrap: break-word;, long words break to fit within the container's width. Here we have some approaches to wrap or break long text/word in a fixed width span : Table of Co 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 Difference Between css(‘width’) and width() methods In jQuery In jQuery, we have two ways to change the width of any HTML element. The jQuery css() method and width() method, below both the methods are described properly with the example. jQuery CSS('width') Method: It is a method present in jQuery which is used to get or set the property on the matched element. It is a property used to get or set the width o 3 min read How to add Background-Color for Text Width Instead of Entire Element Width using CSS ? In this article, we will see how to add background color for the text width instead of the entire element width using CSS. The display: inline or display: inline-block property is used to set the background color of the text width. Syntax: display: inline; display: inline-block; Approach: We will create an element and then apply the display: inline 2 min read How to make a grid with two columns where 1st column has 20% of width and 2nd one 80% width in Tailwind CSS ? Tailwind CSS simplifies the process of making complex UI designs. It has a lot of predefined styles in form of classes, which can be applied to any HTML element in the code. Also, one advantage of Tailwind CSS is that during the production build, it analyzes the HTML components, and removes any unused CSS, hence decreasing the overall size of the C 4 min read What is the difference between 'width: 100%' and 'width: 100vw' in CSS ? The CSS width Property is used to set the width of the text and images. The width can be assigned to the text and images in the form of pixels(px), percentages (%), centimeters (cm), etc. The width: 100% sets an element's width relative to its containing block, making it responsive within its parent. The width: 100% responds to the container's size 1 min read Semantic-UI Table Fixed Variation Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website look more amazing. It uses a class to add CSS to the elements. Tables are a great way to group a set of similar items into rows and columns. Many Database 3 min read Difference Between Fixed and Auto Table Layout in CSS In this article, we will see the difference between fixed auto layout vs auto table layout in CSS. Table layout in CSS is handled by the table-layout property. This property specifies the algorithm that the browser uses to determine the width of the table and its columns. There are 2 types of table layout: fixed: Table and column widths are set by 4 min read How To Create a Table With Fixed Header and Scrollable Body? Creating a table with a fixed header and scrollable body (a table with a sticky header and scrollable body) can enhance the data presentation on a website. To create a table with a fixed header and a scrollable body, you can use various approaches to achieve the desired effect. In this article, we'll cover two popular methods to create such a table 5 min read How to set the Width of a Table in HTML ? To set the width of a table in HTML, you can use the "width" attribute within the <table> tag. This attribute specifies the width of the table, either in pixels (px) or as a percentage of the available space. Syntax<table width="value"> <!-- Table content: rows and cells --></table>Key PointDescriptionFixed WidthIf you speci 1 min read How to Set Table Cell Width & Height in HTML ? In HTML, we can control the width and height of table cells using various approaches. This article discusses different methods to set the width and height of table cells in HTML. Table of Content Using inline StyleUsing width and height attributesUsing Inline StyleIn this approach, we are using the inline style to set the width and height of table 2 min read How to set table column width constant regardless of the amount of text in its cells ? HTML tables are a great way to create tabular layouts for the web page so that the user can access the data in tabular format as it is a much more user-friendly way to present a certain type of data. In this article, we will see how we can set the column width of a table to a fixed width that does not change with respect to the amount of text in it 3 min read How To Set Width Of mat-table Column In Angular? In Angular Material, the mat-table component is a powerful data table tool that allows you to display data in a structured and flexible way. However, when it comes to customizing the appearance of the table, especially the width of individual columns, you may need to apply custom styling techniques. In this article, will walk you through various me 4 min read HTML | DOM Table width Property The Table width property in HTML DOM is used to set or return the value of the width attribute of a table. Note: This property is not supported by HTML5. Syntax: It returns the width property of a table.tableObject.width;It is used to set the width property of a table.tableObject.width ="pixels"; Attribute Values: pixels: It sets the width of a tab 2 min read Semantic-UI Table Column Width Variation Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website look more amazing. It uses a class to add CSS to the elements. Tables are an easy way to organize a lot of data. A table is an arrangement of data in rows 3 min read Semantic-UI Table Full-Width Header / Footer Variation Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website look more amazing. It uses a class to add CSS to the elements. Tables are an easy way to organize a lot of data. A table is an arrangement of data in rows 3 min read How to Define a Minimum Width for HTML Table Cell using CSS ? To set a minimum width for an HTML table cell using CSS, apply the min-width property directly to the <td> element, ensuring it does not shrink below the specified size. This ensures a minimum width for the cell content, maintaining layout integrity. Table of Content Using the min-width PropertyApplying a Class or IDUsing the min-width Proper 2 min read How to change the Width of a Column in Bootstrap Table ? In Bootstrap, the table is an important component used for organizing and displaying data in rows and columns. Below are the approaches to change the width of a column in the Bootstrap table: Table of Content Using Bootstrap Grid ClassesUsing width AttributeUsing Bootstrap Grid ClassesIn this approach, we are using Bootstrap's grid system with the 2 min read How to Create a Full-Width Table using CSS? Creating a full-width table using CSS involves setting the table's width to 100% and adjusting other relevant properties to ensure that it stretches across the entire width of its parent container. In this article, we will explain the approach to creating a full-width table using CSS. ApproachCreate a container using <div> to hold a table.Ins 2 min read How to Auto Adjust Table td Width from the Content in HTML? HTML tables are needed to be displayed in responsiveness and adaptability across different screen sizes. Auto-adjusting table cell widths ensures that the table's layout adapts dynamically to the content, improving readability and usability. This can be done using different CSS properties like table layout and width, allowing for better control ove 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 width ="px | %">Approach: The width attribute 3 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 Table Tag Column Width