[css] Add space between <li> elements

I have a nav-menu on which it seems that I can't add a space (margin: 3px;) between the <li> elements.

You can see the HTML and CSS code on this jsfiddle or below.

You will see that I've added a border-bottom: 2px solid #fff; to the #access li to simulate the space between elements, but that is not going to work because under the nav-menu I will have a bunch of different colors. If I add margin-button: 2px it doesn't work.

This is the HTML:

<nav id="access" role="navigation">
    <div class="menu-header-menu-container">
        <ul id="menu-header-menu" class="menu">
            <li id="menu-item-41" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-41">
                <a href="http://localhost:8888/fullstream/?page_id=5">About Us</a>
            </li>
            <li id="menu-item-35" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-35">
                <a href="http://localhost:8888/fullstream/?page_id=7">Services</a>
            </li>
            <li id="menu-item-34" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-34">
                <a href="http://localhost:8888/fullstream/?page_id=9">Environmental Surface Cleaning</a>
            </li>
            <li id="menu-item-33" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-33">
                <a href="http://localhost:8888/fullstream/?page_id=11">Regulations</a>
            </li>
            <li id="menu-item-32" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-32">
                <a href="http://localhost:8888/fullstream/?page_id=13">Contact Us</a>
            </li>
       </ul>
</div>

This is the CSS:

#access {
    background: #0f84e8; /* Show a solid color for older browsers */
    display: block;
    margin: 0 auto 6px 55px;
    position: absolute;
    top: 100px;
    z-index: 9999;
}
#access ul {
    font-size: 13px;
    list-style: none;
    margin: 0 0 0 -0.8125em;
    padding-left: 0;
}
#access li {
    position: relative;
    padding-left: 11px;
}
#access a {
    border-bottom: 2px solid #fff;
    color: #eee;
    display: block;
    line-height: 3.333em;
    padding: 0 10px 0 20px;
    text-decoration: none;
}

#access li:hover > a,
#access ul ul :hover > a,
#access a:focus {
    background: #efefef;
}
#access li:hover > a,
#access a:focus {
    background: #f9f9f9; /* Show a solid color for older browsers */
    background: -moz-linear-gradient(#f9f9f9, #e5e5e5);
    background: -o-linear-gradient(#f9f9f9, #e5e5e5);
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#f9f9f9), to(#e5e5e5)); /* Older webkit syntax */
    background: -webkit-linear-gradient(#f9f9f9, #e5e5e5);
    color: #373737;
}
#access ul li:hover > ul {
    display: block;
}

This question is related to css

The answer is


Since you are asking for space between , I would add an override to the last item to get rid of the extra margin there:

_x000D_
_x000D_
li {_x000D_
  background: red;_x000D_
  margin-bottom: 40px;_x000D_
}_x000D_
_x000D_
li:last-child {_x000D_
 margin-bottom: 0px;_x000D_
}_x000D_
_x000D_
ul {_x000D_
  background: silver;_x000D_
  padding: 1px;  _x000D_
  padding-left: 40px;_x000D_
}
_x000D_
<ul>_x000D_
<li>Item 1</li>_x000D_
<li>Item 1</li>_x000D_
<li>Item 1</li>_x000D_
<li>Item 1</li>_x000D_
<li>Item 1</li>_x000D_
</ul>
_x000D_
_x000D_
_x000D_

The result of it might not be visual at all times, because of margin-collapsing and stuff... in the example snippets I've included, I've added a small 1px padding to the ul-element to prevent the collapsing. Try removing the li:last-child-rule, and you'll see that the last item now extends the size of the ul-element.


#access a {
    border-bottom: 2px solid #fff;
    color: #eee;
    display: block;
    line-height: 3.333em;
    padding: 0 10px 0 20px;
    text-decoration: none;
}

I see that you had used line-height but you gave it to <a> tag instead of <ul> Try this:

#access ul {line-height:3.333em;}

You wouldn't need to play with margins then.


I just want to say guys:

Only Play With Margin

It is a lot easier to add space between <li> if you play with margin.


Most answers here are not correct as they would add bottom space to the last <li> as well, so they are not adding space ONLY in between <li> !

The most accurate and efficient solution is the following:

li.menu-item:not(:last-child) { 
   margin-bottom: 3px;  
}

Explanation: by using :not(:last-child) the style will be applie to all items (li.menu-item) but the last one.


You can use the margin property:

li.menu-item {
   margin:0 0 10px 0;   
}

Demo: http://jsfiddle.net/UAXyd/