HTML Table Colgroup | Tables
If you want to style the two first columns of a table, use the <colgroup>
and <col>
elements.
MON | TUE | WED | THU | FRI | SAT | SUN |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
The <colgroup>
element should be used as a container for the column specifications.
Each group is specified with a <col>
element.
The span
attribute specifies how many columns that get the style.
The style
attribute specifies the style to give the columns.
Note: There is a very limited selection of legal CSS properties for colgroups.
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Colgroup</title>
<style>
table, th, td {border: 1px solid black;border-collapse: collapse;}
</style>
</head>
<body>
<h2>Colgroup</h2>
<p>Add the a colgroup with a col element that spans over two columns to define a style for the two columns:</p>
<table style="width: 100%;">
<colgroup>
<col span="2" style="background-color: #D6EEEE">
</colgroup>
<tr>
<th>#</th>
<th>Name of Student</th>
<th>Class</th>
</tr>
<tr>
<td>1.</td>
<td>Taranpreet Singh</td>
<td>6th</td>
</tr>
<tr>
<td>2.</td>
<td>Mehakpreet Kaur</td>
<td>7th</td>
</tr>
<tr>
<td>3.</td>
<td>Veerpal Kaur</td>
<td>8th</td>
</tr>
<tr>
<td>4.</td>
<td>Jasmeen</td>
<td>9th</td>
</tr>
<tr>
<td>5.</td>
<td>Sonia</td>
<td>10th</td>
</tr>
<tr>
<td>6.</td>
<td>Jaskaran Singh</td>
<td>11th</td>
</tr>
<tr>
<td>7.</td>
<td>Arshdeep Kaur</td>
<td>12th</td>
</tr>
</table>
</body>
</html>
Note: The <colgroup>
tag must be a child of a <table>
element and should be placed before any other table elements, like <thead>
, <tr>
, <td>
etc., but after the <caption>
element, if present.
Legal CSS Properties
There is only a very limited selection of CSS properties that are allowed to be used in the colgroup:
width
property
visibility
property
background
properties
border
properties
All other CSS properties will have no effect on your tables.