- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- 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 - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- 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 Code Builder
- HTML References
- 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 - <colgroup> Tag
HTML <colgroup> tag is used to describe the collection of one or more columns in a table for formatting. Instead of duplicating the styles for each column and each row, it is excellent for applying styles to full columns. To declare distinct characteristics for a column use the <col> tag within the <colgroup> tag. The <colgroup> tag must be a child of the <table> element, coming after any <caption> elements and before any <thead>, <tbody>, <tfoot>, and <tr> elements.
Syntax
Following is the syntax for HTML <colgroup> tag −
<colgroup>...</colgroup>
Example
Following is an example where we are going to use the <colgroup> tag in the table.
<!DOCTYPE html> <html> <body> <table border="1"> <colgroup> <col style="background-color:#ABEBC6"> <col style="background-color:#BB8FCE"> </colgroup> <tr> <th>Roll.No</th> <th>Name</th> </tr> <tr> <td>1</td> <td>Maya</td> </tr> <tr> <td>2</td> <td>Surya</td> </tr> <tr> <td>3</td> <td>Ram</td> </tr> </table> </body> </html>
When we execute the above code, it will generate an output consisting of a table displayed on the webpage applied with CSS.
Example
Consider another scenario where we are going to use the span attribute with the <colgroup> tag.
<!DOCTYPE html> <html> <style> table, th, td { border: 1.5px solid #DE3163; } </style> <body> <table> <colgroup> <col span="2" style="background-color:#F9E79F"> </colgroup> <tr> <th>Item</th> <th>Price</th> </tr> <tr> <td>5-Star</td> <td>$10</td> </tr> <tr> <td>Dairy-Milk</td> <td>$50</td> </tr> <tr> <td>Kitkat</td> <td>$20</td> </tr> </table> </body> </html>
On running the above code, the output window will pop up, displaying the table along with the CSS applied to the two columns mentioned using the span attribute displayed on the webpage.
Example
In the following example, we are going to apply CSS to the middle of the table by passing an empty <col> without applying the style for the column before.
<!DOCTYPE html> <html> <style> table, th, td { border: 1.5px solid #85C1E9; border-collapse: collapse; } </style> <body> <table style="width: 50%;"> <colgroup> <col span="1"> <col span="1" style="background-color: #ABEBC6 "> </colgroup> <tr> <th>MON</th> <th>TUE</th> <th>WED</th> <th>THU</th> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> <tr> <td>9</td> <td>10</td> <td>11</td> <td>12</td> </tr> </table> </body> </html>
When we execute the above code, it will generate an output consisting of the table along with CSS applied in the middle of the table by using the 1st vacant column displayed on the webpage.
Example
Let’s look at another example where we are going to hide the column using the visibility:collapse property.
<!DOCTYPE html> <html> <style> table, th, td { border: 1.5px solid #DE3163; border-collapse: collapse; } </style> <body> <table style="width: 100%;"> <colgroup> <col span="2"> <col span="3" style="visibility: collapse"> </colgroup> <tr> <th>ID</th> <th>Employee</th> <th>Department</th> <th>Age</th> </tr> <tr> <td>112</td> <td>Rahul</td> <td>IT</td> <td>22</td> </tr> <tr> <td>113</td> <td>Ram</td> <td>Associate</td> <td>24</td> </tr> <tr> <td>114</td> <td>Maya</td> <td>HR</td> <td>30</td> </tr> <tr> <td>115</td> <td>Rahul</td> <td>BPO</td> <td>23</td> </tr> </table> </body> </html>
On running the above code, the output window will pop up, consisting of the table along with some of its columns collapsed by using the visibility property displayed on the webpage.