HTML <colgroup> Element

Publish in HTML Tutorial el 23/05/2025 22:38

The <colgroup> element in HTML is used to group columns within a table for styling or formatting purposes. It serves as a container for one or more <col> elements, which define column properties. This element allows you to apply styles or attributes to entire columns without repeating the same rules for each cell.


Key features of <colgroup>:

  • Must appear after the <caption> element (if present) and before any table content
  • Can span multiple columns using the span attribute
  • Supports styling through CSS or HTML attributes
  • Improves table rendering performance by applying styles at column level

Basic Syntax


Examples

Example 1: Basic Column Styling

This example shows how to apply different background colors to table columns.

Play Code


Example 2: Column Spanning

This example demonstrates how to use the span attribute to apply styles to multiple columns.

Play Code


Example 3: Column Width Control

This example shows how to set different widths for table columns.

Play Code


Example 4: JavaScript Column Highlight

This example uses JavaScript to highlight a column when hovered.

Play Code


Example 5: Dynamic Column Styling

This example uses JavaScript to dynamically change column styles.

Play Code


Tips and Tricks

  • Use <colgroup> for consistent column styling rather than styling individual cells
  • Combine with CSS classes for more maintainable code
  • Remember that <colgroup> only affects visual presentation, not table structure
  • For complex tables, consider using multiple <colgroup> elements
  • Use JavaScript with <colgroup> to create interactive column features


Browser Compatibility

The <colgroup> element is supported by all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It has been part of HTML since HTML 3.2.

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...