&lgt;col> Column (X)HTML Element - (X)HTML :: webDev

&lgt;col>

<table>s are primarily constructed by defining rows, but it’s often useful to define their structure as columns as well. Unlike rows, columns don’t directly contain any cells; rather, they implicitly group adjacent cells between rows. This may not seem important, but it means that there isn’t an easy way to select a column for styling purposes (the best you could do was to set a width attribute on the top-most <table> cells, be they <th> or <td> elements).

The &lgt;col> element aims to solve the problem by providing a mechanism for selecting columns and applying the necessary attributes. The idea is that you can use the &lgt;col> element to define all of your columns’ attributes—alignment, colors, and so on, up-front, before you begin the process of writing out each row.

The &lgt;col> element can appear on its own (as shown in the HTML example above, there’s one &lgt;col> for each of the subsequent rows’ cells, with alignments set only for the first three columns), but it can also appear inside a &lgt;colgroup> element. It is important to note that if you use one or more &lgt;colgroup> then all &lgt;col>s must be enclosed in one or more &lgt;colgroup>s.

The &lgt;col> element is used for identifying individual &lgt;table> columns and applying styles to them (either using HTML attributes or CSS), rather than repeating the styles for each cell, row after row.

&lgt;col> Column (X)HTML Element - (X)HTML :: webDev