CSS border attributes allow you to set the border style, width, and color of an element.
Using the border-image property
The most basic way to apply a gradient to a border is to utilize the border-image property (similar to how background gradients are applied):
border: 15px solid;
border-image-source: linear-gradient(to left, #00c853, #b2ff59);
- The '1' following the linear-gradient statement represents the border-image-slice value. We designate a single border region by using the number 1.
The disadvantage of this technique is that border-image and border-radius cannot be combined ⛔, This means that border-radius property will not work.
Using the pseudo selector
padding: 30% 2em;
background-clip: padding-box; /* !importanté */
border: solid $border transparent; /* !importanté */
margin: -$border; /* !importanté */
border-radius: inherit; /* !importanté */
background: linear-gradient(to right, red, orange);
Using the background-clip approach
There is an alternate solution that is consistent with the border-radius property and is based on the background-clip property.
background: linear-gradient(white, white) padding-box, linear-gradient(
border: 3px solid transparent;
- The background-clip values are represented by the 'padding-box' and 'border-box' values supplied after the linear gradients.
The initial linear gradient is restricted to the padding box (i.e., all the content of the element, except the borders). Because we're using the same color twice, it's a 'fake' gradient; we're using it to mask the second background gradient with the background color.
The second gradient is placed beneath the first and is used to apply the gradient colors. The second gradient, in contrast to the first, encompasses the border region (background-clip: border-box).
Finally, a translucent border is needed to distinguish the border-box from the padding box (otherwise they would be identical and the first gradient would completely cover the second one).
Thank you for coming this far with me today; I hope it was useful to you, and I hope to see you again in future techniques and courses.
Happy coding 🎉