CSS
How to Reduce Border Length in CSS: A Complete Guide
Borders in CSS play a crucial role in enhancing the design of web elements. However, sometimes you may need to reduce the border length rather than having it span the full width or height of an element.
This blog will explore different CSS techniques to control and shorten the length of borders effectively.
1. Using width
or height
to Control Border Length
By default, a border applies to the entire element. To reduce its length, you need to control the element’s width or height.
Example: Reducing Horizontal Border Length
.short-border {
width: 50px; /* Controls the border length */
border-bottom: 2px solid black;
}
<div class="short-border"></div>
✅ This creates a short horizontal border of 50px width instead of spanning the full width of the page.
2. Using display: inline-block
to Reduce Border Length
If you are applying a border to inline elements (like <span>
), you can use display: inline-block
to control the width.
Example: Short Border Under Text
.short-border-text {
display: inline-block;
border-bottom: 3px solid blue;
padding-bottom: 5px;
width: 100px;
text-align: center;
}
<span class="short-border-text">Short Border</span>
✅ This ensures the border only extends under the text, not the entire width of the container.
3. Using ::before
and ::after
Pseudo-Elements
You can create a short border effect by using ::before
or ::after
pseudo-elements instead of applying a border directly.
Example: Short Border Below Heading
h2::after {
content: "";
display: block;
width: 80px; /* Shorter border */
height: 3px; /* Border thickness */
background: red;
margin: 5px auto; /* Centers the border */
}
<h2>Section Title</h2>
✅ This technique is great for adding stylish section dividers.
4. Using border-image
for Custom Border Lengths
If you want more precise control over your borders, border-image
allows you to set a shortened border with images or gradients.
Example: Short Dashed Border
.box {
border: 5px solid transparent;
border-image: linear-gradient(to right, black, transparent) 1;
}
<div class="box">Short Border with Gradient</div>
✅ This creates a border that fades out at the edges, appearing shorter.
5. Using background
Instead of Border
Instead of applying a border directly, you can simulate a border effect using a background
with limited width.
Example: Fake Border Under a Button
.button {
background: none;
border: none;
position: relative;
font-size: 18px;
}
.button::after {
content: "";
display: block;
width: 60px;
height: 3px;
background: black;
margin: auto;
}
<button class="button">Click Me</button>
✅ This method is commonly used for stylish buttons and navigation menus.
Conclusion
Reducing border length in CSS is simple when you use the right approach. Here’s a quick summary:
✅ Use width
or height
to limit the length.
✅ Apply display: inline-block
for text elements.
✅ Use ::before
and ::after
pseudo-elements for decorative borders.
✅ Use border-image
for gradient-style borders.
✅ Use a background
instead of a border for precise control.
By mastering these techniques, you can achieve clean, modern, and customized border effects in your designs.