tags:

views:

200

answers:

6

Hi, I've noticed that some browsers have trouble with margins, especially when an element is floated. For example, this website I'm doing looks fine in Firefox, but IE7 screws up the margins completely it seems. I also testet it on several Linux browsers as well and some of the make similar mistakes.

The site is http://w3box.com/mat

This looks fine in FF3.0 as far as I can tell. Haven't seen it in FF2 yet, or IE6. Why does this happen? Is it because I've got floated DIVs with specified margins?

Are there some things I should avoid or should have done differently?

Edit: So it looks like my tags was the source of the screwup. I'd placed images in the that was not defined in the CSS and that had floats on them, combined with margins. These screwed up everything and I have to redo these.

Also, some stuff happened when I used XHTML Strict instead :) Thanx everyone! I'll try to fix this on my own :)

+2  A: 

I'd suggest using some form of CSS Framework (Blueprint CSS, 960 Grid, etc) as they have a number of margin, padding and other common HTML element resets. You should find cross browser development is easier using a framework.

Kane
+2  A: 

Different browsers have different ways to handle box model. Most of the time the sites which are displayed well in FF, Chrome or IE8 can have problems in IE6 and 7. To workaround this problem you can try to set all the default margin and padding to 0 (and adjust them as needed on specific elements):

*{ margin:0px; padding: 0px; } //Simplest rule...

To see more on CSS reset you can look at: http://meyerweb.com/eric/tools/css/reset/

And then apply different styles for IE7 and 6 with conditional comments.

mamoo
Just tried this and it didn't really do anything. I'd set this in the html and body in the CSS anyway. I put in the CSS reset code though :)
Kenny Bones
+3  A: 

Marging are not bad, but IE has some troubles with the margins of float elements. While there are many recipes for fixing, I believe that in your case you may use absolute positioning instead of float+margins (you don't really need "float" behavior when the image is wrapped by text)

Dmitry
This is IE7 and the error does not occur in IE8. Just tested it.I haven't tested it in IE6 yet because I cannot find a single terminal server where it is still installed.I'll try absolute positioning though. No need to clutter the code more than necessary! :)
Kenny Bones
See, the thing is. I don't really understand why this is even happening. I mean, the DIV #contentInner doesn't even have a float. And this it's margins are like doubled or something. Could it be the images I've placed there? They do not have their own classes, but I've specified CSS code in the <img> tag. This could be the explanation? Because I see the error in the header menu and the #contentInner DIV. And both of those have <img> tags before them..
Kenny Bones
It is the <img> tags that's the problem. IE seems to add some space after the images. Much like a line break or something. The problem is resolved if I remove the <img> tags. So I'll probably have to look into positioning them instead.
Kenny Bones
In IE6 it looks ugly (don't know if it looks the same as in IE7 but anyway). Try setting "position:relative" for content_header and "position: absolute; right:0; top:0" for the image. This should place the image into the top-left corner of content_header. After that play with top and right values.
Dmitry
+4  A: 

I disagree with using a library if you want to learn about CSS part of the curve unfortunately is learning about the ways different browsers react to CSS. I wouldn't even suggest using a reset stylesheet. If you are going to be doing this a lot learn how CSS works. If you use a library or a set stylesheet which you don't understand how will you fix it when it breaks.

matpol
+1  A: 

As mentioned on other answers it's to do with IE's interpretation of the box model.

Whenever anything is floated IE tends to double the margins specified. This can be fixed with an extra stylesheet for IE using conditional comments.

See also: http://www.positioniseverything.net/explorer/doubled-margin.html

Jonny Haynes
But wasn't this fixed with IE7? That's what I thought. But it doesn't seem like it anyway.
Kenny Bones
IE7 still is a bit quirky with the box model - they fixed the majority of the bugs but some still exist
Jonny Haynes
+3  A: 

There is nothing wrong with using margins.

Old versions of IE have one bug and that alone isn't nearly enough of a reason to avoid using one of the core layout features of CSS. Specifically, this bug occurs in IE when you float an object and give it a margin in the same direction, e.g.:

.whatever {
    float: right;
    margin-right: 5px;
}

You can deal with this a number of ways, depending on your layout. One way would be to add another div around your box and use padding on that to replicate the same space a margin would.

Philip Morton
Margins and paddings don't produce the same effect. That's the whole point of the box model. It would only product the same effect with no background and no border.
Jonny Haynes
Yes, you're right that they don't produce the same effect, but if you're fixing a bug you might be able to switch them around to provide the same space between objects. It definitely depends on the situation, but it can be one option.
Philip Morton
Agreed +1 reinstated - well if you edit your answer I can remove it. :-)
Jonny Haynes
You've got a deal! :P
Philip Morton