巧妙實現帶圓角的漸變邊框


如何實現下面這個漸變的邊框效果:

這個問題本身不難,實現的方法也有一些,主要是有一些細節需要注意。

 

border-image

border-image 是 CSS 規范 CSS Backgrounds and Borders Module Level 3 (最新一版的關於 background 和 border 的官方規范) 新增的一個屬性值。

顧名思義,我們可以給 border 元素添加 image,類似於 background-image,可以是圖片也可以是漸變,不再局限於純色。

 

使用 border-image 實現漸變邊框

有了 border-image 之后,實現漸變邊框變得很方便

不過多介紹 border-image 的語法,讀者需要自行了解一下。

實現如下:

<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;
}

上面關於 border-image 的三個屬性可以簡寫為 border-image: linear-gradient(45deg, gold, deeppink) 1;

得到如下結果:

 

border-radius 失效

仔細看上面的 Demo,設置了 border-radius: 10px 但是實際表現沒有圓角。使用 border-image 最大的問題在於,設置的 border-radius 會失效。

我們無法得到一個帶圓角的漸變邊框。原因,查看官方規范 W3C 解釋如下:

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.

為此,我們得另辟蹊徑或者稍加改進,得到帶圓角的漸變邊框。

 

法一:background-image + 偽元素

第一種方法,我們不再使用 border-image ,而是使用 background-image + 偽元素 的方案,這也是在 border-image 規范沒有出現最常用的方法。

非常簡單,簡單的示意圖如下:

利用 background-image 實現一個漸變背景,再通過疊加一個白色背景使之形成一個漸變邊框。

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;
}

因為用到了 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;
} 

效果如下:

當然,這里還是多借助了一個元素實現。還有一種方法,可以不使用多余元素實現:

 

法四: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 看到這個例子:

CSS-Inspiration -- 使用 clip-path 和 border-image 實現圓角漸變邊框

 

最后

好了,本文到此結束,希望對你有幫助 :)

更多精彩 CSS 技術文章匯總在我的 Github -- iCSS ,持續更新,歡迎點個 star 訂閱收藏。

更多精彩有趣的 CSS 效果,歡迎來這里看看 CSS 靈感 -- 在這里找到寫 CSS 的靈感

如果還有什么疑問或者建議,可以多多交流,原創文章,文筆有限,才疏學淺,文中若有不正之處,萬望告知。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM