Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


How to create a top/bottom border with less width than the element it self ?

How to create a top/bottom border with less width than the element it self ? also how to align it in the top centre or bottom centre?

thanks :)

2 Answers

John Lukacs
John Lukacs
26,806 Points

Have you tryed to define all four borders with the left and right really thick and then hiding the left and right. border-right: 30px hidden

18,813 Points

There isn't really an elegant, easy way to do this using plain CSS. You can achieve the effect by using a div with it's height, width, and background color set to the border styles you want.

<div id="border-test">
  <h1>This is a fancy heading!</h1>
  <div id="smaller-border"></div>
h1 {
  font-family: cursive;
  font-size: 1.75rem;
  text-align: center;

#smaller-border {
  height: 0.125rem;
  width: 12rem;
  margin: -0.75rem auto 0 auto;
  background-color: #000;
  border-radius: 5px;

Certainly not the prettiest code, but it should give the effect you want. You can then position the div above or below the element you want to have a border.