Span attribute on colgroup and col

前端 未结 2 830
眼角桃花
眼角桃花 2021-01-06 18:30

Are these codes logically equivalent?



And


2条回答
  •  一向
    一向 (楼主)
    2021-01-06 18:46

    Here’s my interpretation of the specs. Update: After looking at the HTML4 specs, I’ve changed my mind about the colgroup element's span attribute.

    (This is also in response to my own 2nd question comment in @mu’s answer.)

    A colgroup represents a group of one or more columns, and its span specifies the number of columns in a column group. So I think of it as a shortcut, saving the author from writing multiple col elements in succession.

    
    

    The column group spans over three columns and is styled according to the CSS rule .x {...}. This is equivalent to

    
        
        
        
    
    

    On the other hand, col represents one or more columns in the column group, and its span specifies the number of columns "spanned" by the COL element… which is a cyclical definition if you ask me.

    The only way I can interpret this is that by writing

    
    

    you’re saying there are three columns, each in the same logical grouping, styled the same way according to .y {...}. This is a shortcut to writing

    
        
        
        
    
    

    Presentationally, I’m not sure there would be a noticeable difference. How it all looks depends on your CSS of course. But semantically, they are very different. The first example represents three groups of columns with each group containing one column, whereas the second example represents one group of three columns.

    After rethinking this, I’ve decided that they’re both the same. Having a colgroup span n number of columns is the same as having one colgroup with a col child that spans n columns. There is no logical or semantic difference in my opinion.

    Note: the col element must be contained in a colgroup element that has no span attribute. It may not be a direct child of the table element.

提交回复
热议问题