search
HomeWeb Front-endCSS TutorialUse CSS to implement rounded gradient borders

Use CSS to implement rounded gradient borders

Jan 27, 2021 pm 07:29 PM
cssrounded corners

Use CSS to implement rounded gradient borders

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:

Use CSS to implement rounded gradient borders

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:

Use CSS to implement rounded gradient borders

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 CSS to implement rounded gradient borders

Use background-image Implement a gradient background, and then overlay a white background to form a gradient border.

CodePen Demo -- bg + overflow 实现渐变边框 

缺点

这个方案有两个问题,第一个是多使用了两个元素(当然在这里是 ::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;
}

Use CSS to implement rounded gradient borders

因为用到了 background-clip: border-box,所以还需要 background-origin: border-box 使图案完整显示,可以尝试下关掉这个属性,即可明白为什么需要这样做。

CodePen Demo -- background-clip 实现渐变边框

缺点

与第一种方法类似,如果要求边框內的背景是透明的,此方案便行不通了。

法三: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;
}

效果如下:

Use CSS to implement rounded gradient borders

当然,这里还是多借助了一个元素实现。还有一种方法,可以不使用多余元素实现:

法四: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 的容器,将这个元素之外的所有东西裁剪掉(即不可见)。

非常完美,效果如下:

Use CSS to implement rounded gradient borders

当然,还可以利用 filter: hue-rotate()顺手再加个渐变动画:

Use CSS to implement rounded gradient borders

你可以在我 CSS-Inspiration 看到这个例子:

CSS-Inspiration -- 使用 clip-path 和 border-image 实现圆角渐变边框

更多编程相关知识,请访问:编程视频!!

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!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
A Little Reminder That Pseudo Elements are Children, Kinda.A Little Reminder That Pseudo Elements are Children, Kinda.Apr 19, 2025 am 11:39 AM

Here's a container with some child elements:

Menus with 'Dynamic Hit Areas'Menus with 'Dynamic Hit Areas'Apr 19, 2025 am 11:37 AM

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

Improving Video Accessibility with WebVTTImproving Video Accessibility with WebVTTApr 19, 2025 am 11:27 AM

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

Weekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteWeekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteApr 19, 2025 am 11:25 AM

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.

Making width and flexible items play nice togetherMaking width and flexible items play nice togetherApr 19, 2025 am 11:23 AM

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

Position Sticky and Table HeadersPosition Sticky and Table HeadersApr 19, 2025 am 11:21 AM

You can't position: sticky; a

Weekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryWeekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryApr 19, 2025 am 11:18 AM

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

IndieWeb and WebmentionsIndieWeb and WebmentionsApr 19, 2025 am 11:16 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

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

SublimeText3 Mac version

God-level code editing software (SublimeText3)