You can also use inline-table
alongside table-cell
if you want to center your items vertically and horizontally. Below an example of using those display properties to make a menu:
.menu {_x000D_
background-color: lightgrey;_x000D_
height: 30px; /* calc(16px + 12px * 2) */_x000D_
}_x000D_
_x000D_
.menu-container {_x000D_
margin: 0px;_x000D_
padding: 0px;_x000D_
padding-left: 10px;_x000D_
padding-right: 10px;_x000D_
height: 100%;_x000D_
}_x000D_
_x000D_
.menu-item {_x000D_
list-style-type: none;_x000D_
display: inline-table;_x000D_
height: 100%;_x000D_
}_x000D_
_x000D_
.menu-item a {_x000D_
display: table-cell;_x000D_
vertical-align: middle;_x000D_
padding-left: 2px;_x000D_
padding-right: 2px;_x000D_
text-decoration: none;_x000D_
color: initial;_x000D_
}_x000D_
_x000D_
.text-upper {_x000D_
text-transform: uppercase;_x000D_
}_x000D_
_x000D_
.text-bold {_x000D_
font-weight: bold;_x000D_
}
_x000D_
<header>_x000D_
<nav class="menu">_x000D_
<ul class="menu-container">_x000D_
<li class="menu-item text-upper text-bold"><a href="javascript:;">StackOverflow</a></li>_x000D_
<li class="menu-item"><a href="javascript:;">Getting started</a></li>_x000D_
<li class="menu-item"><a href="javascript:;">Tags</a></li>_x000D_
</ul>_x000D_
</nav>_x000D_
</header>
_x000D_
It works by setting display: inline-table;
to all the <li>
, and then applying display: table-cell;
and vertical-align: middle;
to the children <a>
. This gives us the power of <table>
tag without using it.
This solution is useful if you do not know the height of your element.
The compatibilty is very good (relative to caniuse.com), with Internet Explorer >= 8.