Reply
Why would removing a border shift a <div> down?
Old 03-02-2008, 07:20 PM Why would removing a border shift a <div> down?
Junior Talker

Posts: 3
Hello,

I'm kinda new to CSS. I've been working on centering my web site per the links provided in the stickies in this sub-forum. So I created a <body> and center aligned it, with a <div> container created inside the <body> to reinstate the left align parameter. Then I applied a border to the <div> tag so I could see its dimensions displayed on my screen. The whole technique worked beautifully. Centering the website this way solved a lot of problems I was having previously.

But here's the problem: The <div>container had a background image about 650px by 700px. It was in perfect position with the border around it. But the border was temporary- just a visual guide for me. When I removed it, the background image dropped about 10px to 20px. Any ideas on why this happened?

By the way, it happened in Firefox but not IE.

Thanks!
SDSU is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 03-02-2008, 07:50 PM Re: Why would removing a border shift a <div> down?
LadynRed's Avatar
Super Moderator

Posts: 6,557
Location: Tennessee
Lets see the code or a url please
__________________
Web Goddess & Web Standards Evangelist :) - Tables Be Gone !!
"Using or working with IE is like having to wear a 1970's polyester suit with pantyhose and a girdle, to work everyday"
Carolina Corvette Club
LadynRed is offline
Reply With Quote
View Public Profile
 
Old 03-02-2008, 08:36 PM Re: Why would removing a border shift a <div> down?
Junior Talker

Posts: 3
Quote:
Originally Posted by LadynRed View Post
Lets see the code or a url please
Drat. I was hoping you wouldn't ask for that.

Here's a sample with the border

Here's a sample without the border

Must be viewed w. Firefox to trigger error.

Last edited by SDSU : 03-02-2008 at 08:38 PM.
SDSU is offline
Reply With Quote
View Public Profile
 
Old 03-03-2008, 11:51 AM Re: Why would removing a border shift a <div> down?
Gilligan's Avatar
Dead Like Me

Posts: 1,608
Name: Stefan
Location: London, UK
try adding padding:15px; to div#container
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 03-03-2008, 02:01 PM Re: Why would removing a border shift a <div> down?
angele803's Avatar
Perfectly Imperfect

Posts: 1,588
Name: Stephanie
Location: Oklahoma
You can even just add one pixel of padding to the top of the container div.

This is an effect of "margin collapse". Here is an article about it: http://www.andybudd.com/archives/200...gin_for_error/

In your case, the container div without any padding or border allows the margin of the body and the margin of the H1 to touch. The browser is collapsing these margins into the largest, which is the margin of the H1. So you get a 20px space at the top of the page, and no space between the top of your container and the H1. Putting a border or padding on the container puts something in between the margins of your H1 and your body, which fixes the collapse. I know that is massively confusing, but it is really hard to explain!

Last edited by angele803 : 03-03-2008 at 02:28 PM.
angele803 is offline
Reply With Quote
View Public Profile
 
Old 03-05-2008, 01:22 AM Re: Why would removing a border shift a <div> down?
Junior Talker

Posts: 3
Quote:
Originally Posted by Gilligan View Post
try adding padding:15px; to div#container
Quote:
Originally Posted by angele803 View Post
You can even just add one pixel of padding to the top of the container div.

This is an effect of "margin collapse". Here is an article about it: http://www.andybudd.com/archives/200...gin_for_error/

In your case, the container div without any padding or border allows the margin of the body and the margin of the H1 to touch. The browser is collapsing these margins into the largest, which is the margin of the H1. So you get a 20px space at the top of the page, and no space between the top of your container and the H1. Putting a border or padding on the container puts something in between the margins of your H1 and your body, which fixes the collapse. I know that is massively confusing, but it is really hard to explain!
Thanks for both of your comments. Adding the padding sure did the trick.

Angele803, thanks for elaborating with your comments and the link. I did read the link. You're right, it's a bit difficult to wrap your head around the first time but I will definitely now be aware of collapsing margins. I'll have to experiment a little to get a better understanding, but these are the types of things I want to learn.
SDSU is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Why would removing a border shift a <div> down?
 

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15047 seconds with 12 queries