
HTML tables are a staple of web development, and understanding how to work with table columns is essential for creating effective and organized layouts.
A table column is a vertical group of cells within an HTML table. It's the column header that usually contains the column title, which helps users understand the data being presented.
In HTML, you can define a table column using the `` element, which stands for table header. This element is used to create column headers that are typically displayed in a bold font.
Suggestion: Html Tag B
HTML Table Column Basics
HTML tables can be complex, but understanding the basics of table columns can make a big difference. A table's column count is determined by the number of TR elements contained by the TABLE element.
To render a table in one pass, authors must tell the user agent the number of columns and their widths. This can be done using a combination of COLGROUP and COL elements.
You can specify the number of columns in a table by counting the number of TR elements contained by the TABLE element.
You might enjoy: Html 2 Columns
Calculating Column Count
There are two ways to determine the number of columns in a table, and user agents should follow a specific order of precedence.
If a table contains COLGROUP or COL elements, the number of columns is calculated by summing the number of columns specified in these elements.
If a table contains no COLGROUP or COL elements, the number of columns is based on what is required by the rows, specifically the row with the most columns, including cells that span multiple columns.
The number of columns is equal to the number of columns required by the row with the most columns, and any row with fewer columns should be padded with empty cells.
The "end" of a row depends on the table directionality.
It's an error if a table contains COLGROUP or COL elements and the two calculations don't result in the same number of columns.
For example, for each of the following tables, the two column calculation methods should result in three columns.
Here's a quick summary of the two column calculation methods:
The first three tables may be rendered incrementally.
Calculating the Width
Calculating the width of columns in an HTML table can be a bit tricky, but don't worry, it's easier than you think.
Authors may specify column widths in three ways, but if the table doesn't have a fixed width, user agents must receive all table data before determining the horizontal space required by the table.
If an author specifies no width information for a column, a user agent may not be able to incrementally format the table since it must wait for the entire column of data to arrive.
The table in this example contains six columns, with the first one not belonging to an explicit column group and the next three belonging to the first explicit column group.
Once the user agent has received the table's data, the available horizontal space will be allotted by dividing it into portions based on the width specifications.
The user agent will allot 30 pixels to columns one and two, then reserve the minimal space required for the third column, and finally divide the remaining space into six equal portions.
A unique perspective: Print Html One by One
Column four will receive two of these portions, column five one, and column six three.
We can also specify the align attribute in a column group to center the cells, but individual cells can override this value.
In the following table, the column width specifications allow the user agent to format the table incrementally, with the first ten columns being 15 pixels wide each and the last two columns receiving half of the remaining 50 pixels.
Discover more: Fastapi Two Html
Width
Column widths are crucial for making your HTML tables look great.
You can specify column widths in three ways: by using the width attribute on the COL element, by using the width attribute on the COLGROUP element, or by not specifying any width information at all.
However, if you don't specify a width for a column, the user agent may not be able to format the table incrementally, since it needs to know the width before it can render the table.
Expand your knowledge: Set up Html Mail Using Word
If you specify no width information for a column, a user agent may not be able to incrementally format the table since it must wait for the entire column of data to arrive in order to allot an appropriate width.
The COLGROUP element creates an explicit column group, and the number of columns in the group can be specified in two ways: either by using the span attribute or by including COL elements.
The COL element can be used to specify the width of a column, and can also be used to assign an id value to a column so that style sheets can refer to it.
User agents must ignore the span attribute if the COLGROUP element contains one or more COL elements.
The width attribute of the COLGROUP element is inherited by all columns in the group, unless overridden by a COL element.
Here are some ways to specify column widths:
- Using the width attribute on the COL element
- Using the width attribute on the COLGROUP element
- Not specifying any width information at all
The width attribute can be specified in various ways, including pixels, percentages, and relative values.
Cells: Th and Td Elements

Cells in a table are defined by two elements: and. The element defines a cell that contains header information, while the element defines a cell that contains data. You can put whatever you want inside a table cell, but these are the elements that make them a table cell. elements are not limited to being within the, they simply indicate header information. They could be used, for instance, at the start of a row in the, if that was relevant.
User agents have two pieces of header information available: the contents of the element and the value of the abbr attribute. You can use the abbr attribute as an abbreviation for table headers when these are rendered along with the contents of the cells to which they apply.
The distinction between header information and data in table cells enables user agents to render them distinctly. For example, visual user agents may present header cell text with a bold font, while speech synthesizers may render header information with a distinct voice inflection.
Here are the roles of elements in different contexts:
- row: The current cell provides header information for the rest of the row that contains it.
- col: The current cell provides header information for the rest of the column that contains it.
- rowgroup: The header cell provides header information for the rest of the row group that contains it.
- colgroup: The header cell provides header information for the rest of the column group that contains it.
Cells may be empty, i.e., contain no data.
Column Properties
Column Properties are crucial in defining the behavior and appearance of table columns. The COLGROUP element defines a column group containing one or more columns, and its span attribute specifies the number of columns in the group.
To specify the width of columns, authors can use the COLGROUP element's width attribute, which allows for standard pixel, percentage, and relative values, as well as the special form "0*" (zero asterisk) that means the width of each column should be the minimum width necessary to hold its contents.
A user agent may render a table in a single pass when the column widths are specified using a combination of COLGROUP and COL elements, but authors must also specify the width of the table itself if any of the columns are specified in relative or percentage terms.
Here are some key attributes of the COL element:
- span: specifies how many columns the COL element contains.
- width: specifies the width of the column.
The COL element manipulates a limited number of CSS properties, including visibility, width, background, and border, but not text color.
Column Groups: Col and Col Elements
Column groups are a crucial aspect of table layout, allowing authors to create structural divisions within a table. This can be achieved through the use of COLGROUP and COL elements.
The COLGROUP element defines a column group containing one or more columns. If the span attribute is set to N > 0, the current COLGROUP element defines a column group containing N columns. This attribute specifies a default width for each column in the current column group.
The COL element allows authors to group together attribute specifications for table columns. It does not group columns together structurally, but rather serves as a support for attributes. The COL element may appear inside or outside an explicit column group.
Here are some key points to keep in mind when using the COL and COLGROUP elements:
The span attribute of the COL element specifies how many columns the element contains. Authors can use the COL element to share attributes among several columns without implying any structural grouping. The "span" of the COL element is the number of columns that will share the element's attributes.
User agents must ignore the span attribute if the COLGROUP element contains one or more COL elements. The width attribute of the COLGROUP element is inherited by all columns in the group, unless overridden by a COL element.
Suggestion: 3 Columns Html
Using the Scope Attribute
The scope attribute in HTML tables is used to define whether a specific header is intended for a column, row, or a group of both. It's declared in the header cell and takes the values col, row, colgroup, and rowgroup.
The scope attribute helps user agents understand the relationship between header cells and data cells. For example, if a header cell has a scope attribute value of "col", it indicates that the header cell provides information for the rest of the column that contains it.
In other words, the scope attribute shows the target data, so the user doesn't have to rely on assumptions. The values col and row indicate that the header cell is providing information for either the rows or columns respectively.
Here's a summary of the scope attribute values:
By using the scope attribute, you can help non-visual user agents process header information more effectively. This can be especially helpful for users who rely on assistive technologies to navigate and understand complex tables.
Cell and Column Alignment
Cell and column alignment in HTML tables can be a bit tricky, but don't worry, I've got the basics covered. The alignment of cell contents can be specified on a cell by cell basis or inherited from enclosing elements like the row, column, or table itself.
The order of precedence for alignment attributes is quite specific. Here are the steps:
- An alignment attribute set on an element within a cell's data.
- An alignment attribute set on a cell (TH and TD).
- An alignment attribute set on a column grouping element (COL and COLGROUP).
- An alignment attribute set on a row or row grouping element (TR, THEAD, TFOOT, and TBODY).
- An alignment attribute set on the table (TABLE).
- The default alignment value.
For vertical alignment, rows are given preference over columns, while horizontal alignment is determined by columns in preference to rows.
Broaden your view: Html Table Space between Rows
Align
Aligning content in tables can be a bit tricky, but it's essential to get it right. The align attribute is used to set the horizontal alignment of content in table cells, but it's not supported in HTML5.
The order of precedence for the align attribute is as follows: an attribute set on an element within a cell's data takes precedence over an attribute set on a cell, and so on.
Explore further: Html Vertical Align Table Cell
If you're using the align attribute, keep in mind that user agents should substitute the default attribute for the current directionality.
Here's a quick rundown of the order of precedence for the align attribute:
- An attribute set on an element within a cell's data (e.g., P).
- An attribute set on a cell (TH and TD).
- An attribute set on a column grouping element (COL and COLGROUP).
- An attribute set on a row or row grouping element (TR, THEAD, TFOOT, and TBODY).
- An attribute set on the table (TABLE).
- The default alignment value.
It's worth noting that horizontal alignment is determined by columns in preference to rows, while vertical alignment is determined by rows in preference to columns.
For another approach, see: Html Alignment
TD and TH Cells
The TD and TH cells are the building blocks of a table, and understanding how they work is crucial for creating well-structured and accessible tables.
The TD element defines a cell that contains data, while the TH element defines a cell that contains header information. This distinction is important because it allows user agents to render header and data cells distinctly.
User agents can use the contents of the TH element or the value of the abbr attribute to render header information. For visual media, the abbr attribute may be used as an abbreviation when there is insufficient space to render the full contents of the cell.
See what others are reading: What Is Tr Th and Td in Html
The headers and scope attributes allow authors to help non-visual user agents process header information. The headers attribute lists which cells provide pertinent header information, while the scope attribute tells the user agent the data cells for which this header provides information.
Here are the possible values for the scope attribute:
- row: The current cell provides header information for the rest of the row that contains it.
- col: The current cell provides header information for the rest of the column that contains it.
- rowgroup: The header cell provides header information for the rest of the row group that contains it.
- colgroup: The header cell provides header information for the rest of the column group that contains it.
The contents of the TH element or the value of the abbr attribute can be used to render header information, and user agents must render either one of them.
Highlighting on Hover
Highlighting on Hover is a simple yet effective way to draw attention to specific cells, rows, or columns in a table. You can do it in CSS by setting the background color on table rows or cells.
Cell highlighting is particularly easy, and you can do it right in CSS. Just set the background color on the table cells, and it will show as long as you don't set a background on the table rows.
Worth a look: Column Merge in Html
But what about column highlighting? That's a bit trickier, and you can't use col:hover because those columns aren't actual elements that take up pixel space on the screen that you could hover over. In that case, you'll need to use JavaScript.
Here's a quick rundown of the steps you'll need to take:
- Get a collection of all cells
- Bind a mouseover and mouseout event to all those cells
- When the mouseover event fires, get the position in the row of that cell
- Loop through all rows and add a highlighting class to each cell in that row that matches that position
- When the mouseout event fires, remove the highlighting class from all cells
Or, if you're using jQuery, you can make it even easier by creating element collections and finding elements by index. Either way, it's a simple process that can make a big impact on the usability of your table.
Column Spanning and Header Association
Column spanning is achieved using the colspan attribute, which assigns a number greater than zero to indicate the number of cells to span. This attribute is used to define the number of columns a cell should span.
A cell can span several rows or columns using the rowspan and colspan attributes for the TH and TD elements. The number of rows or columns spanned by a cell is set by these attributes.
Curious to learn more? Check out: Html Cell Width
To avoid overlapping cells, it's essential to declare the values assigned to the colspan and rowspan attributes correctly. If a cell spans two rows, the definition of the second row will take it into account, affecting the definition of later cells.
The TD element can be used together with the id or scope attributes to make a clean division of cells into headers or data. The headers attribute lists which cells provide pertinent header information, while the scope attribute tells the user agent the data cells for which a header provides information.
Here's a summary of the attributes used for column spanning and header association:
Cells Span Multiple Rows
Cells can span multiple rows, and this is achieved by setting the rowspan attribute for the TH and TD elements. The number of rows spanned by a cell is set by the rowspan attribute.
A cell can span a total of three columns, including the current row, by specifying the rowspan attribute in the table definition. This is illustrated in the example where a cell spans the first and second rows.
A fresh viewpoint: Rowspan and Colspan in Html
The definition of later cells is affected by cell definitions that span more than one row or column. For instance, in the example where cell "2" spans the first and second rows, the definition of the second row takes it into account.
If a TD defining cell "6" is omitted, an extra empty cell would have been added by the user agent to complete the row. This is because the second TD in the row actually defines the third cell ("6").
Defining overlapping cells is an error, and user agents may vary in how they handle this error.
For your interest: Document Type Definition in Html
Associating Header Information with Data Cells
Associating header information with data cells is crucial for non-visual user agents like speech synthesizers and Braille-based devices. This is achieved through the TD and TH element attributes.
The headers attribute lists which cells provide pertinent header information. To use it, each header cell must be named using the id attribute. For instance, in Example 1, the header "Type of Coffee" is abbreviated to "Type" using the abbr attribute.
Here's an interesting read: Using Oembed in Base Html

Authors may choose to use the scope attribute instead of headers, depending on what's more convenient. The scope attribute tells the user agent the data cells for which this header provides information. It's generally needed when headers are placed in irregular positions with respect to the data they apply to.
Here's a breakdown of the attributes used for associating header information with data cells:
In Example 2, the tag is used to add headings to tables. By default, the text passed in the heading is centered and bold. This makes it easy to identify which column contains which information.
Responsive Design and Accessibility
In responsive design, tables can be a challenge, especially on small screens. This is because data tables are often not optimized for mobile devices, leading to a poor user experience.
One way to improve this is to turn rows into blocks, making it easier for users to scan and understand the data.
Consider reading: Advanced Html Tables Examples
FooTable is a jQuery plugin that allows you to hide rows and make them available via a toggle icon, which can be a great solution for reducing clutter on small screens.
Turn data into a chart is another option, as it can be more visually appealing and easier to understand on smaller screens.
Hiding columns to save room and allowing users to toggle them back on is also a good strategy, as it gives users more control over the data they see.
Squishing cells and allowing them to wrap is another technique, but it's worth noting that this can be tricky to implement and may not work well in all cases.
Fixing a header and allowing the body to scroll is a common approach, but it can be tricky to implement and may not work well in all cases.
Here are some specific techniques to consider:
- Turn rows into blocks
- Use FooTable to hide rows and toggle them back on
- Turn data into a chart
- Hide columns to save room
- Squish cells and allow them to wrap
- Fix a header and allow the body to scroll
Advanced Topics
HTML table columns can be a bit tricky to manage, especially when dealing with a large amount of data. You can use the `colspan` attribute to merge two or more columns into one.
To make a column wider, you can use the `colspan` attribute and set its value to the number of columns you want to merge. For example, if you want to merge two columns, you would set `colspan="2"`.
You can also use the `colspan` attribute to make a column narrower by setting its value to a number less than the number of columns it's currently spanning. This can be useful if you want to create a gap between two columns.
Additional reading: Colspan Use in Html
JavaScript Generated
JavaScript Generated tables can be created and manipulated using the HTMLTableElement API. This API provides specific methods for dealing with tables, such as creating tables with JavaScript.
You can use JavaScript to create tables, access sub-elements, and change properties in very specific ways. Louis Lazaris has written about this topic recently.
The MDN page has more information on the HTMLTableElement API, providing a comprehensive scoop on how to use it.
Broaden your view: Html to Pdf Api
Sorting
Sorting is a common need in data presentation, especially when dealing with tables.
Imagine having a table with numerical data like employee IDs, where you can easily sort it in ascending or descending order by clicking on the header. Unfortunately, no browser currently supports table sorting natively.
There are, however, plenty of third-party options available. Some popular ones include tablesorter, which is jQuery-based, and sorttable, which is a raw JavaScript solution.
If you're looking for more options, Codrops has rounded up 33 different table sorting scripts, so you're sure to find one that fits your needs.
It's worth noting that sorting can also be done on the back-end, where you can sort the data before displaying it in the HTML. This might be necessary for paginated tables, where not all data is available in the DOM.
Making Semantic Elements Behave Like a Table
You can use CSS to make any element behave like a table, which can be useful sometimes. This means structuring elements as you would a table, but it's still subject to source-order-dependency.
To achieve this, you'll need to alter the display property of the element. There are several values you can use: display: table; display: table-row; and display: table-cell;.
Notice that there is no alternative to in CSS, as it's only for semantic value. This means that elements behave just like elements, so you don't need to replicate it in CSS.
A handy trick is that you don't need the table-row element if you don't want to. A bunch of display: table-cell; elements that are children of a display: table; element will behave like they are all in one row.
The display: inline-table; value is pretty interesting, as it makes table elements behave like inline-block elements, without the breaking.
Intriguing read: Html Page Cloner Aith Inline Css
Frequently Asked Questions
What is TD and TR in HTML?
In HTML, TD stands for Table Data and represents a single cell in a table, while TR stands for Table Row and represents a row of cells in a table. Understanding the difference between TD and TR is crucial for creating effective and organized tables in web development.
Featured Images: pexels.com


