【问题标题】:Table CSS styling | Borders表格 CSS 样式 |边框
【发布时间】:2021-02-18 23:51:09
【问题描述】:

我真的需要一些 CSS 方面的帮助。

我正在尝试设置表格的样式,但在添加边框时遇到了困难。

这是我想要的表格样式(Photoshop):https://ibb.co/hFkCkDg

在表格周围添加边框很简单:

.table-class {
    border: 1px solid #dddddd !important;
    padding: 20px !important;
    border-radius: 5px;
}

截图:https://ibb.co/Fs6qsNv

要在表格中添加分隔线,我需要为表格中的行添加顶部或底部边框。行是tr 元素。默认情况下,表格的tr 元素不接受边框。所以为了克服这个问题,我在整个表格中添加了{border-collapse: collapse !important;},这允许我为行添加边框,但它弄乱了整个表格的边框。截图:https://ibb.co/Vgfq9jp

由于{border-collapse: collapse !important;},属性borderpaddingborder-radius 不适用于表。

这意味着我可以在整个表格周围添加边框或添加分隔线,但不能同时添加。

如何实现我想要的外观?

【问题讨论】:

标签: html css border


【解决方案1】:

我会使用 flexbox,并将 flex: 1flex-grow: 1 设置为每个 "row"第一个孩子

* {margin:0; box-sizing: border-box;}
body {font: 16px/1.4 'Varela Round', sans-serif; padding: 20px;} /* DEMO ONLY */

/*
  Order
*/

.Order {
  border-radius: 5px;
  border: 1px solid #ddd;
  padding: 10px 25px
}

.Order-price {
  display: flex;
  border-bottom: 1px solid #ddd;
}

.Order-price > * {
  padding: 10px 0;
}

.Order-price > *:first-child{
  flex: 1;
}

.Order-price:last-child {
  border-bottom: none;
}

.Order-price--sub {
  font-size: 1.2em;
  font-weight: 500;
}

.Order-price--tot {
  font-size: 1.4em;
  font-weight: 700;
}

/*
  Colors
*/

.color-lighter {
  color: #999;
}
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Varela+Round&display=swap" rel="stylesheet">

<div class="Order">
  <div class="Order-price">
    <span class="color-lighter">Custom Tatoo Design - Small &times; 1</span>
    <span><s class="color-lighter">$99.00</s> <b>$80.00</b></span>
  </div>
  <div class="Order-price Order-price--sub">
    <span>Subtotal</span>
    <span>$80.00</span>
  </div>
  <div class="Order-price Order-price--tot">
    <span>Total</span>
    <span><small>USD</small> $80.00</span>
  </div>
</div>

【讨论】:

  • 嗨@roko,谢谢你的回答。我尝试使用这种方法。结果如下:desktop view & mobile landscape view。由于某种原因,产品价格卡在中间,但我不知道为什么。你知道这里可能是什么问题吗?这是我结帐页面的link,您可以自己查看表格。感谢您的帮助!
  • 好的,所以我修复了它,但我不知道为什么我所做的工作。我没有将flex: 1 设置为每个产品行的:first-child,而是将其添加到:last-child。我不知道有什么区别,但它解决了产品价格的问题。所有其他表行都与:first-child:last-child 一起使用,这似乎没有什么区别。另外,您的 CSS 代码中的 * 是什么意思?我也没有为:last-child 设置border-bottom: none,因为它什么也没做。
  • @JOKKER *All 的通配符,因此 .foo &gt; * 表示 (RtL):target All Immediate descendants of .foo。如果您检查 Element 并突出显示具有flex-grow: 1(或flex: 1)的元素,您会看到差异。这不是微不足道的。对于我的具体情况,我希望 first SPAN “扩展/增长”(将兄弟姐妹推到最右边),而不是第二个 SPAN。重要的是解决问题:)
  • @JOKKER 再次...如果没有必要,请不要发布图片。我无法仅通过查看图像来猜测您做了什么。您可以随时为您当前的最小示例代码创建一个jsfiddle 或 jsBin - 如果您想分享您当前的进度!
【解决方案2】:

使用表格边框很无聊,我的建议是在td/th 元素中使用边框。

我创建的这个表格没有样式,只解决边框问题

   .table-class {
    border: 1px solid #dddddd;
    border-radius: 6px;
    padding: 30px;
    border-spacing: unset;
    font-family: sans-serif;
    font-size: 1.5em;
}

.table-class thead th {
    border-bottom: 1px solid #dddddd;
    text-align: left;
}

.table-class tbody td {
    border-bottom: 1px solid #dddddd;
}

.table-class td:last-child, .table-class th:last-child {
    text-align: right;
}

.table-class th, .table-class td{
    padding: 10px;
}
<table class="table-class">
  <thead>
    <tr>
      <th>Custom Tattoo Desing - Small x 1</th>
      <th>
        <span><s>$99.00</s></span>
        <span>$80.00</span>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Subtotal</td>
      <td>$80.00</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Total</td>
      <td>USD $80.00</td>
    </tr>
  </tfoot>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 2011-10-05
    • 2014-07-06
    • 2011-01-04
    相关资源
    最近更新 更多