views:

47

answers:

4

Here is the code i'm working on it's not a page it's a treasure hunt for what might be re-used in a page, I ran into this problem last week or so and I'm asking myself what the heck is this margin for a while here

Let's picture that I can't use the float:left; tag for a reason, when I use display:inline-block; a weird margin apears on the elements (say divs) how do I fix it?

Problem is on FF3, and IE8 never tested in other browsers but if it dos not work on these what will it work on?

Original Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Why does that happen</title>
<style type="text/css">

* {padding:0px; margin:0 auto;
vertical-class:baseline;
text-class:left;}

body {padding:0px; margin:0 auto;}

div{margin: 0 auto;}

div div{margin: 0;}

.body {background:#CCC; width:900px; }

.red {background:#F00;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

.blue {background:#03F;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

.green {background:#090;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

.light-blue {background:#39F;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

.light-green {background:#9FC;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

.heavy-red {background:#C00;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

</style>
</head>
<body>

<div class="body">


    <div class="red"></div>
    <div class="blue"></div>
    <div class="green"></div>
    <div class="light-blue"></div>
    <div class="light-green"></div>
    <div class="heavy-red"></div>





</div>


</body>
</html>

Solved: solution is eliminate the spacing in the markup:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Why does that happen</title>
<style type="text/css">

* {padding:0px; margin:0 auto;
vertical-class:baseline;
text-class:left;}

body {padding:0px; margin:0 auto;}

div{margin: 0 auto;}

div div{margin: 0;}

.body {background:#CCC; width:900px; }

.red {background:#F00;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

.blue {background:#03F;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

.green {background:#090;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

.light-blue {background:#39F;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

.light-green {background:#9FC;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

.heavy-red {background:#C00;
height:300px; width:300px;
display:inline-block;
margin-left:0px;}

</style>
</head>
<body>

<div class="body">


    <div class="red"></div><div class="blue"></div><div class="green"></div><div class="light-blue"></div><div class="light-green"></div><div class="heavy-red"></div>

</div>


</body>
</html>
A: 

This is causing a margin on the left and right of every element (that is not display:inline):

* { margin:0 auto; }

You probably want this as your "reset" rule.

* { margin:0; }

You can then apply margin:0 auto to things that you want centered.

BudgieInWA
Use a css reset instead of resetting padding and margins with *
Badr Hari
just a comment. When you use margin 0 you do not need to add the px notation, just 0 as margin:0 auto;
Nervo Verdezoto
That just will take the content from the center and sit it on the left side of the browser
Evilalan
what do you mean with css reset, is it not a css reset already?
Evilalan
If you want the content centred but to remove the padding as budgie suggests, then try div{margin: 0 auto;} div div{margin: 0;}
Mark Chorley
@Mark Chorley Does not work, what am I doing wrong?
Evilalan
A: 

in all your inner div classes use margin-left: 0px

.red {background:#F00;
height:300px; width:300px; margin-left: 0px; display: inline-block;
}

.blue {background:#03F;
height:300px; width:300px;margin-left: 0px; display: inline-block;
}

.green {background:#090;
height:300px; width:300px;margin-left: 0px; display: inline-block;
}

.light-blue {background:#39F;
height:300px; width:300px;margin-left: 0px; display: inline-block;
}

.light-green {background:#9FC;
height:300px; width:300px;margin-left: 0px; display: inline-block;
}

.heavy-red {background:#C00;
height:300px; width:300px;margin-left: 0px; display: inline-block;
}

I have posted the final html at http://jsbin.com/uwavi3

I had to remove display: inline-block to get it to work on jsbin while on running it locally from a file I didn't have to. I think jsbin uses some sort of reset css.

Edit2 for float: left kinda layout

Sorry for misunderstanding the question. Increasing the width of the parent div and using spans instead of div(for colored blocks like red, blue etc.) Works for IE8 and FF3.6. Check here http://jsbin.com/uwavi3/3

Gaurav Saxena
It does not work in fact it doesn't change the end result a bit
Evilalan
Or indeed the slightly shorter div div{margin-left:0;}
Mark Chorley
@Evilalan: It doesn't work for you? It does for me on IE8 and FF3.6. Check the link in edit
Gaurav Saxena
nop, even tested div div {margin:0;} as well, and... The same thing at least of Firefox, how in god's name do this happen?
Evilalan
@Gaurav Saxena not it just show the same thing, testing on FF also
Evilalan
@Evilalan: May be its the version. Can you check on IE and Chrome and also can you confirm the version.
Gaurav Saxena
In regards to the Edit: The whole point is to have them all in the same "row" and without margin like they were floatng left
Evilalan
I'm using IE8 on Windows 7, Firefox 3.6.11 and for webkit I'm using Safari 5.0.1 (7355.17.3)
Evilalan
@Evilalan: I think I might have got it right now... They are arranged all in a row
Gaurav Saxena
Gaurav Saxena Reposted the code with the answer, and it's unbelievable, it's about spacing the markup itself, just odd as hell check out the new version on the question and if you'd like test yourself, it's the same code without spacing on the divs
Evilalan
A: 

I common practice in order your sites to be more cross browser compatible is to use a css reset file. Check this question http://stackoverflow.com/questions/116754/best-css-reset

chchrist
Tested with Eric's reset and it's still showing margin where it should not, I'm really curious now
Evilalan
Do you have a working url?
chchrist
@chchrist Local files only, if you know a place where I can upload it right the way that would be nice... Can't use my server tho
Evilalan
+2  A: 

The problem is caused by spaces in the markup between the divs. If you don't want to float them (why not I wonder), you have a couple of options.

  1. Remove the spaces so that the markup is all on the same line.
  2. If you don't ever want any text to appear in these divs then you can do this

    div{font-size:0;}

Mark Chorley
How in god's name did you find that out? just took away the spacing in the tesxt and just like magic it works, on every browser, I really can seem to make any sense of it because I think space in the markup should not change the output but as it turns out it does!
Evilalan
Bitter experience. Pressing ctrl + A (select all) can show up problems like this.
Mark Chorley
well all I can say it's thanks man, that will help alot my project :D
Evilalan
I'll ctrl+A every thing after that one! :D
Evilalan
You can also remove these errant text nodes with javascript.
Justin