简体   繁体   English

CSS中环形状的径向渐变

[英]Radial gradient in shape of ring in CSS

I would like to create "ring" shape with specified thickness (in px) with radial gradient.我想创建具有径向渐变的指定厚度(以 px 为单位)的“环形”形状。 Desired result is:想要的结果是:

在此处输入图片说明

However, I don't know how to specify thickness in pixels and ensure that the color transition is smooth from green to transparent (not cut off).但是,我不知道如何以像素为单位指定厚度并确保颜色从绿色过渡到透明(不切断)平滑。 My current state is:我现在的状态是:

 div { background-image: radial-gradient(transparent, green, transparent); border-radius: 100%; height: 300px; width: 300px; }
 <div></div>

Is there any way to make it in HTML and CSS, without using canvas or svg ( fiddle ).有什么方法可以在 HTML 和 CSS 中制作它,而不使用 canvas 或 svg ( fiddle )。 I can't use the image, because I would like to render different widths and thicknesses of this shape.我不能使用图像,因为我想渲染这个形状的不同宽度和厚度。

You can play with CSS radial gradient in this site .您可以在此站点中使用 CSS 径向渐变。

I achieved what you want, here's a demo .我实现了你想要的,这是一个演示 Just play around with the percentages to get the desired output.只需使用百分比即可获得所需的输出。

 div { background: radial-gradient(circle, rgba(0,128,0,0) 50%, rgba(0,128,0,1) 60%, rgba(0,128,0,0) 70%); width: 300px; height: 300px; }
 <div></div>

 div { background-image: radial-gradient(transparent, transparent 100px, green 150px, transparent 200px, transparent); border-radius: 100%; height: 300px; width: 300px; }
 <div></div>

I've just used some random px values.我刚刚使用了一些随机的 px 值。 Edit them as your requirements.根据您的要求编辑它们。 Here is the Santax: radial-gradient(color width, color width, color width, ...) width can be set in px, rem, % or any css unit.这是 Santax: radial-gradient(color width, color width, color width, ...)宽度可以设置为 px、rem、% 或任何 css 单位。

Here is a solution that will give you exactly the 50px of thickness you want.这是一个解决方案,可以为您提供所需的50px厚度。 You can also make it a variable to adjust it like you want:您还可以将其设置为变量以根据需要进行调整:

 .box { --t:50px; background: radial-gradient(farthest-side,transparent calc(100% - var(--t)), green, transparent 100%); display:inline-block; height: 250px; width: 250px; }
 <div class="box"></div> <div class="box" style="--t:80px;"></div> <div class="box" style="--t:100px"></div>

It's not a perfect replica but it's close enough.这不是一个完美的复制品,但已经足够接近了。 The trick is to use mask.诀窍是使用面具。

div {
  border-radius:50%;
  background:linear-gradient(green, green, green);
  -webkit-mask: radial-gradient(transparent 330px, #000 90px);
          mask: radial-gradient(transparent 330px, #000 90px);
}
div:before {
  content:"";
  display:block;
  padding-top:100%;
}
<div class="box"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM