Actually you are facing an inheritance issue. When defining the line-height
on the header it will get inherited by the li
and the a
. li
is an inline-block
element and its height is defined by the fixed height you added (or by default the height of its content)
The a
element is an inline element and its height is defined by its line-height (inherited from the li
) and logically you will have an overflow since it will be bigger than the height of li
(81px > 36px)
. If you remove the fixed height from the li
its height will be equal to 81px
which is the height of its content a
.
To avoid this you simply need to remove the inheritance by making the line-height
of li
to be initial
(the a
will also inherit it) and remove the fixed height:
* {
margin: 0;
padding: 0;
}
header {
width: 100%;
height: 81px;
background: #669999;
line-height: 81px;
}
header nav {
background: green;
}
header nav ul {
background: yellow;
}
header nav li {
display: inline-block;
margin-left: 20px;
width: 180px;
line-height: initial;
background: #006666;
margin-top: 20px;
margin: auto;
}
header nav li a {
background: red;
margin: auto;
}
<header>
<nav>
<ul>
<li><a href="">About</a></li>
<li><a href="">Works</a></li>
<li><a href="">Contact</a></li>
</ul>
</nav>
</header>
In case you want to define a fixed height to li
simple specify its line height equal to its fixed height:
* {
margin: 0;
padding: 0;
}
header {
width: 100%;
height: 81px;
background: #669999;
line-height: 81px;
}
header nav {
background: green;
}
header nav ul {
background: yellow;
}
header nav li {
display: inline-block;
margin-left: 20px;
width: 180px;
height:36px;
line-height: 36px;
background: #006666;
margin-top: 20px;
margin: auto;
}
header nav li a {
background: red;
margin: auto;
}
<header>
<nav>
<ul>
<li><a href="">About</a></li>
<li><a href="">Works</a></li>
<li><a href="">Contact</a></li>
</ul>
</nav>
</header>