Is there any way that I can have 4 different colours on one side of a border in CSS? I currently have
#header
{
border-color:#88a9eb;
}
I w
You can use the border-image
property to create a gradient border with 4 colors. Normally gradients move gradually from one color to another and it produces a blur like effect but setting the color-stops (the percentage values) such that the end-point of one color is same as the starting point of the next color makes the colors come to a hard stop and thus end up producing block like effects.
The border can be set to the required side by changing the border-image-width
and the direction of the gradient. For example, top & bottom borders would need the gradient to go from left to right while left & right borders would need the gradient to go from top to bottom.
The gradients use percentage values for the size (and color-stop) and hence they are responsive by default and can adapt automatically even if the container's dimensions change.
The only drawback to using border-image
is the poor browser support for this property at present. IE10- do not support this property.
.bordered-top {
border-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
border-image-slice: 1;
border-image-width: 4px 0px 0px 0px;
}
.bordered-bottom {
border-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
border-image-slice: 1;
border-image-width: 0px 0px 4px 0px;
}
.bordered-left {
border-image: linear-gradient(to bottom, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
border-image-slice: 1;
border-image-width: 0px 0px 0px 4px;
}
.bordered-right {
border-image: linear-gradient(to bottom, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
border-image-slice: 1;
border-image-width: 0px 4px 0px 0px;
}
div {
height: 100px;
width: 300px;
padding: 10px;
background: beige;
margin: 10px;
}
Border only on top
Border only on bottom
Border only on left
Border only on right
For IE10+ support, you could mimic the same behavior by using gradients for the background-image
property instead of border-image
like in the below snippet.
Unlike with border-image
, here the side on which the border is applied cannot be controlled using the border-image-width
and we have to use background-position
instead to position the image at the required position.
The background-size
determines the thickness of the border. For top & bottom borders, the size in x-axis should be 100% and that in y-axis is the thickness of the border. For left & right borders, the size in y-axis should be 100% and that in x-axis is the thickness of the border.
.bordered-top {
background-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
background-size: 100% 4px;
background-repeat: no-repeat;
background-position: 0% 0%;
}
.bordered-bottom {
background-image: linear-gradient(to right, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
background-size: 100% 4px;
background-repeat: no-repeat;
background-position: 0% 100%;
}
.bordered-left {
background-image: linear-gradient(to bottom, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
background-size: 4px 100%;
background-repeat: no-repeat;
background-position: 0% 0%;
}
.bordered-right {
background-image: linear-gradient(to bottom, rgb(139, 191, 64) 25%, rgb(230, 27, 33) 25%, rgb(230, 27, 33) 50%, rgb(124, 196, 236) 50%, rgb(124, 196, 236) 75%, rgb(254, 181, 17) 75%);
background-size: 4px 100%;
background-repeat: no-repeat;
background-position: 100% 0%;
}
div {
height: 100px;
width: 300px;
padding: 10px;
background: beige;
margin: 10px;
}
Border only on top
Border only on bottom
Border only on left
Border only on right