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
##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:
border-radius is invalid
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 .
Method 1: background-image pseudo element
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 实现
第二种方法,使用 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
使图案完整显示,可以尝试下关掉这个属性,即可明白为什么需要这样做。
缺点
与第一种方法类似,如果要求边框內的背景是透明的,此方案便行不通了。
法三:border-image + overflow: hidden
这个方法也很好理解,既然设置了 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; }
效果如下:
当然,这里还是多借助了一个元素实现。还有一种方法,可以不使用多余元素实现:
法四:border-image + clip-path
设置了 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)
。
- clip-path: inset() 是矩形裁剪
- inset() 的用法有多种,在这里
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!

Here's a container with some child elements:

Flyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should

"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee

In this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.

The short answer: flex-shrink and flex-basis are probably what you’re lookin’ for.

In this week's look around the world of web platform news, Google Search Console makes it easier to view crawled markup, we learn that custom properties

The IndieWeb is a thing! They've got a conference coming up and everything. The New Yorker is even writing about it:


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)