How to create multi-color border like image below?
Asked
Active
Viewed 6.5k times
40

Temani Afif
- 245,468
- 26
- 309
- 415

Hamed mayahian
- 2,465
- 6
- 27
- 49
-
1http://jsfiddle.net/yH59y/ – Roysh Aug 09 '16 at 12:17
-
2you can use a border image: http://www.w3schools.com/cssref/css3_pr_border-image.asp – Andrew Aug 09 '16 at 12:17
-
1try this: http://www.hongkiat.com/blog/css-gradient-border/ – Sanjeev Kumar Aug 09 '16 at 12:17
3 Answers
74
You can do it without pseudo-elements
, just with border-image: linear-gradient
.fancy-border {
width: 150px;
height: 150px;
text-align:center;
border-top: 5px solid;
border-image: linear-gradient(to right, grey 25%, yellow 25%, yellow 50%,red 50%, red 75%, teal 75%) 5;
}
<div class="fancy-border">
my content
</div>

Theodore K.
- 5,058
- 4
- 30
- 46
-
7This should be the top answer, it's way less hacky than the currently accepted one. – Erick Aug 28 '17 at 20:57
-
-
2@EdgarAroutiounian just edit the percentages to match. If you only want 3 colors, then divide "100%/3" wich will be 33,33%. Insted of 25% then specify "33%" and so on and you will have only three colors. – Rasmus Lauridsen Sep 01 '18 at 09:33
-
2
-
1@vik I think it's the slice parameter, https://www.w3schools.com/cssref/css3_pr_border-image-slice.asp – Theodore K. Jun 28 '19 at 10:08
46
You can do it with :after
or :before
psuedo element and css linear-gradient
as shown below:
body {
background: #ccc;
}
.box {
text-align: center;
position: relative;
line-height: 100px;
background: #fff;
height: 100px;
width: 300px;
}
.box:after {
background: linear-gradient(to right, #bcbcbc 25%,#ffcd02 25%, #ffcd02 50%, #e84f47 50%, #e84f47 75%, #65c1ac 75%);
position: absolute;
content: '';
height: 4px;
right: 0;
left: 0;
top: 0;
}
<div class="box">Div</div>

Mohammad Usman
- 37,952
- 20
- 92
- 95
1
Try it.
.test {
width: 500px;
height: 100px;
background-color: #ccc;
position: relative;
}
.test:before,
.test:after {
content: "";
position: absolute;
left: 0px;
right: 0px;
height: 10px;
background-image: -webkit-linear-gradient(0deg, red 20px, blue 20px, blue 40px, yellow 40px, yellow 60px, green 60px, green 80px);
background-image: -ms-linear-gradient(0deg, red 20px, blue 20px, blue 40px, yellow 40px, yellow 60px, green 60px, green 80px);
background-size: 80px;
}
.test:before {
top: 0px;
}
.test:after {
bottom: 0px;
}
<div class="test"></div>

M.A.K. Ripon
- 2,070
- 3
- 29
- 47

Neeraj Prajapati
- 541
- 5
- 24
-
-
for firefox: -webkit-border-image: -webkit-gradient(linear, left top, left bottom, from(#00abeb), to(#fff), color-stop(0.5, #fff), color-stop(0.5, #66cc00)) 21 30 30 21 repeat repeat; – Neeraj Prajapati Aug 09 '16 at 12:39