Table of Contents
INTRODUCTION
The <colgroup> element is used define a group of one or more columns within a table. It can contain one or more <col> elements.
location: The <colgroup> must be written after any <caption> element, but before any <thead>, <tbody>, <tfoot>, and <tr> elements.
Syntax:
<table>
<caption> // text </caption>
<colgroup>
<col span="Number" />
</colgroup>
</table>
<table>
<colgroup>
<col span="1" style="background-color: greenyellow;">
<col span="2" style="background-color: lightyellow;">
</colgroup>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice</td>
<td>30</td>
<td>USA</td>
</tr>
<tr>
<td>Bob</td>
<td>25</td>
<td>UK</td>
</tr>
</tbody>
</table>
Output:
Name | Age | Country |
---|---|---|
Alice | 30 | USA |
Bob | 25 | UK |
ATTRIBUTES
1. span
The span atribute Specifies how many columns in a row the <colgroup> element spans. The value must be a whole number greater than zero, and if it's not set, the default is 1. When span is used, you should not use <col> inside that <colgroup>.
example:: In this example the first <colgroup> element spans 2 columns and second <colgroup> element spans 1 column.
<table border="1">
<colgroup span="2" style="background-color: lawngreen;"></colgroup>
<colgroup span="1" style="background-color: lightyellow;"></colgroup>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alice</td>
<td>30</td>
<td>New York</td>
</tr>
<tr>
<td>Bob</td>
<td>25</td>
<td>London</td>
</tr>
</tbody>
</table>
Name | Age | City |
---|---|---|
Alice | 30 | New York |
Bob | 25 | London |
Deprecated Attributes
The following attribute are Deprecated and is advised to not use them. They are mentioned just for reference for updating legacy code or for historical interest.
- align
- bgcolor
- char
- charoff
- valign
- width
Usage Guidelines
- The col tag can only be used inside a <colgroup> tag, and that <colgroup> must not have a span attribute.
- Only a few CSS properties work with <colgroup>, they are background, border, visibility, width. Other properties like font-weight or text-align won't have any effect.