- HTML Tutorial
- HTML - Home
- HTML - Introduction
- 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 - Iframes
- HTML - Phrase Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- 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
- HTML - HEX
- HTML - HSL
- 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 - 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 - 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
In HTML, the <colgroup> element is used to group together a set of columns so that a combined styles and scripts can be applied.
HTML <colgroup> Tag
HTML <colgroup> is often used along 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.
Using <colgroup> in HTML involves the following steps:
- Step 1 - Insert <colgroup> Tag: Place the <colgroup> tag within the <table> element, usually inside the <thead> (table head) or <tbody> (table body) section.
- Step 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.
- Step 3 - Apply Attributes or Styles: Define attributes or styles for the columns by adding attributes such as width, style, or class to the <col> tags.
Examples of Table Colgroup
Below examples will illustrate the Colgroup of HTML table, where and how we should use this property of HTML.
Using Colgroup in a table
The following code show how to use Colgroup inside a table element to style columns. 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>
Applying CSS to Columns using Colgroup
In HTML, the <colgroup> element facilitate the application of specific CSS properties to enhance the presentation of table columns.
<!DOCTYPE html> <html lang="en"> <head> <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 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>
Empty Column Groups
An empty <colgroup> can be used to provide a structural placeholder for potential styling or later use. If cols is not mentioned then styles will be applied only to first column of table. Look at the following code.
<!DOCTYPE html> <html lang="en"> <head> <style> colgroup { background-color: red; border: 2px solid black; } </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>
Legal CSS Properties of cologroup
There are certain CSS properties that are allowed to be used on the cologroup element, other properties will have no effect on this element.
- CSS width Property: The width property sets the width of an element's content area.
- CSS visibility Property: CSS visibility property allows you to show or hide an element without changing the layout of a document, while hidden elements take up space.
- CSS background Property: The background property of CSS is used to set the background of an element.
- CSS border Property: The border property is used to create a border around an element, such as a div, image, or text.
The <colgroup> tag must be a child of a <table> element and should be placed before any other table elements, like <thead>, <tr>, <td>, <th> etc., but after the <caption> element, if present.