【问题标题】:Colspan of css display table did not work [duplicate]css显示表的colspan不起作用[重复]
【发布时间】:2019-10-17 17:16:37
【问题描述】:

我已经厌倦了在 CSS 中修复显示表中的 colspan。

.table {
  display: table;
  table-layout: fixed;
  width: 100%;
  border-collapse: collapse;
}

.table-row {
  display: table-row;
  border-collapse: collapse;
}

.table-cell {
  display: table-cell;
  padding: 5px;
  border: 1px solid black;
}

.table-row.colspan {
  display: block;
}
.table-row.colspan .table-cell {
  display: block;
  width: 200%;
  box-sizing: border-box;
}
<div class="table">
  <div class="table-row">
    <div class="table-cell">
      top left cell
    </div>
    <div class="table-cell">
      top right cell
    </div>
    <div class="table-cell">
      top right cell
    </div>
    <div class="table-cell">
      top right cell
    </div>
    <div class="table-cell">
      top right cell
    </div>
    <div class="table-cell">
      top right cell
    </div>
  </div>
  <div class="table-row">
    <div class="table-cell colspan">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </div>
    <div class="table-cell colspan">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </div>
    <div class="table-cell colspan">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </div>
  </div>
  <div class="table-row">
    <div class="table-cell">
      bottom left cell
    </div>
    <div class="table-cell">
      bottom right cell
    </div>
    <div class="table-cell">
      bottom left cell
    </div>
    <div class="table-cell">
      bottom right cell
    </div>
    <div class="table-cell">
      bottom left cell
    </div>
    <div class="table-cell">
      bottom right cell
    </div>
  </div>
</div>

如何获取表格中第二行的 colspan 行为?提前致谢。

https://codepen.io/mdshohelrana/pen/WNNxBqP

【问题讨论】:

  • @blurryfaceblueberry,两者都不一样。然后请查看整个问题并写下评论。
  • @Hardik's,太棒了
  • 你为什么不用 CSS 网格?
  • @connexo,我们公司决定使用 div 表。

标签: html css


【解决方案1】:

“colspan 行为”是什么意思?

不可能在 CSS 和 display: table-* 的表格中重现 HTML colspan 属性,因为它是结构性的东西而不是样式的东西。加上colspan 需要一个数字参数来确定要跨越的列数,而您没有。

你到底想做什么?如果要使用表格(用于表格数据),请使用 HTML table/tr/td 元素和 colspan 属性。

如果您想要一个类似(页面)的表格布局,那么您应该使用css grid,您可以使用它进行类似colspan 的设计。

在任何情况下,您都不应该使用很多 div 并使用诸如table-row/celletc 之类的类名。类名应该 semantically 描述 content 而不是 look 元素。

【讨论】:

  • 更新了我的代码 sn-p。
  • @Shohel 太好了。不幸的是,更新没有解决我的问题/评论。
【解决方案2】:

这个问题已经讨论过here

我们无法为 colspan 提供简单的 CSS 属性(据我所知)。 我认为使用表格标签更有效且易于理解。

<table>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
  <tr>
    <td colspan="2">Sum: $180</td>
  </tr>
</table>

https://www.w3schools.com/tags/att_td_colspan.asp你可以参考这里。

【讨论】:

    【解决方案3】:

    你可以通过两种方法解决它:

    1. 使用display:table-row-group,然后为单元格指定display:table-caption。但在这种情况下,我正在努力在右侧设置边框。 同样的小提琴如下:

    http://jsfiddle.net/hsbokxv4/

    1. 您可以使用 javascript 进行操作。小提琴在下面(虽然我不喜欢这种方法):

    http://jsfiddle.net/emilianolch/5nvxv5ko/

    【讨论】:

    • 更新了我的代码 sn-p。
    【解决方案4】:

    我可以通过设置浮动和宽度来模拟 hack。也许它可以用于您的设计。但我不确定它是否在所有平台上都一样。为整个表格添加宽度并将 colspan 宽度设置为与表格相同会更安全一些。使用宽度集更新了答案。

    宽度匹配的重要部分是box-sizing: border-box 设置。由于它会使两个单元格和一个连续单元格之间的间隙不同,因此应该设置它。

    .table {
      display: table;
      table-layout: fixed;
      width: 600px;
      border-collapse: collapse;
      box-sizing: border-box;
    }
    
    .table-row {
      display: table-row;
      border-collapse: collapse;
      box-sizing: border-box;
    }
    
    .table-row.colspanned
    {
      float: left;
      margin: -1px 0;
      width: 600px;
    }
    
    .table-cell {
      display: table-cell;
      padding: 5px;
      border: 1px solid black;
      box-sizing: border-box;
    }
    
    .table-cell.colspan {
      margin-top: -1px;
      margin-bottom: -1px;  
      width: 200px !important;
    }
    <div class="table">
      <div class="table-row">
        <div class="table-cell">
          top left cell
        </div>
        <div class="table-cell">
          top right cell
        </div>
        <div class="table-cell">
          top right cell
        </div>
        <div class="table-cell">
          top right cell
        </div>
        <div class="table-cell">
          top right cell
        </div>
        <div class="table-cell">
          top right cell
        </div>
      </div>
      <div class="table-row colspanned">
        <div class="table-cell colspan">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        </div>
        <div class="table-cell colspan">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        </div>
        <div class="table-cell colspan">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        </div>
      </div>
      <div class="table-row">
        <div class="table-cell">
          bottom left cell
        </div>
        <div class="table-cell">
          bottom right cell
        </div>
        <div class="table-cell">
          bottom left cell
        </div>
        <div class="table-cell">
          bottom right cell
        </div>
        <div class="table-cell">
          bottom left cell
        </div>
        <div class="table-cell">
          bottom right cell
        </div>
      </div>
    </div>

    【讨论】:

    • 更新了我的代码 sn-p。
    • 好的,但没有任何变化,只需将单元格的宽度指定为total width / 3。主要思想是一样的。
    • 没有解决您的问题,当我添加 6 列时
    • 更新了代码。
    猜你喜欢
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多