创建填充渐变

时间:2017-06-13 16:44:02

标签: html css3 gradient pie-chart border-color

我设法创建了带边框的简单图表。我想将简单的 border-color 更改为渐变。我试图使用 border-image background 属性,但似乎无法对其进行曲线处理,因此它可以适合容器的拱形形状。 有没有办法在css3中实现这种效果?

HTML

<div class="pie">
    <span class="overlay"></span>
</div>

CSS

.pie {
    margin: 0 auto;
    position: relative;
    width: 116px;
    height: 58px;
    overflow: hidden;
}

.pie *,
.pie::before {
    box-sizing: border-box;
}

.pie::before {
    content: '';
    width: inherit;
    height: inherit;
    border: 20px solid grey;
    border-bottom: none;
    border-top-left-radius: 175px;
    border-top-right-radius: 175px;
    position: absolute;
    left:0;
}

.pie .overlay{
    position: absolute;
    top: 100%;
    left: 0;
    width: inherit;
    height: inherit;
    border: 20px solid;
    border-top: none;
    border-bottom-left-radius: 175px;
    border-bottom-right-radius: 175px;
    transform-origin: 50% 0;
    border-color:yellow;/* background: linear-gradient(to right, rgba(228,232,7,1) 0%, rgba(0,218,156,1) 100%); */
    transform: rotate(90deg); 
}

3 个答案:

答案 0 :(得分:3)

如果你使用pie的伪和overlay作为白色中心,你可以这样做

.pie {
    margin: 0 auto;
    position: relative;
    width: 200px;
    height: 100px;
    border-radius: 200px 200px 0 0;
    overflow: hidden;
}
.pie::after {
    transform: rotate(-60deg);      /*  set rotation degree  */
    background: linear-gradient(to right, rgba(228,232,7,1) 0%, rgba(0,218,156,1) 100%);
    transform-origin: center bottom;
}
.pie::before {
    border: 20px solid grey;
}
.pie .overlay{
    top: 20px;                      /*  match border width  */
    left: 20px;                     /*  match border width  */
    width: calc(100% - 40px);       /*  match border width times 2  */
    height: calc(200% - 40px);      /*  match border width times 2  */
    border-radius: 100%;
    background: white;
    z-index: 1;                     /*  move it on top of the pseudo elements  */
}
.pie *,
.pie::before,
.pie::after {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    border-radius: inherit;
    box-sizing: border-box;
}
<div class="pie">
    <span class="overlay"></span>
</div>

答案 1 :(得分:1)

你可以玩2个渐变,内部遮蔽外部:

.test {
    width: 200px;
    height: 100px;
    background-image: linear-gradient(white, white), linear-gradient(to left, red, green);
    background-clip: padding-box, border-box;
    background-origin: padding-box, border-box; 
    border: solid 50px transparent;
    border-radius: 200px 200px 0px 0px;
    border-width: 50px 50px 0px 50px;
}
    
<div class="test"></div>

.test {
    width: 200px;
    height: 100px;
    background-image: linear-gradient(white, white);
    background-clip: content-box;
    background-origin: content-box; 
    border: solid 50px transparent;
    border-radius: 200px 200px 0px 0px;
    padding: 50px 50px 0px 50px;
    position: relative;
    overflow: hidden;
}

.test:after {
    content: "";
    position: absolute;
    left: -25%;
    top: -50%;
    right: 0px;
    height: 300%;
    width: 150%;
    background-image: linear-gradient(to left, red, green);
    transform: rotate(0deg);
    transform-origin: center center;
    z-index: -1;
    animation: spin 3s infinite;
}

@keyframes spin {
    from {transform: rotate(0deg);}
    to {transform: rotate(360deg);}
}
<div class="test"></div>

答案 2 :(得分:0)

您可以在.pie上添加一个额外的伪元素(::after),将它们放置在左上角和右上角的重叠位置,将它们两者曲线化,然后使用{{1渐变。

然后,将background置于span的中心,并为其提供一个白色背景(而不是透明),其中.pie更高,以确保弧的中心保持不变在机智。

&#13;
&#13;
z-index
&#13;
div {
  width: 200px;
  height: 200px;
  position: relative;
}
div::before {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  background: linear-gradient(to right, azure, slategray);
  width: 100px;
  height: 100px;
  left: 0;
  border-top-left-radius: 100px;
}
div::after {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  background: linear-gradient(to right, midnightblue, steelblue);
  width: 100px;
  height: 100px;
  right: 0;
  border-top-right-radius: 100px;
}
div span {
  display: block;
  position: absolute;
  left: calc(50% - 50px);
  top: calc(50% - 50px);
  background: white;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  z-index: 1;
}
&#13;
&#13;
&#13;