简体   繁体   English

如何为包含边界半径和透明背景的边界添加渐变?

[英]How to add gradient to border that contains border radius and has a transparent background?

I am not sure if the questions has been yet properly asked, but I am looking for a legit answer to this problem. 我不确定是否已经正确提出了问题,但是我正在寻找这个问题的合法答案。

I am trying to achieve the following. 我正在努力实现以下目标。

  • Create div with a border 创建带有边框的div
  • Give div a radius (make circle) 给div取一个半径(圈出)
  • Give border a gradient 给边界渐变
  • Keep background of div transparent 保持div的背景透明

I have found examples that would allow me to create a gradient border, but in most cases the background of the div was not transparent but had a color matching the body background. 我发现了一些示例,这些示例可以让我创建渐变边框,但是在大多数情况下,div的背景不是透明的,但是颜色与主体背景匹配。 example

One way that I found to add a border to your div is shown below, though this does not allow you to use border-radius and therefor keeps your div as a square, although allowing your background to be transparent, it does not allow your shape to be a circle. 我发现为div添加边框的一种方法如下所示,尽管这不允许您使用border-radius,因此将div保持为正方形,尽管允许背景透明,但不允许形状围成一个圈。

border-image:linear-gradient(to right,#23966c, #faaa54, #e23b4a, #db0768, #360670);
border-image-slice:1;

I have also tried to use clip-path circle, but have not found anything that works. 我也尝试过使用剪切路径圆,但没有找到任何可行的方法。

One last solution that struck me was to use font, I have looked for the roundest "o" in google fonts and found the font family "Josefin Sans". 让我震惊的最后一个解决方案是使用字体,我在Google字体中寻找了最圆的“ o”,并找到了字体家族“ Josefin Sans”。 Used the "o" as my circle, but doing such leaves me in a bind with the width of the border. 使用“ o”作为我的圆,但是这样做会使我陷入边界宽度的束缚中。 the following code below was used to achieve a gradient clip on my text. 下面的代码用于在我的文本上实现渐变剪辑。

 background: linear-gradient(to right,#23966c, #faaa54, #e23b4a, #db0768, #360670);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;  

see codepen example. 参见codepen示例。

I am considering to create svg.. but if anyone has came across a solution for this please let me know. 我正在考虑创建svg ..但是,如果有人遇到了解决方案,请告诉我。

Thank you. 谢谢。

Here is an SVG solution: 这是SVG解决方案:

 body { background-color:#080808; animation:changebg 30s infinite; overflow:hidden; } @keyframes changebg { 0%{background-color:#23966c;} 20%{background-color:#faaa54;} 40%{background-color:#e23b4a;} 60%{background-color:#db0768;} 80%{background-color:#360670;} 100%{background-color:#23966c;} } 
 <svg width="300" height="300"> <defs> <!-- define gradient --> <linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%"> <stop offset="0%" stop-color="blue" /> <stop offset="50%" stop-color="purple" /> <stop offset="100%" stop-color="yellow" /> </linearGradient> </defs> <circle r="100" cx="150" cy="150" fill="transparent" stroke="url(#grad)" stroke-width="10" /> </svg> 

By the way here is another idea using CSS but of course without transparency: 顺便说一下,这是使用CSS的另一个想法,但是当然没有透明度:

 body { background-color: pink; overflow: hidden; } .cir { width: 200px; height: 200px; margin: 20px auto; border-radius: 50%; border: 20px solid transparent; background: linear-gradient(pink, pink) padding-box, linear-gradient(to right, #23966c, #faaa54, #e23b4a, #db0768, #360670) border-box; } 
 <div class="cir"></div> 

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

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