Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS/HTML - Horizontal list - Remove this mystery gap between list items?

Tags:

html

css

I am having trouble with a menu bar that I am making. It seems that there is a gap between the menu items and for the life of me I do not understand what the reason for this is.

As a description to the screenshot below, the first link (home) is the current page and it is highlighted. The second link (page1) is a hover effect while my cursor is over this item. You will notice that there is a gap (what on earth is causing this?!) between these two items that shows the background color of div that contains the menu.

It may be worthwhile to note that I am using the latest version of firefox.

Here is a screenshot of my problem:

Mystery Gap

Here is the html for the list:

<div class="nav">
    <ul>
        <li class="selectedPage"><a href="#">HOME</a></li>
        <li><a href="#">PAGE1</a></li>
        <li><a href="#">PAGE2</a></li>
    </ul>
<!-- end .nav --></div>

And here is the css:

div.nav {
    width: 750px;
    background: #52b5f0; /* Old browsers */
    /* IE9 SVG, needs conditional override of 'filter' to 'none' */
    background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSI1JSIgc3RvcC1jb2xvcj0iIzUyYjVmMCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjQ5JSIgc3RvcC1jb2xvcj0iIzM2OTlkMCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijk1JSIgc3RvcC1jb2xvcj0iIzE5NjM4YSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=);
    background: -moz-linear-gradient(top,  #52b5f0 5%, #3699d0 49%, #19638a 95%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(5%,#52b5f0), color-stop(49%,#3699d0), color-stop(95%,#19638a)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  #52b5f0 5%,#3699d0 49%,#19638a 95%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  #52b5f0 5%,#3699d0 49%,#19638a 95%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  #52b5f0 5%,#3699d0 49%,#19638a 95%); /* IE10+ */
    background: linear-gradient(to bottom,  #52b5f0 5%,#3699d0 49%,#19638a 95%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#52b5f0', endColorstr='#19638a',GradientType=0 ); /* IE6-8 */
}

div.nav ul {
    list-style: none; /* this removes the list marker */
}

div.nav li {
    display: inline-block;
}

div.nav li.selectedPage {
    background: #41ff5f;
}

div.nav li.selectedPage a {
    color: #10653b;
}

div.nav a, div.nav a:visited {
    padding: 5px;
    display: block;
    width: 120px;
    text-decoration: none;
    color: #FFFFFF;
    font-weight: bold;
    text-align: center;
}

div.nav a:hover, div.nav a:active, div.nav a:focus {
    background: #41ff5f;
    color: #10653b;
}

EDIT:

I do have this in place earlier in the css:

ul, li {
    padding: 0;  
    margin: 0;
}

JsFiddle Link:

http://jsfiddle.net/Gbg7J/

like image 963
Craig van Tonder Avatar asked Feb 19 '13 13:02

Craig van Tonder


People also ask

How do I reduce horizontal space between list items in CSS?

You need to use display:block and float:left on the li s to remove the space. When they're inline the browser treats them as words, and so leaves space in between.

How do you remove the top gap in CSS?

Go to your main CSS stylesheet and look at the selector for "#wrapper". If it doesn't already contain "clear: both;" inside of it, add that property. That will clear the floats and should remedy that whitespace at the top of the screen.


1 Answers

The gap is caused by the tabs and line feeds separating your list items; inline block elements (or any element that participates within the inline formatting context) are sensitive to their structure in your HTML.

You can either remove the spaces completely:

 <ul>
    <li class="selectedPage"><a href="#">HOME</a></li><li><a href="#">PAGE1</a></li<li><a href="#">PAGE2</a></li>
</ul>

Use comments:

<div class="nav">
    <ul>
        <li class="selectedPage"><a href="#">HOME</a></li><!--
        --><li><a href="#">PAGE1</a></li><!--
        --><li><a href="#">PAGE2</a></li><!--
    --></ul>
<!-- end .nav --></div>

Leave the HTML alone and use float instead (and clear the container):

.nav ul li {
    float: left;
  /*display: inline-block;*/
}

.nav ul {
    overflow: hidden;
}

Or set font-size: 0; on the parent and then reset it on the li

.nav ul {
    font-size: 0;
}

.nav li {
   display: inline-block;
   font-size: 16px;
}

Also, take a look at both: How to remove the space between inline-block elements? & http://css-tricks.com/fighting-the-space-between-inline-block-elements/

like image 172
Adrift Avatar answered Sep 28 '22 03:09

Adrift