Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

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,210 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

rydavim
rydavim
18,786 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>
</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.