473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

'margin-top' Oddity

<!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?
Oct 9 '05 #1
13 1971
Els
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 -
Oct 9 '05 #2
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?
Oct 9 '05 #3
Els
Cool Guy wrote:
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?


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 -
Oct 9 '05 #4
Els <el*********@tiscali.nl> wrote:
Personally, I'd expect 50px of red, and 50px of blue.


You're right - I mixed it up. Sorry.
Oct 9 '05 #5
Els <el*********@tiscali.nl>:
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.


Just to add: Opera needs the floating or the border, too. Otherwise it
displays it like Firefox.
Oct 9 '05 #6
Els
Cool Guy wrote:
Els <el*********@tiscali.nl>:
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.


Just to add: Opera needs the floating or the border, too. Otherwise it
displays it like Firefox.


Yup, knew that :-)
When Opera and Firefox do the same thing while IE is not, usually it's
safe to say IE is wrong. Haven't checked the specs on this though.

--
Els http://locusmeus.com/
Sonhos vem. Sonhos vão. O resto é imperfeito.
- Renato Russo -
Now playing: Jon Bon Jovi - Knockin On Heavens Door (acoustic)
Oct 9 '05 #7
Cool Guy wrote:
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?


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
Oct 9 '05 #8
Gus Richter <gu********@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.
[...]

Oct 9 '05 #9
Els
Cool Guy wrote:
Gus Richter <gu********@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.


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 -
Oct 9 '05 #10
Els <el*********@tiscali.nl>:
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.


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. :(
Oct 10 '05 #11
Els
Cool Guy wrote:
Els <el*********@tiscali.nl>:
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.


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. :(


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 -
Oct 10 '05 #12
Els <el*********@tiscali.nl> wrote:
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.


Hmm. What's the rationale of this?
Oct 10 '05 #13
Els
Cool Guy wrote:
Els <el*********@tiscali.nl> wrote:
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.


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 -
Oct 10 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tony Benham | last post by:
I'm having problems with a css layout where a margin is being inserted with the wrong width. I believe it's my css that's at fault since it is wrong in netscape 7.1, opera,7.11 and IE6 but I cannot...
4
by: Harlan Messinger | last post by:
What is *supposed* to be the way to specify the horizontal offset of (a) the list item's marker and (b) the list item's content? In particular, see ...
3
by: Brian | last post by:
I have a page with content, navigation, and footer divs, in that order. The nav div has position: absolute, width 8em, on green background. The other divs have an 8em green left border, such that...
26
by: meltedown | last post by:
I have 2 left floating divs on a page. Sometime the left side is larger, sometimes the right side is larger. I want the page to have a margin at the bottom of whichever div is the largest. If I...
3
by: Luciano A. Ferrer | last post by:
this is really strange... I cant find what is wrong... so, #botonera have a left margin of 18px, but IE make it 36px! If I put a margin of 30px, IE make it 60px! I am crazy? <!DOCTYPE html...
1
by: Jeremy | last post by:
Let's say I have some block elements that self-organize by floating to the left, so if no more blocks can fit in a row they begin on the next row (see crummy ASCII art at end of message). I...
11
by: Pablito | last post by:
Hi at all into a page I wrote <style> body {margin-left:10%;margin-right:10%;text-align:justify;} </style> reading the page with FireFox the margin left is 10% but the right is 0% Why? How can...
2
by: Steve Richter | last post by:
I want my panel to have an inner margin. Problem is label text does not appear to recognize the margin. In this example, the TextBox aligns on the margin, the label does not: <asp:Panel...
5
by: Neviton | last post by:
Hi this is the html to generate the behavior: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html...
7
by: GTalbot | last post by:
Hello fellow authoring.stylesheets colleagues, Can someone please explain why the bottom margin of the last inflow block-level child in an overflowed parent should not have its margin reachable....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.