英文:
How to get rounded corners on a css tablerow while also having a backgroundimage
问题
我正在尝试创建一个看起来像水平条形图的图像,百分比以颜色显示在中性背景上,并显示在其上方的描述(类似于这个,尽管显然不是可移动/响应的)。由于我要上传到的网站的限制,需要同时使用HTML表格和基本CSS创建它。
我希望每行的角是圆角的,已经发现这只能通过更改单元格的边框半径而不是行来实现。然而,背景图像仍然是矩形。
table {
border-collapse: separate;
border-spacing: 0 0.75vw;
width: 35vw;
margin: 1vw auto auto auto;
}
td {
border: solid 2px white;
padding: 0.5vw;
}
td:first-child {
border-top-left-radius: 0.5vw;
border-bottom-left-radius: 0.5vw;
}
td:last-child {
border-bottom-right-radius: 0.5vw;
border-top-right-radius: 0.5vw;
}
.chartrow {
background-image: linear-gradient(red, red);
background-color: orange;
background-position: left;
background-repeat: no-repeat;
}
.row1 {
background-size: 6.5%;
}
.row2 {
background-size: 20.3%;
}
.container {
background-color: green;
width: 50vw;
margin: auto;
}
<div class="container">
<table>
<tbody>
<tr class="chartrow row1">
<td>Answer 1</td>
<td>6.5%</td>
</tr>
<tr class="chartrow row2">
<td>Answer 2</td>
<td>20.3%</td>
</tr>
</tbody>
</table>
</div>
(白色边框只是为了展示我想要实现的效果)
是否有一种方法可以让背景图像的角变成圆角,或者完全另一种方法来显示背景中的条形图?
英文:
I'm trying to create an image that looks like a horizontal bar chart, with the percentages visible as a colour on a neutral background and the description on top of it (similar to this though obviously not moving/responsive). Due to the limitations of the site I'm going to upload this to it needs to be created with both a html table and basic css.
I would like the corners of the individual rows to be rounded and have already found that this only works by changing the border-radius of the cells, not the rows. However, the background image is still a rectangle.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
table {
border-collapse: separate;
border-spacing: 0 0.75vw;
width: 35vw;
margin: 1vw auto auto auto;
}
td {
border: solid 2px white;
padding: 0.5vw;
}
td:first-child {
border-top-left-radius: 0.5vw;
border-bottom-left-radius: 0.5vw;
}
td:last-child {
border-bottom-right-radius: 0.5vw;
border-top-right-radius: 0.5vw;
}
.chartrow {
background-image: linear-gradient(red, red);
background-color: orange;
background-position: left;
background-repeat: no-repeat;
}
.row1 {
background-size: 6.5%;
}
.row2 {
background-size: 20.3%;
}
.container {
background-color: green;
width: 50vw;
margin: auto;
}
<!-- language: lang-html -->
<div class="container">
<table>
<tbody>
<tr class="chartrow row1">
<td>Answer 1</td>
<td>6.5%</td>
</tr>
<tr class="chartrow row2">
<td>Answer 2</td>
<td>20.3%</td>
</tr>
</tbody>
</table>
</div>
<!-- end snippet -->
(The white border is only to show what I want to achieve)<br/><br/>
Is there a way to get the corners of the background image rounded, or maybe another way altogether to display the bar chart in the background?
答案1
得分: 0
默认情况下,您无法以这种方式样式<tr>
,因为它们具有display: table-row
。但是,您可以将它们设置为display: block
(或flex
或您需要的任何样式),然后可以按您的预期方式对其进行样式化。
table {
border-collapse: separate;
border-spacing: 0 0.75vw;
width: 35vw;
margin: 1vw auto auto auto;
}
td {
border: solid 2px white;
padding: 0.5vw;
}
td:first-child {
flex-grow: 1;
}
td:last-child {
width: 6ch; /* 固定大小,以便在所有行中都相同。一旦支持子网格,就不需要像这样做。 */
}
.chartrow {
background-image: linear-gradient(red, red);
background-color: orange;
background-position: left;
background-repeat: no-repeat;
display: flex;
border-radius: .5vw;
overflow: hidden; /* 以防止背景溢出圆角边框 */
margin-block: .5rem;
}
.row1 {
background-size: 6.5%;
}
.row2 {
background-size: 20.3%;
}
.container {
background-color: green;
width: 50vw;
margin: auto;
}
<div class="container">
<table>
<tbody>
<tr class="chartrow row1">
<td>Answer 1</td>
<td>6.5%</td>
</tr>
<tr class="chartrow row2">
<td>Answer 2</td>
<td>20.3%</td>
</tr>
</tbody>
</table>
</div>
英文:
By default you can’t style <tr>
s that way because they have display: table-row
. But you can set them to display: block
(or flex
or whatever you need) and can style them the way you’d expect.
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-css -->
table {
border-collapse: separate;
border-spacing: 0 0.75vw;
width: 35vw;
margin: 1vw auto auto auto;
}
td {
border: solid 2px white;
padding: 0.5vw;
}
td:first-child {
flex-grow: 1;
}
td:last-child {
width: 6ch; /* fixed so it’s the same in all rows. wouldn’t need to do it like this once subgrid lands. */
}
.chartrow {
background-image: linear-gradient(red, red);
background-color: orange;
background-position: left;
background-repeat: no-repeat;
display: flex;
border-radius: .5vw;
overflow: hidden; /* so the background doesn’t overflow the rounded border */
margin-block: .5rem;
}
.row1 {
background-size: 6.5%;
}
.row2 {
background-size: 20.3%;
}
.container {
background-color: green;
width: 50vw;
margin: auto;
}
<!-- language: lang-html -->
<div class="container">
<table>
<tbody>
<tr class="chartrow row1">
<td>Answer 1</td>
<td>6.5%</td>
</tr>
<tr class="chartrow row2">
<td>Answer 2</td>
<td>20.3%</td>
</tr>
</tbody>
</table>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论