How to create a gradient border using CSS? 5 ways to share
How to create a gradient border using CSS? The following article will share with you 5 ways to implement gradient borders with CSS. I hope it will be helpful to you!
Setting a gradient color for the border is a very common effect. There are many ideas to achieve this effect. Today I list the methods I know for your reference. (Learning video sharing: css video tutorial, web front-end)
1. Use border-image
CSS provided The border-image attribute is used to draw complex patterns for the border. Similar to background-image, we can display image
and linear- gradient
.
Setting the gradient color border through border-image
is the simplest method, which only requires two lines of code:
CSS:
div { border: 4px solid; border-image: linear-gradient(to right, #8f41e9, #578aef) 1; } /* 或者 */ div { border: 4px solid; border-image-source: linear-gradient(to right, #8f41e9, #578aef); border-image-slice: 1; }
Codepen demo
https://codepen.io/mudontire/pen/xxLxeZw
Although this method is simple, it has an obvious flaw and is not supported. Set border-radius
. Next, we will introduce several methods to support border-radius
.
2. Use background-image
It should be easiest to use background-image
to draw a gradient background and cover the middle with a solid color One method that comes to mind is: use two boxes to overlap, set a gradient background and padding for the lower box, and set a solid color background for the upper box.
HTML:
<div class="border-box border-bg"> <div class="content"> Lorem ipsum dolor, sit amet consectetur adipisicing elit. Iste ratione necessitatibus numquam sunt nihil quos saepe sit facere. Alias accusamus voluptate accusantium facere fugiat animi temporibus adipisci! Corporis, accusamus tempora. </div> </div>
CSS:
.border-box { width: 300px; height: 200px; margin: 25px 0; } .border-bg { padding: 4px; background: linear-gradient(to right, #8f41e9, #578aef); border-radius: 16px; } .content { height: 100%; background: #222; border-radius: 13px; /*trciky part*/ }
Codepen demo
https:/ /codepen.io/mudontire/pen/ZEJEZoY
The advantage of this method is that it is easy to understand and has good compatibility. The disadvantage is that setting the content border-radius
will be tricky. and inaccurate.
3. Two-layer elements, background-image
, background-clip
In order to solve border-radius## in method 2 # For inaccuracies, you can use a separate element as the gradient background on the bottom layer, and set a transparent border and solid color background on the upper layer (you need to set background-clip: padding-box to avoid covering the border of the lower element) , set the same
border-radius on the upper and lower layers.
HTML:
<div class="border-box"> <div class='border-bg'></div> <div class="content"> Lorem ipsum dolor, sit amet consectetur adipisicing elit. Iste ratione necessitatibus numquam sunt nihil quos saepe sit facere. Alias accusamus voluptate accusantium facere fugiat animi temporibus adipisci! Corporis, accusamus tempora. </div> </div>
CSS:
.border-box { border: 4px solid transparent; border-radius: 16px; position: relative; background-color: #222; background-clip: padding-box; /*important*/ } .border-bg { position: absolute; top: 0; right: 0; left: 0; bottom: 0; z-index: -1; margin: -4px; border-radius: inherit; /*important*/ background: linear-gradient(to right, #8f41e9, #578aef); }
Codepen demohttps:/ /codepen.io/mudontire/pen/yLoLrxL4. Pseudo elements, simplification of method 3We can use pseudo elements to replace
div.border-bg To simplify the HTML structure.
HTML:
<div class="border-box"> <div class="content"> Lorem ipsum dolor, sit amet consectetur adipisicing elit. Iste ratione necessitatibus numquam sunt nihil quos saepe sit facere. Alias accusamus voluptate accusantium facere fugiat animi temporibus adipisci! Corporis, accusamus tempora. </div> </div>
CSS:
.border-box { border: 4px solid transparent; border-radius: 16px; position: relative; background-color: #222; background-clip: padding-box; /*important*/ } .border-box::before { content: ''; position: absolute; top: 0; right: 0; left: 0; bottom: 0; z-index: -1; margin: -4px; border-radius: inherit; /*important*/ background: linear-gradient(to right, #8F41E9, #578AEF); }
Codepen demohttps:/ /codepen.io/mudontire/pen/JjyjVwN5. Single-layer elements,
background-clip,
background-origin,
background- image
Finally, I think the most elegant method is to use a single-layer element and set background-clip and
background-origin# respectively. ##, background-image
Each of these three attributes sets two sets of values. The first set is used to set the monochrome background in the border, and the second set is used to set the gradient color on the border.
<div class="border-box">
<div class="content">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Iste ratione necessitatibus numquam sunt nihil quos
saepe sit facere. Alias accusamus voluptate accusantium facere fugiat animi temporibus adipisci! Corporis,
accusamus tempora.
</div>
</div>
https:/ /codepen.io/mudontire/pen/wvqvZZO For more programming-related knowledge, please visit: The above is the detailed content of How to create a gradient border using CSS? 5 ways to share. For more information, please follow other related articles on the PHP Chinese website!.border-box {
border: 4px solid transparent;
border-radius: 16px;
background-clip: padding-box, border-box;
background-origin: padding-box, border-box;
background-image: linear-gradient(to right, #222, #222), linear-gradient(90deg, #8F41E9, #578AEF);
}

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.
