Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
May 25, 2012, 08:06:11 AM

Pages: [1]   Go Down
  Print  
Author Topic: Problem with DIV tags and margins  (Read 729 times)
tech4all
Spaceship Captain
*****
Offline Offline

Posts: 119


« on: October 04, 2011, 04:55:36 PM »

Ok I have two DIV tags, one nested in the other. When I give the outer DIV tag a border it's fine. But when I remove the border this is where the problem comes in. Lets say I have the outer DIV tag with a margin-top: 100px and the inner DIV tag with margin-top: 50px. That should have the outer DIV tag be 100px below it's parent element and the inner DIV tag 50px below the top of the outer DIV tag.

But the problem is that when the border is set to 0 for the outer DIV tag, both DIV tags "share" their margin-top CSS attribute. The highest number is applied to both DIV tags and the inner DIV tag "glued" to the top of the outer DIV tag. But when I give the outer DIV tag a border, it works fine. But I don't want a border.

Here's the code:

CSS:

Code:
div.outside {
margin-top: 70px;
height: 400px;
width: 500px;
margin-right: auto;
margin-left: auto;
background-color: red;
border: 0px solid red;
}

div.inside {
margin-top: 20px;
height: 300px;
width: 250px;
margin-right: auto;
margin-left: auto;
background-color: green;
border: 0px solid black;

HTML:

Code:

<div class="outside">
<div class="inside"></div>
</div>


So in this case with the code above both DIV tags are 70px from the top and are "glued" together at their top edges. How can I have the DIV tags "margin-top" behave indepently and have a 0px border?

Hope that makes sense.

 Confused
Logged
MrPhil
Senior Moderator
Berserker Poster
*****
Offline Offline

Posts: 5219



« Reply #1 on: October 04, 2011, 06:14:44 PM »

Perhaps not the most elegant solution, but try padding-top: 20px; on the outer div to force the inner one down. An alternative would be a top: 20px; on the inner div, but you would need position: relative; on at least the outer div. I'm not really sure why the inner div's margin-top isn't effective in pushing it away from the edge of the outer div, but there's a lot of black magic in the CSS box model.
Logged

Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-
scanman20
Senior Moderator
Über Jedi
*****
Offline Offline

Posts: 1524



WWW
« Reply #2 on: October 05, 2011, 06:49:37 AM »

Add overflow:auto to your outer div.
Logged

Even a broken clock is right twice a day.
NotOneBit.com
MCSE - MCSA - MCP
tech4all
Spaceship Captain
*****
Offline Offline

Posts: 119


« Reply #3 on: October 05, 2011, 10:48:17 PM »

Thank you both for your help.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: