2024-07-26 07:00:00

HTML colspan在CSS

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

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

底部填充上一行的空间。

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


当前回答

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

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

其他回答

我创造了这把小提琴:

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;

}

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

这就是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%; }

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

但如果你不需要从表格布局行为中获益,那么durilai的答案将足够适合你。

提供一个最新的答案:今天最好的方法是使用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>
<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>

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的工作。