| Author |
'margin-top' Oddity
|
|
| Cool Guy 2005-10-09, 6:27 pm |
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test</title>
</head>
<body>
<div style="height: 100px; width: 100px; background-color: red;">
<div style="height: 50px;
margin-top: 50px;
background-color: blue">
</div>
</div>
</body>
</html>
Now, with this I'd expect to see 100 vertical pixels of red followed by 100
vertical pixels of blue - and this *is* what I see in IE. However in
Firefox I see 100 vertical pixels of white followed by 100 vertical pixels
of blue followed by 100 vertical pixels of red.
Any idea of what's up?
| |
|
| Cool Guy wrote:
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> "http://www.w3.org/TR/html4/strict.dtd">
> <html>
> <head>
> <title>test</title>
> </head>
> <body>
> <div style="height: 100px; width: 100px; background-color: red;">
> <div style="height: 50px;
> margin-top: 50px;
> background-color: blue">
> </div>
> </div>
> </body>
> </html>
>
> Now, with this I'd expect to see 100 vertical pixels of red followed by 100
> vertical pixels of blue - and this *is* what I see in IE. However in
> Firefox I see 100 vertical pixels of white followed by 100 vertical pixels
> of blue followed by 100 vertical pixels of red.
>
> Any idea of what's up?
Personally, I'd expect 50px of red, and 50px of blue.
Are you sure the above is an exact copy paste of your actual example?
--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
| |
| Cool Guy 2005-10-09, 6:27 pm |
| I wrote:
> Any idea of what's up?
Okay, I've played with this a little and found that giving the outer div
'float: left' fixes this in Firefox. Does anyone know why?
| |
|
| Cool Guy wrote:
> I wrote:
>
>
> Okay, I've played with this a little and found that giving the outer div
> 'float: left' fixes this in Firefox. Does anyone know why?
There's another way of fixing it: give the outer div a border.
It's the margin on the inner div, that's going outside the outer div,
unless it has a border. I guess floating has the same effect, as it
makes the div positioned. Not exactly sure how it works though.
--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
| |
| Cool Guy 2005-10-09, 6:27 pm |
| Els <els.aNOSPAM@tiscali.nl> wrote:
> Personally, I'd expect 50px of red, and 50px of blue.
You're right - I mixed it up. Sorry.
| |
| Cool Guy 2005-10-09, 6:27 pm |
| Els <els.aNOSPAM@tiscali.nl>:
>
> There's another way of fixing it: give the outer div a border.
> It's the margin on the inner div, that's going outside the outer div,
> unless it has a border. I guess floating has the same effect, as it
> makes the div positioned. Not exactly sure how it works though.
Just to add: Opera needs the floating or the border, too. Otherwise it
displays it like Firefox.
| |
| Gus Richter 2005-10-09, 6:27 pm |
| Cool Guy wrote:
> I wrote:
>
>
>
>
> Okay, I've played with this a little and found that giving the outer div
> 'float: left' fixes this in Firefox. Does anyone know why?
This is due to collapsing margins, which basically says that when two
vertical margins meet up (are adjacent), instead of adding together the
largest margin takes precedent and the other one collapses to nothing.
Note that margins are no longer adjacent if a border or padding is used,
if relative or absolute positioned, if floated, or if another element is
between the two Margins.
--
Gus
| |
| Cool Guy 2005-10-09, 6:27 pm |
| Gus Richter <gusrichter@netscape.net> wrote:
Hi. I don't quite understand your reply...
> This is due to collapsing margins, which basically says that when two
> vertical margins meet up (are adjacent),
Which two margins are meeting up? Isn't there's only one margin here? In
any case setting 'margin: 0;' for 'body' has no effect.
> [...]
| |
|
| Cool Guy wrote:
> Gus Richter <gusrichter@netscape.net> wrote:
>
> Hi. I don't quite understand your reply...
>
>
> Which two margins are meeting up? Isn't there's only one margin here? In
> any case setting 'margin: 0;' for 'body' has no effect.
The margins for the outer div and the inner div.
(both top margins in this case)
--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
| |
| Cool Guy 2005-10-10, 3:20 am |
| Els <els.aNOSPAM@tiscali.nl>:
>
> The margins for the outer div and the inner div.
> (both top margins in this case)
But the outer div doesn't have a margin, does it? And anyway if I give it a
margin of 0 nothing changes. I'm confuzzled. :(
| |
|
| Cool Guy wrote:
> Els <els.aNOSPAM@tiscali.nl>:
>
> But the outer div doesn't have a margin, does it? And anyway if I give it a
> margin of 0 nothing changes. I'm confuzzled. :(
A margin of 0 on the outer div, and a margin of 50px on the inner div.
They collapse into one margin of 50px. Unless you add a border or
padding or float or position the outer div. Then the two margins
aren't adjacent anymore, and won't collapse anymore.
I agree it does take a slight mind bend to understand it though ;-)
--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
| |
| Cool Guy 2005-10-10, 3:20 am |
| Els <els.aNOSPAM@tiscali.nl> wrote:
>
> A margin of 0 on the outer div, and a margin of 50px on the inner div.
> They collapse into one margin of 50px. Unless you add a border or
> padding or float or position the outer div. Then the two margins
> aren't adjacent anymore, and won't collapse anymore.
Hmm. What's the rationale of this?
| |
|
| Cool Guy wrote:
> Els <els.aNOSPAM@tiscali.nl> wrote:
>
>
> Hmm. What's the rationale of this?
You'd have to ask the people over at W3c I think.
I don't find it too weird though - if the two elements would not be
nested, I think you would see how collapsing margins are logical. I
see it like two people who keep each other at arm's length.
If one's arm is 75cm long, and the other person's arm's 60cm long, the
distance between the two would be 75cm, not 135cm.
The weird part (imo) only happens when the two elements are nested.
The rules remain the same, it only /appears/ less logical.
--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
|
|
|
|
| Copyright 2003 - 2008 forum4designers.com Software forum Computer Hardware reviews |