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

CSS

Al Lu
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree
Al Lu
UX Design Techdegree Student 15,801 Points

How add another border.

Hey. I want to add another red border after the green one, near "something3". (I changed the color of the border on purpose so you can see it better.) I'm not sure how to do it. Any ideas? Thanks http://jsfiddle.net/Ks4b6/

2 Answers

Kevin Korte
Kevin Korte
28,149 Points

Okay, I have a much better solution worked out.

I'd use the pseudo class :after to accomplish your goal. I also did a little bit of refactoring your CSS. By giving every list item a border left, and border right, and than removing the border left on the first list item, and adding the border right on the last list item, this will allow your list item, or navigation to grow. Whether you had 3 or 30 list items, they would all get the correct borders, without adding more code. As you had it, for every list item, it needed it's own CSS selector.

Here is the full link http://codepen.io/kevink/pen/yhxpA

I left your HTML untouched. You still have some issues if this is going to be responsive to fix.

Basically, these are the big changes.

As you can see, we gave each list item and left and right border. I also had to set the position to relative, so later on our absolutely positioned after pseudo element will position it's self to the list items.

#navbar ul li {
  padding: 24px 3em; 
  display: inline; 
  margin:-2px;
  border-right: 1px solid green;
  border-left: 1px solid red;
  position: relative;
}

Next, we had to go back and remove the border from the first list item. Easy peasy.

#navbar ul li:first-child {
  border-left: none;
}

Now the bread and butter. We create an after pseudo element on the last child. We position it absolutely, which will position itself relative to a parent with position relative. Remember how we set position relative earlier. This is why. Zeroing out the top, bottom, left, and right stretches our this pseudo element to be the same height and width of the list items. Now we can put a border on this element, but based on the CSS box model, borders go outside of the box model. We need this border to go on the outside, so we have to give it a right of -2px. This will push it to clear the 1px green border, and than give it enough room for itself as a 1px border.

#navbar ul li:last-child:after {
  content: "";
  border-right: 1px solid red;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: -2px;
}

This leaves us with no extra markup, which is just what we wanted. Hope that helps to answer your question.

Al Lu
seal-mask
.a{fill-rule:evenodd;}techdegree
Al Lu
UX Design Techdegree Student 15,801 Points

Hey It's working! Now before I'll say big thanks :P... I'm not really familiar with the "content" property, and It makes the the last li item (something 3) unclickable. You know how to get around this? Thanks for the help.

edit: Oh never mind! My bad. The original page going from right to left, like my native language and I forgot to change the

right: -2px;

to

left: -2px;

Thanks I learned something new! Good and effective CSS is not an easy feat and there is always more to learn.

Kevin Korte
Kevin Korte
28,149 Points

I forgot that the pseudo element was "covering" the anchor tag, making it unclickable. The z-index can help us fix that.

Check it out now. http://codepen.io/kevink/pen/yhxpA

Basically, what I added was z index of 1 here

#navbar ul li {
  z-index: 1;
}

And a negative z-index of 1 here

#navbar ul li:last-child:after {
  z-index: -1;
}

This rearranges our stacking order and moves the pusedo element that has the extra border to stack behind the list items, which have the anchor tags nested inside them. The anchors inherit the stacking order of their parent the list, thus making #3 clickable again.

The content tag is necessary any time you use :before or :after even if the content is just a set of empty parenthesis like I have. If you remove the content declaration from the CSS, you'll see the border no longer works. You can enter text, or codes for font symbols inside of that content tag, and it will be rendered to the browser.

Here is some extended reading. Once you get comfortable with pseudo classes you'll find yourself using them a lot.

http://css-tricks.com/almanac/properties/z/z-index/

http://css-tricks.com/pseudo-element-roundup/

I think those are two must read articles.

Kevin Korte
Kevin Korte
28,149 Points

I'm really not a fan of doing this, because it goes against good semantics, but you can put in an empty list item. So you'd have

<ul class=""> 
           <li><a href="index.html">Something</a></li> 
           <li><a href="index.html">Something2</a></li> 
           <li><a href="about.html">Something3</a></li> 
           <li></li>

        </ul> 

and than your CSS would have

#navbar ul li:nth-child(4) {
    border-left: 1px solid red;
}

Let me work on a better solution and I'll post a link here.