<col>

Definition

The HTML col element is used to represent a column within a table. This element is used in conjunction with the colgroup and thead, tbody, and tfoot elements to specify the design and formatting of the table.

The col element is used to specify the alignment, width, and other formatting properties for each column in a table. By using the col element, authors can easily apply styles to entire columns of a table, rather than having to specify the styles for each individual cell.

One common use for the col element is to set the width of columns in a table. This can be useful when you want to ensure that the columns in your table are a specific size, or when you want to make the table more readable by adjusting the width of the columns.

In addition to setting the width of columns, the col element can also be used to specify the alignment of the content within each column. This can be useful when you want to ensure that the text within a column is aligned in a specific way, such as left-aligned, right-aligned, or centered.

The col element is a useful tool for authors who want to control the design and formatting of their HTML tables. By using this element, you can easily apply styles to entire columns of a table, making it easier to create well-designed and consistent tables.

Examples

<table>
	<colgroup>
		<col span="2" style="width: 50%; text-align: left;">
		<col style="width: 25%; text-align: center;">
		<col style="width: 25%; text-align: right;">
	</colgroup>
	<thead>
		<tr>
			<th>Column 1</th>
			<th>Column 2</th>
			<th>Column 3</th>
			<th>Column 4</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>Row 1, Cell 1</td>
			<td>Row 1, Cell 2</td>
			<td>Row 1, Cell 3</td>
			<td>Row 1, Cell 4</td>
		</tr>
		<tr>
			<td>Row 2, Cell 1</td>
			<td>Row 2, Cell 2</td>
			<td>Row 2, Cell 3</td>
			<td>Row 2, Cell 4</td>
		</tr>
		...
	</tbody>
</table>

In this example, the col element is used within the colgroup element to specify the width, alignment, and other formatting properties for each column in the table. The col element is used in combination with the thead, tbody, and tfoot elements to define the structure and content of the table.

Attributes

AttributeDescriptionDeprecated
spanSpecifies the number of columns a col element should spanNo
widthSpecifies the width of the columns in pixels or as a percentageYes, use CSS instead
alignSpecifies the horizontal alignment of the content within the columnsYes, use CSS instead
valignSpecifies the vertical alignment of the content within the columnsYes, use CSS instead

Best Practices

  • Use the col element in combination with the colgroup and thead, tbody, and tfoot elements to define the structure and formatting of a table.
  • Use the col element to specify the width, alignment, and other formatting properties for each column in a table.
  • Use the span attribute on the col element to specify the number of columns that the element should span.
  • Use the width attribute on the col element to specify the width of the columns in pixels or as a percentage.
  • Avoid using the deprecated align and valign attributes on the col element, and use CSS instead to specify the alignment of the content within the columns.
  • Use the scope attribute on the th elements within the table to provide context and information about the table to screen readers.
  • Keep accessibility in mind when using the col element, and ensure that your tables are readable and understandable by all users, regardless of their assistive technology needs.

Accessibility Considerations

When using the col element to specify the width and alignment of columns in a table, it is important to ensure that the table is still accessible to users who rely on assistive technologies, such as screen readers.

Also, one way to ensure that your tables in general are accessible is to use the scope attribute on the th elements within the table. The scope attribute can be used to specify whether the th element applies to a row, column, or group of rows or columns within the table. This information can be used by screen readers to provide more context and information about the table to users.

Additionally, it is important to ensure that the text within the table is readable and easily understandable by all users, regardless of their assistive technology needs. This can be achieved by using clear, concise language, and avoiding the use of complex jargon or abbreviations.

Browser Compatibility

ChromeFirefoxSafariInternet ExplorerMicrosoft EdgeOpera
YesYesYesYesYesYes