Search code examples
htmlcolcolgroup

Span attribute on colgroup and col


Are these codes logically equivalent?

<colgroup span="7">
</colgroup>

And

<col span="7" />

And

<colgroup>
<col />
<col />
<col />
<col />
<col />
<col />
<col />
</colgroup>

Will any attributes via HTML or properties via CSS have equal effect? Can sombody also add "colgroup" Tag. No enough rep for me to do that.


Solution

  • From the specification for <col>:

    Contexts in which this element can be used:
    As a child of a colgroup element that doesn't have a span attribute.
    [...]
    Content attributes: Global attributes
    span

    I read that as saying that just <col span="7" /> on its own is invalid but this:

    <colgroup>
        <col span="7" />
    </colgroup>
    

    is valid and the same as:

    <colgroup span="7">
    </colgroup>
    

    However, if the <colgroup> has a span attribute, then it should not have <col> children:

    If the colgroup element contains no col elements, then the element may have a span content attribute specified...

    My interpretation (based on the HTML4 specification more than the thinner HTML5 one) is that you would usually use <colgroup span="n"> unless you needed to style one of the columns in the group differently as in this (modified) example from the HTML4 specification:

    <colgroup style="width: 20px; font-weight: bold;">
        <col span="39">
        <col id="format-me-specially" style="width: 35px;">
    </colgroup>
    

    so the first 39 columns would use whatever the <colgroup> specifies but the 40th could be tweaked. OTOH, I'm having trouble getting browsers to pay much attention to any of this (despite what the specs say) on jsfiddle.net so YMMV.