Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to use border-radius together with a border-image which has a gradient?

Tags:

css

I'm styling an input field which has a rounded border (border-radius), and attempting to add a gradient to said border. I can successfully make the gradient and the rounded border, however neither work together. It's either rounded with no gradient, or a border with a gradient, but no rounded corners.

-webkit-border-radius: 5px; -webkit-border-image: -webkit-gradient(linear, 0 0, 0 100%, from(#b0bbc4), to(#ced9de)) 1 100%; 

Is there anyway to have both CSS properties work together, or is this not possible?

like image 218
paulwilde Avatar asked Apr 18 '11 18:04

paulwilde


People also ask

Can you use linear gradient on border?

And you can use all types of gradients: linear-gradient , radial-gradient and conic-gradient . However, there's a drawback to this approach. You cannot use border-radius property, as it is not supported with the border-image property.

How do you put a gradient on a border?

There are two methods to create gradient borders which are listed below: Method 1: Using border-image with gradient: The border is created by using the size and color as transparent in the border property. The gradient is used to define the border-image property.

How do you put a border-radius on a border-image in CSS?

CSS Syntaxborder-radius: 1-4 length|% / 1-4 length|%|initial|inherit; Note: The four values for each radius are given in the order top-left, top-right, bottom-right, bottom-left. If bottom-left is omitted it is the same as top-right. If bottom-right is omitted it is the same as top-left.


2 Answers

This is possible, and it does not require extra markup, but uses an ::after pseudo-element.

                                   screenshot

It involves putting a pseudo-element with a gradient background below and clipping that. This works in all current browsers without vendor prefixes or hacks (even IE), but if you want to support vintage versions of IE, you should either consider solid color fallbacks, javascript, and/or custom MSIE CSS extensions (i.e., filter, CSSPie-like vector trickery, etc).

Here's a live example (jsfiddle version):

@import url('//raw.githubusercontent.com/necolas/normalize.css/master/normalize.css');    html {      /* just for showing that background doesn't need to be solid */      background: linear-gradient(to right, #DDD 0%, #FFF 50%, #DDD 100%);      padding: 10px;  }    .grounded-radiants {      position: relative;      border: 4px solid transparent;      border-radius: 16px;      background: linear-gradient(orange, violet);      background-clip: padding-box;      padding: 10px;      /* just to show box-shadow still works fine */      box-shadow: 0 3px 9px black, inset 0 0 9px white;  }    .grounded-radiants::after {      position: absolute;      top: -4px; bottom: -4px;      left: -4px; right: -4px;      background: linear-gradient(red, blue);      content: '';      z-index: -1;      border-radius: 16px;  }
<p class="grounded-radiants">      Some text is here.<br/>      There's even a line break!<br/>      so cool.  </p>

The extra styling above is to show:

  • This works with any background
  • It works just fine with box-shadow, inset or not
  • Does not require you to add the shadow to the pseudo-element

Again, this works with IE, Firefox and Webkit/Blink browsers.

like image 149
Camilo Martin Avatar answered Sep 30 '22 10:09

Camilo Martin


Working on this same problem. Came across a non-svg solution which is more succinct than others here:

div{    width: 300px;    height: 80px;    border: double 1em transparent;    border-radius: 30px;    background-image: linear-gradient(white, white),                       linear-gradient(to right, green, gold);    background-origin: border-box;    background-clip: content-box, border-box;  }
<div></div>

This is not my own solution and has been taken from here: https://gist.github.com/stereokai/36dc0095b9d24ce93b045e2ddc60d7a0

like image 37
alphazwest Avatar answered Sep 30 '22 08:09

alphazwest