There are essentially two ways to achieve this: You can either use display:flex;
or display:inline-block;
.
Using Flex will ensure that your items are displayed side by side with equal spacing (note the CSS rule in the example: justify-content:space-between
), while using inline-block allows you to center align your menu items and manually add spacing with margins.
I recommend using Flex as it appears to be better suited to your needs. You can see the difference between the two methods in this fiddle, where examples are provided below.
Using Flex:
.header ul {
list-style: none;
padding: 0;
margin: 0;
display:flex; /* Focus here */
flex-wrap: wrap; /* Focus here */
justify-content: space-between; /* Focus here */
background-color: rgba(25,57,125, 0.8);
}
.header li {
display: block;
border: 1px solid #000000;
padding: 2px;
}
<div class="header">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3 has a long name</li>
<li>Item 4</li>
<li>One more Item</li>
</ul>
Using Inline Block
.header ul {
list-style: none;
padding: 0;
margin: 0;
text-align: center; /* Focus here */
background-color: rgba(125,57,125, 0.8);
}
.header li {
border: 1px solid #000000;
padding: 2px;
display: inline-block; /* Focus here */
margin: 0 5px;
}
<div class="header">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3 has a long name</li>
<li>Item 4</li>
<li>One more Item</li>
</ul>