Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-08-2007, 09:54 PM   PM User | #1
amcalab
New Coder

 
Join Date: Aug 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
amcalab is an unknown quantity at this point
Why is 100% more than 100%?

I'm trying to do something very simple...A fluid white box on top of a blue background. But, when I implement the solution below, the content slightly exceeds the width and height of the Mozilla window. This gives an uneven borders and scroll bars. Even setting widths and heights to lower percentages (to account from some unknown padding or margins) doesn't seem to eliminate the problem.

Also noted...if I remove the body's width specification, the side margins of the box work perfectly. But, if I remove the body's height specification, Mozilla does not recognize the box's min-height specification. Mozilla will, however, recognize a height=100% specification and make the box as big as the viewport. Why would it recognize height but not min-height?

CSS...
body {
width:100%;
height:100%;
background: #004A80; /* dark cyan blue */
}

#box {
width: 100%;
min-height:100%;
background: #FFFFFF; /* white */
}

HTML...
<body>
<div id="box"></div>
</body>
</html>
amcalab is offline   Reply With Quote
Old 08-08-2007, 10:01 PM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,590
Thanks: 5
Thanked 864 Times in 841 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
The body has a default margin/padding and those add to the overall width of an element. My first action is always:
Code:
body {
  margin: 0;
  padding: 0;
  background: [color];
  color: [color];
}
Be aware that not all users might have the default white background and black text, one can change that in the browser settings, and setting those in the stylesheet will prevent your design from being screwed up.

And by the way: to get the container div 100% high you need to set the height of both, the html and body elements to 100%.
__________________
Don’t click this link!

Last edited by VIPStephan; 08-08-2007 at 10:03 PM..
VIPStephan is online now   Reply With Quote
Old 08-08-2007, 10:28 PM   PM User | #3
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,887
Thanks: 5
Thanked 186 Times in 183 Posts
Arbitrator is on a distinguished road
Quote:
Originally Posted by amcalab View Post
Also noted...if I remove the body's width specification, the side margins of the box work perfectly.
The width declarations are almost certainly not necessary since normal block‐level elements, such as body and div, expand to fill all available horizontal area automatically.

Quote:
Originally Posted by amcalab View Post
But, if I remove the body's height specification, Mozilla does not recognize the box's min-height specification. […] Why would it recognize height but not min-height?
The nearest ancestor element needs to have an explicit height. If the height of the nearest ancestor element is fit‐to‐content or is defined by min-height or max-height, then the height, min-height, or max-height properties with a percentage value on the current element will fail. The exception is the html element’s ancestor, which is the viewport or area through which you see a Web page; the browser viewport’s height acts as an explicit height.

Code:
* { margin: 0; padding: 0; } /* accounts for VIPStephan’s advice */
html, body, * html #box { height: 100%; }
#box { min-height: 100%; }
* html is an Internet Explorer 6 exploit.

Quote:
Originally Posted by amcalab View Post
Mozilla will, however, recognize a height=100% specification and make the box as big as the viewport.
The body and div elements do not have a height attribute. This must be a proprietary feature.
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Old 08-08-2007, 11:43 PM   PM User | #4
amcalab
New Coder

 
Join Date: Aug 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
amcalab is an unknown quantity at this point
OK. Things work great UNTIL I add a margin to the box - to get that blue border. Then the side margins are OK; but, my bottom margin extends past the viewport.

Updated CSS:

body,html {
margin:0;
border:0;
padding:0;
width:100%;
height:100%;
background:#004A80;
}


#box {
margin:2%;
border:0;
padding:0;
width: 96%;
min-height:96%;
background:#FFFFFF;


}
amcalab is offline   Reply With Quote
Old 08-09-2007, 12:32 AM   PM User | #5
amcalab
New Coder

 
Join Date: Aug 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
amcalab is an unknown quantity at this point
OK. The following CSS code fixes the problem. I now get a white box centered perfectly on top of a blue background - no overflow. But, I'm not sure why for width I subtract 1xpadding but for height, I subtract 2xpadding.



body {
margin:0;
border:0;
padding:2%;
width:98%;
height:96%;
background:blue;
}

html{
width:98%;
height:96%;
}

* html #box { height: 100%; }


#box {
width: 100%;
min-height:100%;
background:#FFFFFF;
}
amcalab is offline   Reply With Quote
Old 08-09-2007, 12:40 AM   PM User | #6
amcalab
New Coder

 
Join Date: Aug 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
amcalab is an unknown quantity at this point
And, per arbitrator's advice, I removed all references to width. I was unaware that width specification was not necessary. And, based on previous results, behaved unexpectedly. Although I'm still not clear why width and height behave differently when you do specify them.

Thanks again VIPStephan and Arbitrator.

body {
margin:0;
border:0;
padding:2%;
height:96%;
background:blue;
}

html{
height:96%;
}

* html #box { height: 100%; }


#box {
min-height:100%;
background:#FFFFFF;
}
amcalab is offline   Reply With Quote
Old 08-09-2007, 02:56 PM   PM User | #7
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,887
Thanks: 5
Thanked 186 Times in 183 Posts
Arbitrator is on a distinguished road
Quote:
Originally Posted by amcalab View Post
Although I'm still not clear why width and height behave differently when you do specify them.
I would expect them to behave largely the same. You might be experiencing odd behavior if your document is in quirks mode [1]. I would need to see the document to tell what mode it’s in though.
  1. http://www.quirksmode.org/css/quirksmode.html
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 09:28 PM.


Advertisement
Log in to turn off these ads.