【问题标题】:Table using divs - no y overflow for row使用 div 的表 - 行没有 y 溢出
【发布时间】:2014-04-22 19:02:43
【问题描述】:

我正在尝试使用 div 创建一个表。 (我不能使用 table、tr 和 td 标签)

问题是:

  • 我的单元格必须彼此相邻(我使用float:left
  • 我的行不应垂直扩展,但可以在 x 轴上增长。

这是我的 HTML:

<div id="table">
  <div class="rowHeader">

  </div>
  <div class="row">
      <div class="cell">bla</div>
      <div class="cell">bla</div>
      <div class="cell">bla</div>
  </div>
  <div class="row">
      <div class="cell">bla2</div>
      <div class="cell">bla2</div>
      <div class="cell">bla2</div>
  </div>
</div>

和 CSS :

#table {
  display: table;
  width: 100%;
}

.row {
  width: 100%;
  overflow: hidden;
  display: table-row;
}

.cell {
  float: left;
  display: table-cell;
  margin: 1px;
  padding: 2px;
  width: 35%;
  background: #ff3434;

}

有什么建议吗?

警告:我的网站应该在 IE6 上运行(请避免使用 CSS3/或提供替代方式)... FML =)

演示:JSFiddle


编辑

使用display:block/inline-block时同样的问题

JSFiddle

【问题讨论】:

  • display:table-cell 不支持 IE8 以下的 IE:caniuse.com/css-table
  • 编辑显示不同显示属性的相同问题
  • 为什么不使用表格——当它是你应该使用的数据时。 (语义)。
  • @JohanVdR 我不会只包含数据

标签: html css internet-explorer-6


【解决方案1】:

当您使用 width: 100% 时,您会强制 div 表为该宽度。使用min-width: 100% 代替表格并删除单元格div 的float: left 和行的宽度。 在这里看到它:http://jsfiddle.net/C6UgT/1/

编辑:

如果你想保持单元格的宽度,并且只有当它的宽度比正文宽时才能在表格中获得一个水平滚动条,你必须在父元素中添加属性overflow-x: auto;white-space: nowrap;(表):

#table {
    white-space: nowrap;
    width: 100%;
    overflow-x: auto;

}

并为子元素(单元格)使用属性display: inline-block;

.cell {
    display: inline-block;
    margin: 1px;
    padding: 2px;
    min-width: 35%;
    background: #ff3434;
}

更新小提琴:http://jsfiddle.net/C6UgT/5/

【讨论】:

  • 我想要相同的结果,但如果行的内容超过正文的宽度,则在表格上使用 X 滚动条。这里有些单元格比应有的要小。
  • 太完美了!编辑你的答案,我会接受 =)
  • 我错过了 white-space: nowrap; 的东西
  • 知道为什么我的单元格看起来有 5px 的边距吗? (这就是我以前设置float:left的原因...)
  • 是的,它只是 div 之间的空白。当它们在新行中时,会呈现此空白。把所有的div标签排成一行就可以了:jsfiddle.net/C6UgT/6
【解决方案2】:

尝试重新调整单元格的宽度。

【讨论】:

  • 我希望单元格宽度的总和大于窗口宽度。
  • 试试看这个 Jsfiddle 我更新它:jsfiddle.net/kLetE/4 这就是你想要弄清楚的吗?
  • 是的,我想要相同的结果,但如果行的内容超过正文的宽度,则在表格上使用 X 滚动条。 (这就是为什么我使用 35% 作为示例)
  • 好的,在表格和行中设置一个固定宽度,然后在css中添加一个溢出:滚动。 (宽度必须为 px)
猜你喜欢
  • 2013-05-23
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多