2024-07-26 07:00:00

HTML colspan在CSS

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

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

底部填充上一行的空间。

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


当前回答

如果使用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;
}

其他回答

我创造了这把小提琴:

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

HTML

<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;

}

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

复制<td>,只显示所需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>

您可以尝试使用像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描述页面内容的结构,或者为表中的数据赋予一些含义,这是HTML的工作。