1

See this CodePen: http://codepen.io/anon/pen/KypbC.

HTML

<body> <nav> <ul> <li>One</li> <li>Two</li> <li>Three</li> </ul> </nav> </body> 

CSS

body { font-family: "Helvetica Neue"; } nav { height: 50px; background-color: #EEE; /* border: 1px solid #DDD; */} nav ul li { display: inline; padding: 20px; line-height: 50px; } .nav-inverse { background-color: black; color: lightGray; } 

I know it has something to do with the line-height property, but don't understand how it works.

Edit: I've read through the documentation of line-height, but don't understand it.

  • What is a "line box"?
  • What makes an inline element "replaced" vs. "not replaced"? ...
3
  • 1
    This isn't relative to the border. You are facing "collapsing margins" see this example with 0 margin set on the ul (remember browsers give a default margin to them) codepen.io/web-tiki/pen/yBtur Commented Oct 5, 2014 at 14:50
  • 1
    more about collapsing margins (scroll down to "Collapsing Margins Between Parent and Child Elements") sitepoint.com/web-foundations/collapsing-margins Commented Oct 5, 2014 at 14:55
  • Thanks, that's helpful! Commented Oct 5, 2014 at 14:57

2 Answers 2

1

The "issue" you are facing is related to collapsing margins between parent and child element.

Parent and first/last child

If there is no border, padding, inline content, or clearance to separate the margin-top of a block with the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block with the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.

source : MDN

The default styling of the <ul> element includes margin-top. It collapses with the parent container (nav) when you don't give a border to it.

This is why th ul element "moves" down when you set a border to it.

Sign up to request clarification or add additional context in comments.

Comments

1

you should use display:inline-block and always use box-sizing:border-box which gives border inside or it will be 100% width + border 2px (right + left) so it will go beyond 100% and scroller will appear

the issue was you gave line-height + padding so the height increased

Codepen

1 Comment

Why? I'm not particularly interested in getting it to work. I'm interested in learning why things work the way they do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.