tags:

views:

878

answers:

6

I'm trying to create a horizontal menu with a thick border bar that shows over the hovered item. However, for some reason there's a small gap at the right end of the bar in Firefox and Chrome. Strangely, IE displays it without the gap. Firebug doesn't show any reason for this gap.

I tried using simple divs and still it appears. I've distilled it down to a single HTML sample, with divs only.

Can anyone explain this and tell me how to get rid of that gap?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Weird border spacing</title>
    <style type="text/css">
    div.outer
    {
        border-top: dotted 1px lime;
        margin: 10px;
    }

    div.outer div
    {
        display: inline;
        margin: 0;
        padding: 0 12px;
        border-left: solid 1px silver;
        border-top: solid 3px red;
    }        
    </style>
</head>
<body>
    <div class="outer">
        <div>First</div>
        <div>Second</div>
        <div>Third</div>
    </div>
</body>
</html>
+1  A: 

display:inline rarely works well. Consider using float:left instead.

Oli
Thanks, that fixed it!
netgirlk
A: 

Try to add:

body {
    margin: 0;
    padding: 0;
}

Browsers have different default values for these CSS properties.

Nadia Alramli
I find myself applying `* { margin: 0px; padding: 0px }` in all my stylesheets. Let the browser not make decisions.
Alan Haggai Alavi
@alan thats never a good idea. Its simply a beginners fix and will only cause trouble later on. You will want to consider changing that.
corymathews
A: 

display:inline-block may be what you want.

SpliFF
Again, this has fairly unpredictable implementations. I don't think any two major versions of IE render it the same. Floating is a lot more standardised.
Oli
inline-block is not supported by many browsers.
corymathews
A: 

the gap is caused by the breaking space/new line between the <div>. To fix this put all the <div> on the same line with no space between them. However I would recommend re-thinking the css, possibly using list <ul>

Andrew
I did use an unordered list but it had the same gaps and I was trying to eliminate any issues with lists by making a sample with divs. Is there a way to get rid of the space without putting all the ul items together on a single line?
netgirlk
+2  A: 

I floated the inner div left which fixed the weird spacing and the outer div left which forced the inner div to be inside of it. You can adjust the styles more to fit your needs.

div.outer{border-top: dotted 1px lime;margin: 10px;float:left;}
div.outer div
{
    float:left;
    margin: 0;
    padding: 0 12px;
    border-left: solid 1px silver;
    border-top: solid 3px red;
}

You could also remove the new lines between the divs to fix just the spaces.

mattphp
Oli gave the same suggestion earlier so I marked his as the correct answer. Thanks, though. I'll up vote it once I get 15+ rep ;-)
netgirlk
A: 

The reason you're getting the space is due to inline elements interpreting spaces as something to be rendered. This lets you do:

Hello World

and still have 2 separate words. To remove the space you have to...well, remove the space. When you're trying to put elements flush together for layout, as already mentioned, using float: left is the preferred option.

jvenema