There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Solved: Question about CSS margins


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,515 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
08-Apr-2008, 02:23 PM #1
Solved: Question about CSS margins
So, I've been playing with some nested DIVs and noticed something strange.

Here is the HTML in question:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
  <title>DIV top margin</title>

  <style type="text/css">
.inner {
  border: 1px solid black;
  display: block;
  margin-right: auto;
  margin-left: auto;
  width: 220px;
  height: 280px;
  margin-top: 10%;
}
.outer {
  display: block;
  width: 800px;
  height: 600px;
  margin-right: auto;
  margin-left: auto;
  background-color: #99ffff;
}

  </style>
</head>
<body>

<div class="outer">
<div class="inner">inner DIV</div>

</div>

</body>
</html>
This produces a DIV that is 800px X 600px with a colored background. Inside that DIV, there is another DIV with the text "inner DIV" and a 1px border. Now, the inner DIV has a top margin of 10%.

What's strange is the parent DIV has this 10% margin applied to it!!! If I add a border to the outer DIV OR if I add some text to the outer DIV, the 10% margin of the inner DIV is applied to inner DIV and not the outer DIV.

Why is this?

Peace...
Attached Thumbnails
solved-question-about-css-margins-before.jpg  solved-question-about-css-margins-after.jpg  

Last edited by tomdkat : 08-Apr-2008 08:06 PM.
amanxman's Avatar
Senior Member with 129 posts.
 
Join Date: Mar 2006
Location: New Zealand
Experience: Advanced Web Developer
08-Apr-2008, 05:01 PM #2
Hey,

What is the end result you actually want? Which one, and what margins with what borders?

Let us know and i'll write fixed css for u

Ta
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,515 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
08-Apr-2008, 08:05 PM #3
I've got the CSS worked out such that the 10% margin applies only to the inner DIV. The second attachment shows what I'm after. I'm asking why the 10% margin applies to the outer DIV when it's specified on the inner DIV unless the outer DIV has a border or text of its own.

Peace...
amanxman's Avatar
Senior Member with 129 posts.
 
Join Date: Mar 2006
Location: New Zealand
Experience: Advanced Web Developer
09-Apr-2008, 05:43 AM #4
It's a padding bug - wack a touch of padding to the top of the outer div and you'll be sweet... for example:

.outer {
display: block;
width: 800px;
height: 600px;
margin-right: auto;
margin-left: auto;
background-color: #99ffff;
padding: 1px 0 0 0;
}

that extra padding on the top (be it 1px or 10px or whatever stops the 10% margin on .inner affecting .outer.)
Hope that helps
amx
__________________
It deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht frist and lsat ltteer is at the rghit pclae. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe.
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,515 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
09-Apr-2008, 10:45 AM #5
I guess adding the padding is the moral equivalent of adding a border or text to the outer DIV. Is this really a bug? I saw the behavior I described above in Firefox, Opera, Safari, and Maxthon 2.0.9 (IE6 engine). Or is the "bug" in the CSS rules governing margins?

Thanks!

Peace...
amanxman's Avatar
Senior Member with 129 posts.
 
Join Date: Mar 2006
Location: New Zealand
Experience: Advanced Web Developer
09-Apr-2008, 03:48 PM #6
i've never come across it before, it's quite strange so i would say bug, unless im missing something obvious!!
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,515 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
20-Jun-2008, 01:14 PM #7
Well, I found the answer to this question on another forum. The behavior I experienced is correct, per CSS 2.1 specs. It's called margin collapsing. There is an easier to digest description here. The second link was easier for me to digest, at least.

Peace...
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:58 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.