HTML Table Colgroup

In this tutorial, you will learn HTML table colgroup

HTML table colgroup allows you to group columns together and apply styles or attributes to them as a group. Here is a tutorial on how to use colgroup in HTML tables:

  • To create a colgroup, use the <colgroup> element and place it inside the <table> element.
<table>
  <colgroup>
    <!-- columns go here -->
  </colgroup>
  <!-- table rows go here -->
</table>
  • Inside the <colgroup> element, use the <col> element to define the columns.
<colgroup>
  <col>
  <col>
  <col>
</colgroup>
  • You can also use the span attribute on the <col> element to specify how many columns the style should be applied to.
<colgroup>
  <col span="2">
  <col>
</colgroup>
  • To apply styles to the colgroup, you can use the :nth-child(n) selector in CSS.
colgroup col:nth-child(2) {
  background-color: #ddd;
}
  • If you want to set width of the columns in the colgroup, you can use the width attribute on the <col> element.
<colgroup>
  <col width="50px">
  <col width="100px">
  <col width="150px">
</colgroup>
  • You can also use class and id attributes on the <col> elements to target them in CSS
<colgroup>
  <col class="col1">
  <col

 

Scroll to Top