
- HTML - Home
- HTML - Roadmap
- HTML - Introduction
- HTML - History & Evolution
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - Table Colgroup
HTML Table Colgroup
In HTML, the <colgroup> element is used to define a group of columns in a table. It allows you to apply properties to multiple columns simultaneously, providing a more efficient way to style or format columns.

The <colgroup> Tag
The <colgroup> is often used in conjunction with the <col> element, where each <col> tag represents an individual column within the group. This grouping enhances readability and simplifies the application of styles or attributes to specific columns in a table.
Syntax
Following is the syntax to use <colgroup> with <table> tag:
<table> <colgroup> <col span="value" style="width: ...;"> <col style="..."> <!-- More <col> elements... --> </colgroup> <!-- Other table elements such as <thead>, <tbody>, ... --> </table>
Using <colgroup> Tag in HTML Table
Using <colgroup> in HTML involves the following steps −
1. Insert <colgroup>
Tag
Place the <colgroup>
tag within the <table>
element, usually inside the <thead>
(table head) or <tbody>
(table body) section.
<table> <colgroup> <!-- Column definitions --> </colgroup> <thead> <!-- Table headers --> </thead> <tbody> <!-- Table rows --> </tbody> </table>
2. Define Columns
Inside the <colgroup>
tag, use one or more <col>
tags to represent each column. Specify attributes or styles for the columns within these <col>
tags.
<table> <colgroup> <col> <col> <col> </colgroup> <!-- Table content --> </table>
3. Apply Attributes or Styles
Define attributes or styles for the columns by adding attributes such as span
, width
, style
, or class
to the <col>
tags.
<table> <colgroup> <col style="background-color: lightgrey;" span="2"> <!-- First two columns --> <col style="background-color: lightblue;"> <!-- Third column --> </colgroup> <thead> <tr> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> </tbody> </table>
Example of HTML Table Colgroup
In this example, the <colgroup> tag defines two columns with different widths, and the styles are applied to the columns using the `<col>` tags. The second row in the table is highlighted using a CSS class.
<!DOCTYPE html> <html> <body> <table border=1> <colgroup> <col style="width: 30%;"> <col style="width: 70%;"> </colgroup> <thead> <tr> <th>Column 1</th> <th>Column 2</th> </tr> </thead> <tbody> <tr> <td>Row 1, Col 1</td> <td>Row 1, Col 2</td> </tr> <tr class="highlight"> <td>Row 2, Col 1</td> <td>Row 2, Col 2</td> </tr> </tbody> </table> </body> </html>
CSS Properties for <colgroup> Tag
In HTML, the <colgroup> element allows some specific CSS properties to enhance the presentation of table columns. The legal CSS properties that can be used within a <colgroup> are as follows −
width Property − This property sets the width of the columns within the <colgroup>. It allows you to define the relative or absolute width of each column.
visibility Property − The visibility property can be used to control the visibility of columns within the <colgroup>. You can set it to "hidden" to make a column invisible.
Background Properties − Background properties, such as background-color, can be applied to add background styling to the columns. This can enhance the visual appeal of the table.
Border Properties − Border properties, like border-color and border-width, enable the customization of borders around the columns. This is useful for creating well-defined visual boundaries.
Attempting to apply other CSS properties will have no impact on the styling of the table columns. Therefore, when styling tables with <colgroup>, focus on the available properties to achieve the desired layout and appearance.
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> table { width: 100%; border-collapse: collapse; } colgroup { /* Setting width for columns */ width: 20%; background-color: #f0f0f0; /* Background color for columns */ visibility: visible; /* Making columns visible */ border: 2px solid #3498db; /* Border around columns */ } col { /* Additional styling for individual columns */ background-color: #ecf0f1; border: 1px solid #bdc3c7; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } </style> </head> <body> <table> <colgroup> <col> <col style="width: 30%;"> <col> </colgroup> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> </tbody> </table> </body> </html>
Multiple Col Elements
Certainly! The <colgroup> element in HTML allows you to group a set of columns in a table and apply styles to them collectively. Within <colgroup>, you can use multiple <col> elements to define different styles for individual columns.
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> col { /* Additional styling for individual columns */ background-color: #ecf0f1; border: 1px solid #bdc3c7; } </style> </head> <body> <table border=5> <colgroup> <col style="width: 20%;"> <col style="width: 30%;"> <col style="width: 50%;"> </colgroup> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> </tbody> </table> </body> </html>
The <colgroup> contains three <col> elements, each with a specific 'width' style, defining the width of individual columns.
Empty Column groups
In HTML, a <colgroup> element can be used to define a group of columns in a table. An empty <colgroup> can be employed to provide a structural placeholder for potential styling or later use. While it doesn't contain explicit <col> elements, it can still influence the overall structure of the table.
Example
Here's a simple example demonstrating the use of an empty <colgroup>. In here, the <colgroup> is empty but serves as a placeholder for potential styling. The entire <colgroup> is styled with a background color and a border. The <col> elements are not explicitly used, but their styling can be defined within <colgroup> for future use or consistency in the structure.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> colgroup { /* Styling for the colgroup (can be empty) */ background-color: #f0f0f0; /* Background color for the entire colgroup */ border: 2px solid #3498db; /* Border around the entire colgroup */ } /* Additional styling for individual columns */ col { background-color: #ecf0f1; border: 1px solid #bdc3c7; } </style> </head> <body> <table border=3> <colgroup></colgroup> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 4</td> <td>Data 5</td> <td>Data 6</td> </tr> </tbody> </table> </body> </html>