With CSS properties, we can only control the thickness of border; not length.
However we can mimic border effect and control its width
and height
as we want with some other ways.
We can use linear-gradient()
to create a background image(s) and control its size and position with CSS so that it looks like a border. As we can apply multiple background images to an element, we can use this feature to create multiple border like images and apply on different sides of element. We can also cover the remaining available area with some solid color, gradient or background image.
Required HTML:
All we need is one element only (possibly having some class).
<div class="box"></div>
Steps:
linear-gradient()
.background-size
to adjust the width
/ height
of above created image(s) so that it looks like a border.background-position
to adjust position (like left
, right
, left bottom
etc.) of the above created border(s).Necessary CSS:
.box {
background-image: linear-gradient(purple, purple),
// Above css will create background image that looks like a border.
linear-gradient(steelblue, steelblue);
// This will create background image for the container.
background-repeat: no-repeat;
/* First sizing pair (4px 50%) will define the size of the border i.e border
will be of having 4px width and 50% height. */
/* 2nd pair will define the size of stretched background image. */
background-size: 4px 50%, calc(100% - 4px) 100%;
/* Similar to size, first pair will define the position of the border
and 2nd one for the container background */
background-position: left bottom, 4px 0;
}
Examples:
With linear-gradient()
we can create borders of solid color as well as having gradients. Below are some examples of border created with this method.
Example with border applied on one side only:
.container {_x000D_
display: flex;_x000D_
}_x000D_
.box {_x000D_
background-image: linear-gradient(purple, purple),_x000D_
linear-gradient(steelblue, steelblue);_x000D_
background-repeat: no-repeat;_x000D_
background-size: 4px 50%, calc(100% - 4px) 100%;_x000D_
background-position: left bottom, 4px 0;_x000D_
_x000D_
height: 160px;_x000D_
width: 160px;_x000D_
margin: 20px;_x000D_
}_x000D_
.gradient-border {_x000D_
background-image: linear-gradient(red, purple),_x000D_
linear-gradient(steelblue, steelblue);_x000D_
}
_x000D_
<div class="container">_x000D_
<div class="box"></div>_x000D_
_x000D_
<div class="box gradient-border"></div>_x000D_
</div>
_x000D_
Example with border applied on two sides:
.container {_x000D_
display: flex;_x000D_
}_x000D_
.box {_x000D_
background-image: linear-gradient(purple, purple),_x000D_
linear-gradient(purple, purple),_x000D_
linear-gradient(steelblue, steelblue);_x000D_
background-repeat: no-repeat;_x000D_
background-size: 4px 50%, 4px 50%, calc(100% - 8px) 100%;_x000D_
background-position: left bottom, right top, 4px 0;_x000D_
_x000D_
height: 160px;_x000D_
width: 160px;_x000D_
margin: 20px;_x000D_
}_x000D_
_x000D_
.gradient-border {_x000D_
background-image: linear-gradient(red, purple),_x000D_
linear-gradient(purple, red),_x000D_
linear-gradient(steelblue, steelblue);_x000D_
}
_x000D_
<div class="container">_x000D_
<div class="box"></div>_x000D_
_x000D_
<div class="box gradient-border"></div>_x000D_
</div>
_x000D_
Example with border applied on all sides:
.container {_x000D_
display: flex;_x000D_
}_x000D_
.box {_x000D_
background-image: linear-gradient(purple, purple),_x000D_
linear-gradient(purple, purple),_x000D_
linear-gradient(purple, purple),_x000D_
linear-gradient(purple, purple),_x000D_
linear-gradient(steelblue, steelblue);_x000D_
background-repeat: no-repeat;_x000D_
background-size: 4px 50%, 50% 4px, 4px 50%, 50% 4px, calc(100% - 8px) calc(100% - 8px);_x000D_
background-position: left bottom, left bottom, right top, right top, 4px 4px;_x000D_
_x000D_
height: 160px;_x000D_
width: 160px;_x000D_
margin: 20px;_x000D_
}_x000D_
_x000D_
.gradient-border {_x000D_
background-image: linear-gradient(red, purple),_x000D_
linear-gradient(to right, purple, red),_x000D_
linear-gradient(to bottom, purple, red),_x000D_
linear-gradient(to left, purple, red),_x000D_
linear-gradient(steelblue, steelblue);_x000D_
}
_x000D_
<div class="container">_x000D_
<div class="box"></div>_x000D_
_x000D_
<div class="box gradient-border"></div>_x000D_
</div>
_x000D_
Screenshot: