2024-07-26 07:00:00

HTML colspan在CSS

我试图构造一个类似于下面的布局:

+---+---+---+
|   |   |   |
+---+---+---+
|           |
+-----------+

底部填充上一行的空间。

如果这是一个实际的表,我可以使用<td colspan="3">轻松完成,但由于我只是创建一个类似于表的布局,所以不能使用<table>标记。这是可能的使用CSS?


当前回答

您可以尝试使用像http://960.gs/这样的网格系统

你的代码应该是这样的,假设你使用的是“12列”布局:

<div class="container_12">
<div class="grid_4">1</div><div class="grid_4">2</div><div class="grid_4">3</div>
<div class="clear"></div>
<div class="grid_12">123</div>
</div>

其他回答

<div style="width: 100%;">
    <div style="float: left; width: 33%;">Row 1 - Cell 1</div>
    <div style="float: left; width: 34%;">Row 1 - Cell 2</div>
    <div style="float: left; width: 33%;">Row 1 - Cell 3</div>
</div>
<div style="clear: left; width: 100%;">
Row 2 - Cell 1
</div>

提供一个最新的答案:今天最好的方法是使用css网格布局,像这样:

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: auto;
  grid-template-areas: 
    "top-left top-middle top-right"
    "bottom bottom bottom"
}

.item-a {
  grid-area: top-left;
}
.item-b {
  grid-area: top-middle;
}
.item-c {
  grid-area: top-right;
}
.item-d {
  grid-area: bottom;
}

以及HTML

<div class="container">
  <div class="item-a">1</div>
  <div class="item-b">2</div>
  <div class="item-c">3</div>
  <div class="item-d">123</div>
</div>

我已经取得了一些成功,尽管它依赖于一些属性来工作:

表布局:固定 border-collapse:独立

以及单元格宽度,即4个单元格的宽度为25%:

.div-table-cell, * { box-sizing: border-box; } .div-table { display: table; border: solid 1px #ccc; border-left: none; border-bottom: none; table-layout: fixed; margin: 10px auto; width: 50%; border-collapse: separate; background: #eee; } .div-table-row { display: table-row; } .div-table-cell { display: table-cell; padding: 15px; border-left: solid 1px #ccc; border-bottom: solid 1px #ccc; text-align: center; background: #ddd; } .colspan-3 { width: 300%; display: table; background: #eee; } .row-1 .div-table-cell:before { content: "row 1: "; } .row-2 .div-table-cell:before { content: "row 2: "; } .row-3 .div-table-cell:before { content: "row 3: "; font-weight: bold; } .div-table-row-at-the-top { display: table-header-group; } <div class="div-table"> <div class="div-table-row row-1"> <div class="div-table-cell">Cell 1</div> <div class="div-table-cell">Cell 2</div> <div class="div-table-cell">Cell 3</div> </div> <div class="div-table-row row-2"> <div class="div-table-cell colspan-3"> Cor blimey he's only gone and done it. </div> </div> <div class="div-table-row row-3"> <div class="div-table-cell">Cell 1</div> <div class="div-table-cell">Cell 2</div> <div class="div-table-cell">Cell 3</div> </div> </div>

https://jsfiddle.net/sfjw26rb/2/

此外,应用display:table-header-group或table-footer-group是将'row'元素跳转到'table'的顶部/底部的便捷方法。

这不是CSS权限的一部分。colspan描述页面内容的结构,或者为表中的数据赋予一些含义,这是HTML的工作。

尝试添加display: table-cell;宽度:1%;到您的表格单元格元素。

.table-cell { display: table-cell; width: 1%; padding: 4px; border: 1px solid #efefef; } <div class="table"> <div class="table-cell">one</div> <div class="table-cell">two</div> <div class="table-cell">three</div> <div class="table-cell">four</div> </div> <div class="table"> <div class="table-cell">one</div> <div class="table-cell">two</div> <div class="table-cell">three</div> <div class="table-cell">four</div> </div> <div class="table"> <div class="table-cell">one</div> </div>