Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

4 gradient borders in CSS

I need help applying a gradient border on all the 4 sides of a box. I tried it, but it only works for two sides. After looking at all the links and SO answers I have got this:

.test{
  height:250px;
  border: 2px solid;
  border-image: linear-gradient(to left,rgba(78,137,176,1)  1%, rgba(115,192,85,1)  100%) 100% 0 100% 0/2px 0 2px 0;
  padding-top:50px;
}
<div class="test">
  This is a box and I want borders for all the sides
</div>

I would appreciate any help. I am trying something similar to the image below. Thank you.

enter image description here

like image 643
rahul patel Avatar asked Feb 20 '17 14:02

rahul patel


1 Answers

Using background image: (produces the exact output as your image)

You seem to be having gradients that are different on each sides and so it is difficult to achieve this with the border-image property. You could try and mimic the behavior using background-image like in the below snippet.

Basically what the below snippet does is that it creates the gradient for each of the 4 sides as gradient background image strips and then uses background-position to place them on the correct location.

The transparent border on parent is a placeholder where the mimiced border would end up appearing. The background-origin: border-box makes the background of the element start from border-box area itself (and not padding-box or content-box). These two are just extra steps to avoid the usage of unnecessary calc stuff in the background-position.

.test {
  height: 250px;
  border: 2px solid transparent;
  background-image: linear-gradient(to right, rgb(187, 210, 224), rgb(203, 231, 190)), linear-gradient(to bottom, rgb(114, 191, 87), rgb(116, 191, 86)), linear-gradient(to left, rgb(204, 233, 187), rgb(187, 210, 224)), linear-gradient(to top, rgb(84, 144, 184), rgb(80, 138, 176));
  background-origin: border-box;
  background-size: 100% 2px, 2px 100%, 100% 2px, 2px 100%;
  background-position: top left, top right, bottom right, bottom left;
  background-repeat: no-repeat;
  padding-top: 50px;
}
<div class="test">
  This is a box and i want border for all the side
</div>

Using border image: (produces a border on all 4 sides but not same output as your image)

The best output that you could get with border-image property would be the below but as you can see from the demo it is not exactly the same as your image (or the first snippet's output):

.test {
  height: 250px;
  border: 2px solid;
  border-image: linear-gradient(to left, rgba(78, 137, 176, 1) 1%, rgba(115, 192, 85, 1) 100%);
  border-image-slice: 1;
  padding-top:50px;
}
<div class="test">
  This is a box and i want border for all the side
</div>
like image 78
Harry Avatar answered Sep 19 '22 19:09

Harry