简体   繁体   English

如何在边框中创建径向渐变?

[英]How would I create a radial gradient in my border?

I'm trying to accomplish the following: 我正在尝试完成以下任务:

径向渐变盒

Basically, this is just a block element: 基本上,这只是一个块元素:

<div></div>

div {
    width: 100px;
    height: 100px;
}

How would I put the radial gradient image inside the top left of the elements border? 我如何将径向渐变图像放在元素边框的左上方?

You can use border-image with some radial-gradient like this: 您可以使用带有一些radial-gradient border-image ,如下所示:

HTML : HTML

<div><div>

CSS : CSS

div {
  width:200px;
  height:200px;
  background:blue;
  border-style:solid;
  border-image:-webkit-radial-gradient(-15% -15%, farthest-side, red, blue) 20/8;
  border-image:-moz-radial-gradient(-15% -15%, farthest-side, red, blue) 20/8; //Will work starting with FF29.
  border-image:radial-gradient(-15% -15%, farthest-side, red, blue) 20/8;
}

Here is the Fiddle 这是小提琴

NOTE : Unfortunately Internet Explorer does not support this yet. 注意 :很遗憾,Internet Explorer尚不支持此功能。 The current version of Firefox (29.0.1) does have support , but all previous versions including ESRs won't render it correctly, and there is nothing on the horizon for IE, and all past versions will never support this method. 当前版本的Firefox(29.0.1)确实具有支持 ,但是所有以前的版本(包括ESR)都无法正确呈现它,并且IE即将面世,并且所有以前的版本将永远不支持此方法。 If you need to support any versions of IE, you'll need to use another method, such as using a pseudo-element :before . 如果需要支持IE的任何版本,则需要使用其他方法,例如使用伪元素:before

http://jsfiddle.net/ypJ8k/2/ http://jsfiddle.net/ypJ8k/2/

<div class="div1">
<div class="div2"></div>
</div>

You can do it without pseudo elements like after so your gradiant will be much more accurate 您可以在没有伪元素的情况下进行操作,例如after,这样您的渐变效果将更加准确

the big wall of css is just the gradiant. CSS的长城只是梯度。 go to http://www.colorzilla.com/gradient-editor/ , paste that gradiant (you have space before and after so you can copy it nicely from the fiddle) and modifie it at will 转到http://www.colorzilla.com/gradient-editor/ ,粘贴该渐变(您之前和之后都有空间,以便可以从小提琴中很好地复制它)并随意进行修改

updated: bigger size (300x300). 已更新:更大尺寸(300x300)。 http://jsfiddle.net/ypJ8k/3/ http://jsfiddle.net/ypJ8k/3/

One pseudo element should be enough with a radial gradient background from the corner and appropriate color stop. 一个伪元素应该足够,从角落开始有一个径向渐变背景,并具有适当的色标。

Codepen Demo Codepen演示

CSS CSS

.element {
  width:100px;
  height:100px;
  margin:50px auto;
  position: relative;
  background-color: lightblue;
}

.element:before {
  content:"";
  position: absolute;
  width:120%;
  height:120%;
  top:-20%;
  left:-20%;
  background: -webkit-radial-gradient(top left,red ,lightblue 50%, lightblue);
  background: -moz-radial-gradient(top left,red ,lightblue 50%, lightblue);
  background: radial-gradient(top left,red ,lightblue 50%, lightblue);
  z-index: -1;
}

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

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