html - 使用显示: table; in CSS?时colspan的模拟是什么

标签 html css css-tables

这个任务很奇怪。我必须创建 html 表但是 我不允许使用传统的 <table>标签。我的表应该是这样的:enter image description here

像下面这样很容易做到:

<table>
     <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
     </tr>
     <tr>
         <td colspan="5"></td>
     </tr>
     ...

但是,正如我所说,我不允许使用传统的表格标签( tabletrtdth )。这是 JSFIddle我现在所拥有的。我怎样才能得到与 <td colspan="5"></td> 相同的结果? 但是仅使用 div 和 CSS。

编辑:
* 表格单元格在一行中的宽度不能固定,它应该是动态的,并且应该可以使它们(单元格)在一行中具有不同的宽度。
* 表格单元格在同一列不同行的宽度必须相等。就像在传统的 table 上一样。只有“colspanned”单元格的宽度必须不同。

最佳答案

CSS 2.1 规范中 "17.5 Visual layout of table contents" 部分所述

Cells may span several rows or columns. (Although CSS 2.1 does not define how the number of spanned rows or columns is determined ...

所以答案很简单!不要认为 CSS tables 与 HTML tables 完全一样。由于存在一些差异,如 "17.2.1 Anonymous table objects" 中提到的:

... the "missing" elements must be assumed in order for the table model to work. Any table element will automatically generate necessary anonymous table objects around itself, consisting of at least three nested objects corresponding to a 'table'/'inline-table' element, a 'table-row' element, and a 'table-cell' element. ...

所以你可以这样做(每行作为一个表并删除表行以避免不必要的 div block )直到他们指定一种定义跨行或跨列数的方法:

CSS

.row {
    display: table;
    width: 100%;
}
.cell {
    display: table-cell;
    width: 16.67%;
}
.wideCell {
    display: table-cell;
}

HTML

<div class="row">
    <div class="cell">One</div>
    <div class="cell">Two</div>
    <div class="cell">Three</div>
    <div class="cell">Four</div>
    <div class="cell">Five</div>
    <div class="wideCell">Six</div>
</div>
<div>One Two Three Four Five Six</div>
<div class="row">
    <div class="cell">One</div>
    <div class="cell">Two</div>
    <div class="cell">Three</div>
    <div class="cell">Four</div>
    <div class="cell">Five</div>
    <div class="wideCell">Six</div>
</div>

关于html - 使用显示: table; in CSS?时colspan的模拟是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15496708/

相关文章:

html - div 之间的垂直线

jquery - FineUploader 自定义 fileTemplate 选项

html - CSS Inline-table 添加底部边距

html - Google Chrome 中表格行的 CSS 背景问题

python - Django:如何从 .css 文件链接到静态文件目录?

jquery - 如何禁用富人的启动画面:extendedDataTable

javascript - jQuery 插件 - 高亮文本

javascript - 淡入淡出的背景横幅图像,自动调整为当前窗口宽度

html - 如何使用 DIV UL LI 制作 CSS 表格

javascript - 嵌套 <div> 的 Jquery 滚动函数