【问题标题】:How is a CSS "display: table-column" supposed to work?CSS“显示:表格列”应该如何工作?
【发布时间】:2011-11-28 20:47:20
【问题描述】:

鉴于以下 HTML 和 CSS,我在浏览器中完全看不到任何内容(Chrome 和 IE 在撰写本文时是最新的)。一切都崩溃到 0x0 像素。为什么?

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        section { display: table; height: 100%; background-color: grey; }

        #colLeft { display: table-column; height: 100%; background-color: green; }
        #colRight { display: table-column; height: 100%; background-color: red; }

        #row1 { display: table-row; height: 100%; }
        #row2 { display: table-row; height: 100%; }
        #row3 { display: table-row; height: 100%; }

        #cell1 { display: table-cell; height: 100%; }
        #cell2 { display: table-cell; height: 100%; }
        #cell3 { display: table-cell; height: 100%; }
    </style>
</head>
<body>
    <section>
        <div id="colLeft">
            <div id="row1">
                <div id="cell1">
                    AAA
                </div>
            </div>
            <div id="row2">
                <div id="cell2">
                    BBB
                </div>
            </div>
        </div>
        <div id="colRight">
            <div id="row3">
                <div id="cell3">
                    CCC
                </div>
            </div>
        </div>
    </section>
</body>
</html>

【问题讨论】:

    标签: css tablelayout css-tables


    【解决方案1】:

    “table-column”显示类型意味着它的行为类似于 HTML 中的 &lt;col&gt; 标签 - 即一个不可见元素,其宽度* 控制封闭表格的相应物理列的宽度。

    有关 CSS 表格模型的更多信息,请参阅 the W3C standard

    * 以及一些其他属性,例如边框、背景。

    【讨论】:

    • 如果col 元素具有逻辑结构,它确实如此,那么如何使用CSS 规则display: table-column; 来证明其合理性,它只有表现结构?根据display (w3.org/wiki/CSS/Properties/display) 的规范,它应该像col 元素一样“表现”。但我看不出这有什么用......
    • @TestSubject528491 因为这样您就可以设置 [presentational] 表的列背景、列宽等。但实际上,它在为 &lt;col&gt; 标签本身或不同的基于 XML 的标记语言中的等效标签指定默认样式规则方面非常有用。
    • @chharvey:HTML 的 col 是一个 logical 元素,它包含 only presentational 样式。你不能在里面放一列信息——这是一个常见的误解。表数据总是在行中。 display: table-column; 将逻辑元素(通常是 div)转换为 col。它使分配给该元素的其他样式字段应用于表示概念“列”。结果是一个不显示的逻辑元素,其内容(如果有)将被忽略;它的唯一作用是将样式应用于相关演示文稿的“列”。请参阅我对模板的回答。
    【解决方案2】:

    CSS 表格模型基于 HTML 表格模型 http://www.w3.org/TR/CSS21/tables.html

    一个表被分成ROWS,每一行包含一个或多个单元格。单元格是 ROWS 的子级,它们绝不是列的子级。

    “display: table-column”不提供用于制作列式布局的机制(例如,具有多列的报纸页面,其中内容可以从一列流到下一列)。

    相反,“table-column”仅设置应用于表格行中相应单元格的属性。例如。可以描述为“每行第一个单元格的背景颜色为绿色”。

    表格本身的结构始终与 HTML 相同。

    在 HTML 中(注意“td”在“tr”内,而不是在“col”内):

    <table ..>
      <col .. />
      <col .. />
      <tr ..>
        <td ..></td>
        <td ..></td>
      </tr>
      <tr ..>
        <td ..></td>
        <td ..></td>
      </tr>
    </table>
    

    使用 CSS 表格属性的对应 HTML(注意“列”div 不包含任何内容——标准不允许内容直接在列中):

    .mytable {
      display: table;
    }
    .myrow {
      display: table-row;
    }
    .mycell {
      display: table-cell;
    }
    .column1 {
      display: table-column;
      background-color: green;
    }
    .column2 {
      display: table-column;
    }
    <div class="mytable">
      <div class="column1"></div>
      <div class="column2"></div>
      <div class="myrow">
        <div class="mycell">contents of first cell in row 1</div>
        <div class="mycell">contents of second cell in row 1</div>
      </div>
      <div class="myrow">
        <div class="mycell">contents of first cell in row 2</div>
        <div class="mycell">contents of second cell in row 2</div>
      </div>
    </div>


    可选:“行”和“列”都可以通过为每个行和单元格分配多个类来设置样式,如下所示。这种方法在指定要设置样式的各种单元格集或单个单元格方面提供了最大的灵活性:

    //Useful css declarations, depending on what you want to affect, include:
    
    /* all cells (that have "class=mycell") */
    .mycell {
    }
    
    /* class row1, wherever it is used */
    .row1 {
    }
    
    /* all the cells of row1 (if you've put "class=mycell" on each cell) */
    .row1 .mycell {
    }
    
    /* cell1 of row1 */
    .row1 .cell1 {
    }
    
    /* cell1 of all rows */
    .cell1 {
    }
    
    /* row1 inside class mytable (so can have different tables with different styles) */
    .mytable .row1 {
    }
    
    /* all the cells of row1 of a mytable */
    .mytable .row1 .mycell {
    }
    
    /* cell1 of row1 of a mytable */
    .mytable .row1 .cell1 {
    }
    
    /* cell1 of all rows of a mytable */
    .mytable .cell1 {
    }
    <div class="mytable">
      <div class="column1"></div>
      <div class="column2"></div>
      <div class="myrow row1">
        <div class="mycell cell1">contents of first cell in row 1</div>
        <div class="mycell cell2">contents of second cell in row 1</div>
      </div>
      <div class="myrow row2">
        <div class="mycell cell1">contents of first cell in row 2</div>
        <div class="mycell cell2">contents of second cell in row 2</div>
      </div>
    </div>

    在当今灵活的设计中,&lt;div&gt; 用于多种用途,明智的做法是在每个 div 上放置 some 类,以帮助引用它。在这里,以前 HTML 中的&lt;tr&gt; 变成了class myrow,而&lt;td&gt; 变成了class mycell。这种约定使上述 CSS 选择器很有用。

    性能说明:将类名放在每个单元格上,并使用上述多类选择器,比使用以* 结尾的选择器(例如.row1 * 甚至@987654336)性能更好@。原因是选择器是last first匹配的,所以在寻找匹配元素时,.row1 *首先匹配*,匹配所有元素,然后检查每个元素的所有祖先,查找是否有任何祖先有class row1。这在慢速设备上的复杂文档中可能会很慢。 .row1 &gt; * 更好,因为只检查直接父级。但是最好还是通过.row1 .cell1 立即消除大多数元素。 (.row1 &gt; .cell1 是一个更严格的规范,但它是搜索的第一步,这是最大的不同,所以通常不值得混乱,以及关于它是否永远是一个直接的孩子的额外思考过程,添加子选择器&gt;。)

    重新性能的关键是选择器中的最后一个项应该尽可能具体,并且不应该是*

    【讨论】:

    • 我自己没有尝试过,但我看到了tanalin.com/en/projects/display-table-htc 的建议,这是一个适用于 IE6 和 IE7 的 JavaScript 解决方案。当 javascript 运行时,它会将页面转换为旧的 HTML 表格标签。
    • 解决方案适用于 IE8+、Firefox、Chrome、iPad、Android。如果您需要灵活的表格结构并且支持上面列出的更现代的浏览器,这是避免表格布局的好方法。
    • &lt;col style="color: red;" &gt; 不行,但&lt;col style="background-color: red;" &gt; 没问题!这有什么问题?
    • @xgqfrms:其他一些 css 规则必须覆盖颜色。可能是应用于单元格内元素的规则。例如,如果您的文本在

      内,那么在某处您有一个 更具体 规则设置

      颜色。试试&lt;col style="color: red !important;"&gt;。如果这有效,那么这就是问题所在。 不过,不要养成过度使用!important 的习惯。看到这项工作后,最好将其删除,并找出更具体的选择器优先。 google "css 更具体的选择器",或查看smashingmagazine.com/2007/07/…

    猜你喜欢
    • 2011-07-04
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 2013-03-10
    相关资源
    最近更新 更多