Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my first inline-block element have space above it

Tags:

html

css

I have inline-block elements inside of a container that should all be displayed starting at the top left of the container. For some reason the first element is displayed below where it should be. I've tried margin and padding resets but when inspected there is no margin or padding anyways.

here is the html (no spaces so they don't ruin the layout):

 <div class='nav'><div class='logo'><p>test</p></div><ul><li><a href='#'>Link</a></li><li><a href='#'>Link</a></li><li><a href='#'>Link</a></li><li><a href='#'>Link</a></li><li><a href='#'>Link</a></li></ul></div>

here is the css:

.nav {
    position: relative;
    width: 80%;
    height: 50px;
    background-color: red;
    z-index: 1;
}
.nav .logo {
    display: inline-block;
    width: 10%;
    height: 100%;
    background-color: #f90;

}
.nav ul {
    display: inline-block;
    width: 90%;
    text-align: center;
}
.nav li {
    display: inline-block;
    height: 100%;
    line-height: 50px;
    width: 20%;
    background-color: grey;
}
.nav li a {
    margin: 0;
}

here is a codepen showing the problem:

http://codepen.io/Wryte/pen/Guavp

like image 945
Wryte Avatar asked Mar 22 '13 01:03

Wryte


People also ask

Why does inline block add space?

That solution, however, ignores why that space is being added. The inline-block display property treats block level elements (e.g. <div> ) as an inline element (e.g. <span> ), and, just like if you had a line break between two <span> elements, the line-break between the <div> s is creating a space between the <div> s.

How do you remove extra space in CSS?

apply float:left and that will remove the space so the text doesn't have to be on 1 line.

How do I make no space in HTML?

A commonly used entity in HTML is the non-breaking space: &nbsp; A non-breaking space is a space that will not break into a new line. Two words separated by a non-breaking space will stick together (not break into a new line). This is handy when breaking the words might be disruptive.


1 Answers

They aren't lined up because their vertical alignments are baseline, if you set them to top they would line up:

.nav .logo {
    display: inline-block;
    vertical-align: top;
    width: 10%;
    height: 100%;
    background-color: #f90;

}
.nav ul {
    display: inline-block;
    vertical-align: top;
    width: 90%;
    text-align: center;
}

http://codepen.io/anon/pen/Kpthz

like image 104
Musa Avatar answered Oct 04 '22 18:10

Musa