
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- 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
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM ColumnGroup span Property
The HTML DOM ColumnGroup span property is associated with the span attribute of the HTML <colgroup> element. The ColumnGroup span property set or returns the value of the span attribute of a column group. The span attribute is used to define the number of columns a <colgroup> element should span to.
Syntax
Following is the syntax for −
Setting the ColumnGroup span property −
columngroupObject.span = number
Here, the number specifies the number of columns the <colgroup> element should span to.
Example
Let us look at an example for the ColumnGroup span property −
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid blue; } </style> </head> <body> <table> <colgroup id="Colgroup1"></colgroup> <tr> <th>Fruit</th> <th>COLOR</th> <th>Price</th> </tr> <tr> <td>watermelon</td> <td>dark green</td> <td>40Rs</td> </tr> <tr> <td>papaya</td> <td>yellow</td> <td>30Rs</td> </tr> </table> <p>lick the button to change the background color of the first two columns. <button onclick="changeColor()">CHANGE</button> <script> function changeColor() { document.getElementById("Colgroup1").span = "2"; document.getElementById("Colgroup1").style.backgroundColor = "lightgreen"; } </script> </body> </html>
Output
This will produce the following output −
On clicking the CHANGE button −
In the above example −
We have created a table with two rows and three columns.There is also some styling applied to the table, th and td elements −
table, th, td { border: 1px solid blue; } <table> <colgroup id="Colgroup1"></colgroup> <tr> <th>Fruit</th> <th>COLOR</th> <th>Price</th> </tr> <tr> <td>watermelon</td> <td>dark green</td> <td>40Rs</td> </tr> <tr> <td>papaya</td> <td>yellow</td> <td>30Rs</td> </tr> </table>
We have then created a button CHANGE that will execute changeColor() method when clicked upon by the user.
<button onclick="changeColor()">CHANGE</button>
The changeColor() function gets the <colgroup> element using the getElementById() method and giving the <colgroup> element id as parameter. It then sets the <colgroup> element span to 2 and changes its background color to green. This makes the first two columns from left green as specified by the span attribute of the <colgroup> element:
function changeColor() { document.getElementById("Colgroup1").span = "2"; document.getElementById("Colgroup1").style.backgroundColor = "lightgreen"; }
- Related Articles
- HTML DOM Column span Property
- HTML DOM ColumnGroup object
- HTML DOM Span object
- HTML DOM tagName Property
- HTML DOM accessKey Property
- HTML DOM activeElement Property
- HTML DOM name Property
- HTML DOM nextElementSibling Property
- HTML DOM nextSibling Property
- HTML DOM nodeName Property
- HTML DOM nodeType Property
- HTML DOM nodeValue Property
- HTML DOM offsetHeight Property
- HTML DOM offsetLeft Property
- HTML DOM offsetParent Property
