HTML colspan在CSS

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

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

底部填充上一行的空间。

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

480623 次浏览
<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权限的一部分。colspan描述页面内容的结构,或者为表中的数据赋予一些含义,这是HTML的工作。

你可以尝试使用像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>

colspan没有简单、优雅的CSS模拟。

对这个问题的搜索将返回各种解决方案,其中包括一系列替代方案,包括绝对定位、大小,以及类似的各种浏览器和特定环境的警告。阅读,并根据你的发现做出最明智的决定。

据我所知,css中没有colspan,但在不久的将来会有用于多列布局的column-span,但由于它只是CSS3中的草稿,您可以在在这里中检查它。不管怎么说,你可以使用divspan这样的表格显示来解决问题。

这就是HTML:

<div class="table">
<div class="row">
<span class="cell red first"></span>
<span class="cell blue fill"></span>
<span class="cell green last"></span>
</div>
</div>
<div class="table">
<div class="row">
<span class="cell black"></span>
</div>
</div>

这就是css:

  /* this is to reproduce table-like structure
for the sake of table-less layout. */
.table { display:table; table-layout:fixed; width:100px; }
.row { display:table-row; height:10px; }
.cell { display:table-cell; }


/* this is where the colspan tricks works. */
span { width:100%; }


/* below is for visual recognition test purposes only. */
.red { background:red; }
.blue { background:blue; }
.green { background:green; }
.black { background:black; }


/* this is the benefit of using table display, it is able
to set the width of it's child object to fill the rest of
the parent width as in table */
.first { width: 20px; }
.last { width: 30px; }
.fill { width: 100%; }

使用这个技巧的唯一原因是为了获得table-layout行为的好处,如果仅仅设置div和span宽度为一定的百分比并不能满足我们的设计要求,我就会使用它。

但是如果你不需要从table-layout行为中获益,那么durilai的回答就足够适合你了。

你总是可以position:absolute;的东西并指定宽度。这不是一个很流畅的方法,但这是可行的。

如果使用div和span,当datagrid-table行体积更大时,它将占用更多的代码大小。下面的代码在所有浏览器中都进行了检查

HTML:

<div id="gridheading">
<h4>Sl.No</h4><h4 class="big">Name</h4><h4>Location</h4><h4>column</h4><h4>column</h4><h4>column</h4><h4>Amount(Rs)</h4><h4>View</h4><h4>Edit</h4><h4>Delete</h4>
</div>
<div class="data">
<h4>01</h4><h4 class="big">test</h4><h4>TVM</h4><h4>A</h4><h4>I</h4><h4>4575</h4><h4>4575</h4></div>
<div class="data">
<h4>01</h4><h4 class="big">test</h4><h4>TVM</h4><h4>A</h4><h4>I</h4><h4>4575</h4><h4>4575</h4></div>

CSS:

#gridheading {
background: #ccc;
border-bottom: 1px dotted #BBBBBB;
font-size: 12px;
line-height: 30px;
text-transform: capitalize;
}
.data {
border-bottom: 1px dotted #BBBBBB;
display: block;
font-weight: normal;
line-height: 20px;
text-align: left;
word-wrap: break-word;
}
h4 {
border-right: thin dotted #000000;
display: table-cell;
margin-right: 100px;
text-align: center;
width: 100px;
word-wrap: break-word;
}
.data .big {
margin-right: 150px;
width: 200px;
}

Media Query类可用于实现带有重复标记的可传递内容。下面是我使用bootstrap的方法:

  <tr class="total">
<td colspan="1" class="visible-xs"></td>
<td colspan="5" class="hidden-xs"></td>
<td class="focus">Total</td>
<td class="focus" colspan="2"><%= number_to_currency @cart.total %></td>
</tr>

colspan 1用于移动,colspan 5用于其他使用CSS的工作。

我创造了这把小提琴:

enter image description here

http://jsfiddle.net/wo40ev18/3/

超文本标记语言

<div id="table">
<div class="caption">
Center Caption
</div>
<div class="group">
<div class="row">
<div class="cell">Link 1t</div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell ">Link 2</div>
</div>
</div>

CSS

   #table {
display:table;
}


.group {display: table-row-group; }


.row {
display:table-row;
height: 80px;
line-height: 80px;
}


.cell {
display:table-cell;
width:1%;
text-align: center;
border:1px solid grey;
height: 80px
line-height: 80px;
}


.caption {
border:1px solid red; caption-side: top; display: table-caption; text-align: center;
position: relative;
top: 80px;
height: 80px;
height: 80px;
line-height: 80px;


}

尝试添加display: table-cell; width: 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>

另一个建议是使用flexbox而不是完全使用表格。这当然是一个“现代浏览器”的事情,但拜托,这是2016年;)

至少对于那些现在正在寻找答案的人来说,这可能是另一种解决方案,因为最初的帖子是在2010年发布的。

这里有一个很好的指南:https://css-tricks.com/snippets/css/a-guide-to-flexbox/

.table {
border: 1px solid red;
padding: 2px;
max-width: 300px;
display: flex;
flex-flow: row wrap;
}
.table-cell {
border: 1px solid blue;
flex: 1 30%;
}
.colspan-3 {
border: 1px solid green;
flex: 1 100%;
}
<div class="table">
<div class="table-cell">
row 1 - cell 1
</div>
<div class="table-cell">
row 1 - cell 2
</div>
<div class="table-cell">
row 1 - cell 3
</div>
<div class="table-cell colspan-3">
row 2 - cell 1 (spans 3 columns)
</div>
</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>

如果你来这里是因为你必须打开或关闭colspan属性(比如移动布局):

复制__abc0,只显示所需的colspan:

table.colspan--on td.single {
display: none;
}


table.colspan--off td.both {
display: none;
}
<!-- simple table -->
<table class="colspan--on">
<thead>
<th>col 1</th>
<th>col 2</th>
</thead>
<tbody>
<tr>
<!-- normal row -->
<td>a</td>
<td>b</td>
</tr>
<tr>
<!-- the <td> spanning both columns -->
<td class="both" colspan="2">both</td>


<!-- the two single-column <td>s -->
<td class="single">A</td>
<td class="single">B</td>
</tr>
<tr>
<!-- normal row -->
<td>a</td>
<td>b</td>
</tr>
</tbody>
</table>
<!--
that's all
-->


 


<!--
stuff only needed for making this interactive example looking good:
-->
<br><br>
<button onclick="toggle()">Toggle colspan</button>
<script>/*toggle classes*/var tableClasses = document.querySelector('table').classList;
function toggle() {
tableClasses.toggle('colspan--on');
tableClasses.toggle('colspan--off');
}
</script>
<style>/* some not-needed styles to make this example more appealing */
td {text-align: center;}
table, td, th {border-collapse: collapse; border: 1px solid black;}</style>

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

< p > table-layout: fixed border-collapse: separate < / p >

以及单元格宽度,即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属性“column-count”、“column-gap”和“column-span”可以做到这一点,使伪表的所有列保持在相同的包装器中(HTML保持漂亮和整洁)。

唯一需要注意的是,您只能定义1列或所有列,而且column-span在Firefox中还不能工作,因此需要一些额外的CSS来确保它将正确显示。 https://www.w3schools.com/css/css3_multiple_columns.asp < / p >

.split-me {
-webkit-column-count: 3;
-webkit-column-gap: 0;
-moz-column-count: 3;
-moz-column-gap: 0;
column-count: 3;
column-gap: 0;
}


.cols {
/* column-span is 1 by default */
column-span: 1;
}


div.three-span {
column-span: all !important;
}


/* alternate style for column-span in Firefox */
@-moz-document url-prefix(){
.three-span {
position: absolute;
left: 8px;
right: 8px;
top: auto;
width: auto;
}
}




<p>The column width stays fully dynamic, just like flex-box, evenly scaling on resize.</p>


<div class='split-me'>
<div class='col-1 cols'>Text inside Column 1 div.</div>
<div class='col-2 cols'>Text inside Column 2 div.</div>
<div class='col-3 cols'>Text inside Column 3 div.</div>
<div class='three-span'>Text div spanning 3 columns.</div>
</div>






<style>
/* Non-Essential Visual Styles */


html * { font-size: 12pt; font-family: Arial; text-align: center; }
.split-me>* { padding: 5px; }
.cols { border: 2px dashed black; border-left: none; }
.col-1 { background-color: #ddffff; border-left: 2px dashed black; }
.col-2 { background-color: #ffddff; }
.col-3 { background-color: #ffffdd; }
.three-span {
border: 2px dashed black; border-top: none;
text-align: center; background-color: #ddffdd;
}
</style>

我来这里是因为目前WordPress表块不支持colspan参数,我想我会用CSS替换它。这是我的解决方案,假设列的宽度相同:

table {
width: 100%;
}


table td {
width: 50%;
background: #dbdbdb;
text-align: center;
}


table tr:nth-child(2n+1) {
position:relative;
display:block;
height:20px;
background:green;
}


table tr:nth-child(2n+1) td {
position:absolute;
left:0;
right:-100%;
width: auto;
top:0;
bottom:0;
background:red;
text-align:center;
}
<table>
<tr>
<td>row</td>
</tr>
<tr>
<td>cell</td>
<td>cell</td>
</tr>
<tr>
<td>row</td>
</tr>
<tr>
<td>cell</td>
<td>cell</td>
</tr>
</table>