How to implement a gradient border with rounded corners in CSS? The following article will introduce to you several ways to use CSS to cleverly implement gradient borders with rounded corners. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
How to achieve the following gradient border effect:
This problem itself is not difficult, and there are some ways to achieve it. , mainly because there are some details that need to be paid attention to.
##border-image is CSS Specification
CSS Backgrounds and Borders Module Level 3 (the latest version of the official specification for background and borders) adds a new attribute value.
As the name suggests, we can add image to the border element, similar to background-image, which can be a picture or a gradient, and is no longer limited to solid color.
Use border-image to achieve gradient border
With border- After image, it becomes very convenient to implement gradient borders
However, the syntax of border-image is introduced too much, readers need to understand it by themselves.
The implementation is as follows:
<div class="border-image"></div>
.border-image { width: 200px; height: 100px; border-radius: 10px; border-image-source: linear-gradient(45deg, gold, deeppink); border-image-slice: 1; border-image-repeat: stretch; }
The above three attributes of border-image can be abbreviated as border-image: linear-gradient(45deg, gold, deeppink) 1;
The following results are obtained:
Look carefully at the demo above and set border-radius: 10px But The actual performance does not have rounded corners. The biggest problem with using
border-image is that the set
border-radius will become invalid.
We cannot get a gradient border with rounded corners. For the reason, check the official specification W3C and explain it as follows:
A box's backgrounds, but not its border-image, are clipped to the appropriate curve (as determined by 'background-clip' ). Other effects that clip to the border or padding edge (such as 'overflow' other than 'visible') also must clip to the curve. The content of replaced elements is always trimmed to the content edge curve. Also, the area outside the curve of the border edge does not accept mouse events on behalf of the element.
For this, we have to find another way or slightly improve it to get a gradient border with rounded corners .
The first method, we No longer use border-image, but use the
background-image pseudo-element solution. This is also the most commonly used method when the
border-image specification does not appear.
Very simple, the simple diagram is as follows:
Use background-image Implement a gradient background, and then overlay a white background to form a gradient border.
缺点
这个方案有两个问题,第一个是多使用了两个元素(当然在这里是 ::before 和 ::after),其次最致命的是,如果要求边框內的背景是透明的,此方案便行不通了。
第二种方法,使用 background-clip: content-box
以及 background-clip: border-box
配合使用。
background-clip:background-clip 设置元素的背景(背景图片或颜色)是否延伸到边框下面。它的部分取值和 box-sizing
类似。其中,
background-clip: border-box
表示设置的背景 background-image
将延伸至边框background-clip: content-box
表示设置的背景 background-image
被裁剪至内容区(content box)外沿这里,我们使用设置两个 background-image
,设置两个 background-clip
,并且将 border 设置为透明即可:
核心 CSS:
div { width: 200px; height: 100px; border: solid 10px transparent; border-radius: 10px; background-image: linear-gradient(#fee, #fee), linear-gradient(to right, green, gold); background-origin: border-box; background-clip: content-box, border-box; }
因为用到了 background-clip: border-box
,所以还需要 background-origin: border-box
使图案完整显示,可以尝试下关掉这个属性,即可明白为什么需要这样做。
缺点
与第一种方法类似,如果要求边框內的背景是透明的,此方案便行不通了。
这个方法也很好理解,既然设置了 background-image
的元素的 border-radius
失效。那么,我们只需要给它加一个父容器,父容器设置 overflow: hidden
+ border-radius
即可:
<div class="border-image-overflow"></div>
.border-image-pesudo { position: relative; width: 200px; height: 100px; border-radius: 10px; overflow: hidden; } .border-image-pesudo::before { content: ""; position: absolute; width: 200px; height: 100px; top: 50%; left: 50%; transform: translate(-50%, -50%); border: 10px solid; border-image: linear-gradient(45deg, gold, deeppink) 1; }
效果如下:
当然,这里还是多借助了一个元素实现。还有一种方法,可以不使用多余元素实现:
设置了 background-image
的元素的 border-radius
失效。但是在 CSS 中,还有其它方法可以产生带圆角的容器,那就是借助 clip-path
。
[clip-path](https://developer.mozilla.org/zh-CN/docs/Web/CSS/clip-path)
,一个非常有意思的 CSS 属性。
clip-path CSS 属性可以创建一个只有元素的部分区域可以显示的剪切区域。区域内的部分显示,区域外的隐藏。剪切区域是被引用内嵌的URL定义的路径或者外部 SVG 的路径。
简而言之,这里,我们只需要在 border-image
的基础上,再利用 clip-path
裁剪出一个带圆角的矩形容器即可:
<div class="border-image-clip-path"></div>
.border-image-clip-path { position: relative; width: 200px; height: 100px; border: 10px solid; border-image: linear-gradient(45deg, gold, deeppink) 1; clip-path: inset(0 round 10px); }
解释一下:clip-path: inset(0 round 10px)
。
inset(0 round 10px)
可以理解为,实现一个父容器大小(完全贴合,垂直水平居中于父容器)且 border-radius: 10px
的容器,将这个元素之外的所有东西裁剪掉(即不可见)。非常完美,效果如下:
当然,还可以利用 filter: hue-rotate()
顺手再加个渐变动画:
你可以在我 CSS-Inspiration 看到这个例子:
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of Use CSS to implement rounded gradient borders. For more information, please follow other related articles on the PHP Chinese website!