具有叠加颜色的精美 css 表格设计
Posted
技术标签:
【中文标题】具有叠加颜色的精美 css 表格设计【英文标题】:Fancy css table design with overlaying colors 【发布时间】:2019-04-24 22:21:40 【问题描述】:我从一位设计师那里得到了以下表格设计,并被要求在 html/css 中实现它。
我尝试了以下实现,但现在我被卡住了,觉得我跑错了方向。
我的方法是添加空列/行以分隔垂直框和没有覆盖框的区域。 我现在面临的一些问题:
-
垂直框的起始位置应该比表格的彩色行高一点,然后再往下一点。
我应该在右侧画线,然后在顶部向左延伸。
在 Firefox 上,圆角与覆盖框结合起来会表现得很奇怪(见右侧)。在镀铬中,圆角看起来像预期的那样。
我试图从我的脚本中获取代码。这有点棘手,因为我使用 R 生成表格(不要问;))。
::after,
::before
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
::selection
background: #eee;
::selection
background: #eee;
element
*
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
body
line-height: 1.4;
color: #222222;
font-family: MaisonNeue;
font-size: 14px;
body
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.42857143;
color: #333;
html
font-size: 10px;
html
font-family: sans-serif;
-webkit-text-size-adjust: 100%;
.vanilla
font-size: 12px;
width: 90%;
border-collapse: collapse;
text-align: center;
border-spacing: 0 5px;
background-size: 100% 50%;
.vanilla th
text-align: center;
background: #ffffff;
color: #000000;
.vanilla td
padding: 5px 0;
border-bottom: 5px solid white;
.vanilla tbody tr
border-bottom: 1px solid rgba(0, 0, 0, 0.35);
.vanilla tr:first-child td:first-child
border-top-left-radius: 10px;
text-align: left;
padding-left: 7px;
border-bottom-left-radius: 10px;
.vanilla tr:first-child td:last-child
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
.vanilla tr:nth-child(2) td:first-child
text-align: left;
padding-left: 7px;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
.vanilla tr:nth-child(2) td:last-child
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
.vanilla tr:nth-child(3) td:first-child
text-align: left;
padding-left: 7px;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
.vanilla tr:nth-child(3) td:last-child
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
.vanilla tr:nth-child(4) td:first-child
text-align: left;
padding-left: 7px;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
.vanilla tr:nth-child(4) td:last-child
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
.vanilla tr:nth-child(5) td:first-child
text-align: left;
padding-left: 7px;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
.vanilla tr:nth-child(5) td:last-child
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
.vanilla tr:nth-child(6) td:first-child
text-align: left;
padding-left: 7px;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
.vanilla tr:nth-child(6) td:last-child
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
.vanilla td:nth-child(3),
td:nth-child(5),
td:nth-child(7),
td:nth-child(9)
background: rgba(236, 236, 236, 0.95);
text-align: center;
padding-left: 7px;
width: 180px;
color: black;
.vanilla td:nth-child(2),
td:nth-child(4),
td:nth-child(6),
td:nth-child(8)
padding-left: 10px;
.vanilla tr:first-child
font-weight: bold;
font-style: arial;
font-size: 17px;
color: #ffffff;
background-color: RGB(181, 194, 198);
.vanilla tr:nth-child(2)
font-weight: bold;
font-style: arial;
font-size: 17px;
color: white;
background-color: RGB(65, 143, 126);
.vanilla tr:nth-child(3)
background-color: RGB(146, 206, 193);
color: white;
font-weight: bold;
font-style: Times new roman;
font-size: 17px;
.vanilla tr:nth-child(4)
background-color: RGB(15, 74, 96);
color: white;
font-weight: bold;
font-style: arial;
font-size: 17px;
padding-left: 10px;
.vanilla tr:nth-child(5)
background-color: RGB(74, 123, 143);
color: white;
font-weight: bold;
font-style: arial;
font-size: 17px;
padding-left: 10px;
.vanilla tr:nth-child(6)
background-color: RGB(56, 56, 54);
color: white;
font-weight: bold;
font-style: arial;
font-size: 17px;
padding-left: 10px;
.delta_value
/*width: 30px;*/
display: inline-block;
.delta
font-weight: bold;
font-size: 17px;
width: 25px;
color: #69A252;
height: 25px;
border-radius: 15px;
vertical-align: middle;
/*padding-top: 5px;*/
margin-left: 4px;
/*padding-left: 8px;*/
.delta_base
text-align: left;
color: #000000;
<div>
<div id="tacos_1_1-taco" class="shiny-html-output shiny-bound-output">
<table class="vanilla">
<thead>
<tr>
<th><span> </span></th>
<th><span></span></th>
<th><span>2015</span></th>
<th><span></span></th>
<th><span>2016</span></th>
<th><span></span></th>
<th><span>2017</span></th>
</tr>
</thead>
<tbody>
<tr>
<td><span>XXX</span></td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value">0 XXXX</span><span style="width:60px;float:right;margin-right:3px;color:white" class="delta"></span></div>
</span>
</td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value">0 XXX</span><span style="width:60px;float:right;margin-right:3px;color:green" class="delta">(+0%)</span></div>
</span>
</td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value">0 XXXX</span><span style="width:60px;float:right;margin-right:3px;color:black" class="delta">(0%)</span></div>
</span>
</td>
</tr>
<tr>
<td><span>XXXX</span></td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value">0 XXX</span><span style="width:60px;float:right;margin-right:3px;color:white" class="delta"></span></div>
</span>
</td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value">0 XXX</span><span style="width:60px;float:right;margin-right:3px;color:green" class="delta">(+0%)</span></div>
</span>
</td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value">0 XXX</span><span style="width:60px;float:right;margin-right:3px;color:green" class="delta">(+0%)</span></div>
</span>
</td>
</tr>
<tr>
<td><span>XXXX</span></td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value">0 XXX</span><span style="width:60px;float:right;margin-right:3px;color:white" class="delta"></span></div>
</span>
</td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value">0 XXX</span><span style="width:60px;float:right;margin-right:3px;color:green" class="delta">(+0%)</span></div>
</span>
</td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value">0 XXX</span><span style="width:60px;float:right;margin-right:3px;color:green" class="delta">(0%)</span></div>
</span>
</td>
</tr>
<tr>
<td><span>XXXX</span></td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value"> 0 %</span><span style="width:60px;float:right;margin-right:3px;color:white" class="delta"></span></div>
</span>
</td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value"> 0 %</span><span style="width:60px;float:right;margin-right:3px;color:green" class="delta">(+0%)</span></div>
</span>
</td>
<td><span></span></td>
<td><span><div style="position:relative"><span style="position:absolute;right:40%" class="delta_value"> 0 %</span><span style="width:60px;float:right;margin-right:3px;color:red" class="delta">(0%)</span></div>
</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
【问题讨论】:
请提供您的代码,以便我修改它:) 在某个地方扔一些表格 html,然后我会玩它。喜欢codepen.io/# :) 好的,我正在 r 中生成 html,但我会尝试复制它并添加相关的 css。 Imo 尝试使用 div 而不是 table 来实现这项工作。根据艺术品告诉我的内容,使用 div 而不是 table 更容易完成这项工作。 告诉设计师 HTML 中的table
元素有很多布局限制(他可能应该知道,因为他的工作是为 HTML 设计)。
【参考方案1】:
你的桌子设计确实很棘手,但我想我设法让它发挥作用。
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<td></td>
<td>2018</td>
<td>2017</td>
<td>2016</td>
</tr>
</thead>
<tbody>
<tr>
<th>XXX</th>
<td>0 XXX <span class="details">(+50)</span></td>
<td>0 XXX </td>
<td>0 XXX </td>
</tr>
<tr>
<th>XXX</th>
<td>0 XXX <span class="details">(+50)</span></td>
<td>0 XXX </td>
<td>0 XXX </td>
</tr>
<tr>
<th>XXX</th>
<td>0 XXX <span class="details">(+50)</span></td>
<td>0 XXX </td>
<td>0 XXX </td>
</tr>
</tbody>
<!-- each "group" of rows is its own tbody -->
<tbody class="blue">
<tr>
<th>XXX</th>
<td>0 XXX <span class="details">(+50)</span></td>
<td>0 XXX </td>
<td>0 XXX </td>
</tr>
<tr>
<th>XXX</th>
<td>0 XXX <span class="details">(+50)</span></td>
<td>0 XXX </td>
<td>0 XXX </td>
</tr>
</tbody>
</table>
这里有 CSS(我在代码笔中写了 SCSS,这是编译结果)
table thead th, table thead td
padding: 5px 10px;
font-weight: bold;
table tbody td, table tbody th
color: #fff;
position: relative;
padding: 10px 25px;
table tbody td:before, table tbody th:before
content: '';
position: absolute;
top: 5px;
left: 0;
right: 0;
bottom: 5px;
background: green;
z-index: -2;
table tbody.blue th:before, table tbody.blue td:before
background: blue;
table tbody td
color: #000;
table tbody td:after
content: '';
position: absolute;
top: 0;
left: 10px;
right: 10px;
bottom: 0px;
background: rgba(200, 200, 200, 0.9);
z-index: -1;
table tbody th:before
border-radius: 5px 0 0 5px;
left: 5px;
table tbody td:last-child:before
border-radius: 0 5px 5px 0;
table tbody th:after
content: '';
position: absolute;
top: 0;
left: 0;
width: 0;
bottom: 0;
border-left: 1px solid red;
table tbody tr:first-child th:after
content: '';
position: absolute;
top: 0;
left: 0;
width: 50%;
border-top: 1px solid red;
table tbody tr:last-child th:after
bottom: 5px;
.details
color: green;
这里是代码笔:https://codepen.io/anon/pen/dQmymP
(请忽略外观,我没有时间用适当的颜色、间距和字体设置样式)
我在 Chrome、Firefox 和 Edge 中检查过它。告诉我这对你有用。
【讨论】:
太棒了!看起来你几乎可以解决我所有的大问题。将尝试将其移植回我的 R 脚本中,但已经非常感谢您了!以上是关于具有叠加颜色的精美 css 表格设计的主要内容,如果未能解决你的问题,请参考以下文章