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
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.
apply float:left and that will remove the space so the text doesn't have to be on 1 line.
A commonly used entity in HTML is the non-breaking space: 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With