【问题标题】:Is a grid possible with elements representing rows in CSS?是否可以使用表示 CSS 中的行的元素来创建网格?
【发布时间】:2019-06-18 05:08:26
【问题描述】:

我正在尝试使用现代浏览器的 HTML/CSS 复制此设计:

它本质上是一个包含行和列的表格,这意味着如果行的名称单元格变大,那么所有行的名称单元格都应该变大。我看到了两种可能性:表格和 CSS 网格。

据我所见,表格中的行不够风格,例如它们不能采用边框半径,我没有尝试过盒子阴影。

如果我使用 CSS 网格,我可以设置单元格的样式以在最后模拟边框半径,但是由于第二个单元格的盒子阴影覆盖了第一个单元格,所以盒子阴影变得不可能。

我认为问题归结为具有表示样式的行但仍在每个行内的元素,单元格应与其他行中的宽度相同,以表示列。

在 CSS 中有什么方法可以实现这一点吗?

例如,这里尝试使用 HTML 的 table 来执行此操作,其中边距和边框半径无效:

table {
  border-collapse: collapse;
}

tr {
  background-color: grey;
  border-radius: 8px;
  margin: 10px;
}
<table>
  <tr>
    <td>Eva Lee</td>
    <td>Call back</td>
    <td>15/02/19</td>
  </tr>
  <tr>
    <td>Evelyn Allen</td>
    <td>Call back</td>
    <td>14/01/19</td>
  </tr>
  <tr>
    <td>Slawomir Pelikan</td>
    <td>Voicemail</td>
    <td>14/01/19</td>
  </tr>
  <tr>
    <td>Christopher Walken</td>
    <td>Voicemail</td>
    <td>14/01/19</td>
  </tr>
</table>

这是另一个尝试,使用display: table,其作用与table相同:

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

.row {
  display: table-row;
  background-color: grey;
  border-radius: 8px;
  margin: 10px;
}

.cell {
  display: table-cell;
}
<div class="table">
  <div class="row">
    <div class="cell">Eva Lee</div>
    <div class="cell">Call back</div>
    <div class="cell">15/02/19</div>
  </div>
  <div class="row">
    <div class="cell">Evelyn Allen</div>
    <div class="cell">Call back</div>
    <div class="cell">14/01/19</div>
  </div>
  <div class="row">
    <div class="cell">Slawomir Pelikan</div>
    <div class="cell">Voicemail</div>
    <div class="cell">14/01/19</div>
  </div>
  <div class="row">
    <div class="cell">Christopher Walken</div>
    <div class="cell">Voicemail</div>
    <div class="cell">14/01/19</div>
  </div>
</div>

【问题讨论】:

  • 使用display:table,您将避免使用表格,并且仍然可以使用您想要的样式进行表格布局
  • @TemaniAfif:我认为这与表格有相同的缺点。我更新了问题以包含示例。
  • 我已经编辑了我的答案以包含所需的样式

标签: css html-table css-grid


【解决方案1】:

编辑 1

这适用于 FF、Chrome、MSIE、MS Edge。
边缘可能需要稍作调整,因为表格单元格具有亚像素宽度,如果现在给出像素宽度,则会导致令人讨厌的垂直条。

这可以通过在 divs 上使用负边距来实现,它包含单元格内容和 overflow: hidden 在单元格上。

table {
  border-collapse: collapse;
  border-spacing: 0;
}
table tr td {
  overflow: hidden;
  padding: 0 0 5px 0;
}
table tr td > div {
  background-color: gold;
  padding: 4px;
  border-radius: 0px;
  box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.3);
  margin-left: -10px;
  margin-right: -10px;
  padding-left: 14px;
  padding-right: 14px;
}
table tr td:first-child {
  padding-left: 10px;
}
table tr td:first-child > div {
  border-radius: 10px 0 0 10px;
}
table tr td:last-child {
  padding-right: 20px;
}
table tr td:last-child > div {
  border-radius: 0 10px 10px 0;
}
<table>
  <tr>
    <td>
      <div>Lorem.</div>
    </td>
    <td>
      <div>Ea!</div>
    </td>
    <td>
      <div>Animi.</div>
    </td>
  </tr>
  <tr>
    <td>
      <div>Quas!</div>
    </td>
    <td>
      <div>Dolor!</div>
    </td>
    <td>
      <div>Suscipit.</div>
    </td>
  </tr>
  <tr>
    <td>
      <div>Mollitia?</div>
    </td>
    <td>
      <div>Inventore!</div>
    </td>
    <td>
      <div>Dolorem.</div>
    </td>
  </tr>
</table>

旧的,无法在 Chrome 中运行

来点伪元素魔法怎么样?

table {
  border-spacing: 10px;
  border-collapse: separate;
}

tr {
  position: relative;
  margin: 10px;
}
tr:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: gold;
  z-index: -1;
  border-radius: 20px;
  box-shadow: 3px 2px 5px rgba(0, 0, 0, 0.25);
}

td {
  padding: 10px;
}
<table>
  <tr>
    <td>Eva Lee</td>
    <td>Call back</td>
    <td>15/02/19</td>
  </tr>
  <tr>
    <td>Evelyn Allen</td>
    <td>Call back</td>
    <td>14/01/19</td>
  </tr>
  <tr>
    <td>Slawomir Pelikan</td>
    <td>Voicemail</td>
    <td>14/01/19</td>
  </tr>
  <tr>
    <td>Christopher Walken</td>
    <td>Voicemail</td>
    <td>14/01/19</td>
  </tr>
</table>

【讨论】:

  • 当我运行代码 sn-p 时,输出看起来不像我发布的设计。
  • 我不会复制你的设计。我只是给你一个提示如何管理边界半径和阴影问题。
  • 是的,但是您将边框半径应用于表格,这是可行的。问题是将边框半径和盒子阴影应用于 tr。
  • 请再看一遍。我将边界半径 Nd 阴影应用于绝对定位在相对 tr 内的伪元素
  • 我猜你正在使用 FF 测试你的代码。 tr 上的伪元素在 Chrome 上不起作用(不知道其他浏览器),但据我所知,它们不应该在 tr 上工作 ..这就是为什么我在回答中在 td 上使用它们
【解决方案2】:

您可以通过一些技巧来克服样式问题。这是一个我依靠伪元素来设置行样式的示例:

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

.row {
  display: table-row;
}

.cell {
  display: table-cell;
  position: relative;
  z-index: 0;
  padding: 5px;
}

.cell span {
  background: orange;
  color: #fff;
  border-radius: 10px;
  padding: 2px 4px;
}

.cell:first-child {
  padding: 10px 5px 20px;
  width: 50%;
}

.cell:before {
  content: "";
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 10px;
  background: #fff;
  border: 1px solid grey;
}

.cell:first-child:before {
  border-right: 0;
  border-radius: 8px 0 0 8px;
  background:#fff;
  box-shadow: -1px 0 6px;
}

.cell:last-child:before {
  border-left: 0;
  border-radius: 0 8px 8px 0;
  background: #fff;
  box-shadow: 1px 0 6px;
}

.cell:nth-child(2) {
  text-align: center;
  z-index: 1;
}

.cell:nth-child(2):before {
  border-right: 0;
  border-left: 0;
  box-shadow: 0 0 6px;
}

.cell:nth-child(2):after {
  content: "";
  position: absolute;
  top: 1px;
  bottom: 11px;
  left: -5px;
  right: -5px;
  background: 
   linear-gradient(green,green) left 5px top 50%/ 2px 80% no-repeat,
   linear-gradient(green,green) right 5px top 50%/ 2px 80% no-repeat,
  #fff;
  z-index: -1;
}

body {
  background: #f3f3f3;
}
<div class="table">
  <div class="row">
    <div class="cell">Eva Lee</div>
    <div class="cell"><span>Call back</span></div>
    <div class="cell">15/02/19</div>
  </div>
  <div class="row">
    <div class="cell">Evelyn Allen</div>
    <div class="cell"><span>Call back back</span></div>
    <div class="cell">14/01/2019</div>
  </div>
  <div class="row">
    <div class="cell">Slawomir Pelikan</div>
    <div class="cell"><span>Voicemail</span></div>
    <div class="cell">14/01/19</div>
  </div>
  <div class="row">
    <div class="cell">Christopher Walken</div>
    <div class="cell"><span>Voicemail Voicemail</span></div>
    <div class="cell">14/01/19</div>
  </div>
</div>

【讨论】:

  • 如何将 box-shadow 集成到其中?
  • @yunzen 检查更新,四面八方的影子;)
  • 虽然这在技术上是一个阴影,但它看起来更像是一个外发光。像这样的阴影呢:box-shadow: 3px 3px 5px rgba(0,0,0,0.3);
  • @yunzen 好吧,可以根据需要调整,我在所有侧面都做了,以表明可以,我们可以轻松翻译它
【解决方案3】:

用方框阴影网格行

修复盒子阴影,而不是用元素创建一个网格。
我为每一行创建一个网格。
这让我们可以创建行大小函数。
同时仍然持有获取单行元素以应用框阴影。

body {
  background-color: #eee;
}

.container {
  width: 100%;
}

.row {
  display: grid;
  grid-template-columns: 1fr 150px 200px;
  width: 100%;
  /* box-shadow: 0px 0px 0px 1px #222; */
  margin: 10px 0px;
  border-radius: 5px;
  box-shadow: 0px 0px 2px 0px #222;
}

.name {}

.status {
  position: relative;
  text-align: center;
  width: 150px;
}

.status:before {
  content: "";
  position: absolute;
  display: inline-block;
  border-left: 2px solid #aaa;
  height: calc(1em + 20px);
  left: 0;
  top: 5px;
}

.status:after {
  content: "";
  position: absolute;
  display: inline-block;
  border-left: 2px solid #aaa;
  height: calc(1em + 20px);
  right: 0;
  top: 5px;
}

.decoration {
  width: 100%;
  padding: 2px 30px;
  border-radius: 50px;
}

.decoration.error {
  background-color: tomato;
}

.date {
  text-align: center;
}

.container .row div {
  background-color: white;
  padding: 15px;
}

.container .row div:first-of-type {
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
}

.container .row div:last-of-type {
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
}
<div class="container">
  <div class="row">
    <div class="name">
      Lee Wong
    </div>
    <div class="status">
      <span class="decoration error">
      error
      </span>
    </div>
    <div class="date">
      12/5/2020
    </div>
  </div>
  <div class="row">
    <div class="name">
      Lee Wong
    </div>
    <div class="status">
      <span class="decoration error">
      error
      </span>
    </div>
    <div class="date">
      12/5/2020
    </div>
  </div>
  <div class="row">
    <div class="name">
      Lee Wong
    </div>
    <div class="status">
      <span class="decoration error">
      error
      </span>
    </div>
    <div class="date">
      12/5/2020
    </div>
  </div>

</div>

【讨论】:

  • 注意。虽然这可以按预期工作,但只有在每行只有一个 no no auto 列时才有效。
  • jsfiddle.net/qe8367o1 您可以有多个自动列。但是您的标记必须与给定的 css 匹配。可以使用区域名称设置网格来解决这个问题。
  • 如果你想要文本独立大小使用1fr 1fr 150px 200px 而不是auto auto 150px 200px
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-04
  • 2023-01-09
相关资源
最近更新 更多