/

HTML / CSS / JavaScript Tutorial

HTML Element: <colgroup>

[this page | pdf | back links]

The HTML <colgroup> element specifies a group of one or more columns in a table. It can be used to apply styles to entire columns in a table. It must be a child of a <table> element, positioned after any <caption> elements and before any <tbody>, <tfoot>, <thead> and <tr> elements. If you want to define different styles to column within the column group then use the <col> element.

 

The attributes it can take (other than HTML global attributes and HTML event attributes) include:

 

Attribute

Description

More

span

Number of columns to span

Here

 

To create or access such an element in JavaScript see here. The corresponding HTML DOM object supports standard DOM properties and methods, and additional properties with the same name and meaning as the attributes of the underlying HTML element referred to above. The default style applicable to this element is shown here.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table, th, td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
Created using HTML:<br>
<table>
  <colgroup>
    <col span="2" style="background-color: yellow">
    <col style="background-color: orange">
  </colgroup>
  <tr><td>1</td><td>2</td><td>3</td></tr>
</table>

<br><br>Created using JavaScript:<br>
<table id="tbl">
  <tr><td>1</td><td>2</td><td>3</td></tr>
</table>

<script>
var x = document.createElement("COLGROUP");
var x1 = document.createElement("COL");
var x2 = document.createElement("COL");
x1.setAttribute("span","2");
x1.setAttribute("style","background-color: yellow");
x2.setAttribute("style","background-color: orange");
x.appendChild(x1); x.appendChild(x2);
document.getElementById("tbl").appendChild(x);
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | HTML Elements


Desktop view | Switch to Mobile