473,394 Members | 1,870 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,394 software developers and data experts.

[css] margin-top on body tag

mickey0
142 100+
Hi,
I can't understand how manage the body tag. I wrote this below and to avoid a "white" space between the body tag and the top edge, I had to use margin:-top: -10px; Is it normal?
Moreover, it seems that changing the margin-top of block1, it moves down the entire 'body'. So why can't set e.g. to '10' the margin of 'block1' ?

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.     <head>
  4.       <title>1 - positioning</title>
  5.       <style type="text/css">
  6.     html {background: white; }
  7.     body {  
  8.         background: gray; width: 400px; 
  9.         margin: 0px; padding: 0px;  margin-top: -10px;
  10.  
  11.         height: 400px
  12.     }
  13.  
  14.     #block1 { 
  15.         background: pink; display: block; border: Dashed 2px; 
  16.         width: 80px; height: 80px; margin: 20px;
  17.     }
  18.  
  19.     #block2 { 
  20.         background: yellow; display: block; border: Dashed 1px; 
  21.         width: 80px; height: 80px; 
  22.     }
  23.  
  24.     #block3 { background: green;  display: block; border: Dashed 1px; 
  25.         width: 80px; height: 80px; 
  26.     }
  27.  
  28.     #wrapper { 
  29.         background: red;  position: relative; top: 50px; left: 50px;
  30.         width: 300px;
  31.         height: 300px;
  32.     }
  33.  
  34.  
  35.       </style>    
  36.     </head>
  37.  
  38. <body> 
  39.  
  40.     <div id="wrapper">
  41.         <div id="block1">
  42.             Block1
  43.         </div>
  44.  
  45.         <div id="block2">
  46.             Block2
  47.         </div>
  48.  
  49.         <div id="block3">
  50.             Block3
  51.         </div>
  52.     </div>
  53.  
  54.  
  55. </body>
  56. </html>
  57.  
http://imageshack.us/photo/my-images/210/cssr.jpg/
Dec 1 '11 #1
7 4295
drhowarddrfine
7,435 Expert 4TB
This has to do with margin collapse between block level elements when they are stacked on top of each other. It prevents two elements, such as paragraphs, from getting double the margin when they are placed next to each other. If you add a border to the wrapper div, it unlinks the connection and the problem goes away.

I profess to have never seen the issue between html and body before and can't think of the reasoning behind it right now.
Dec 1 '11 #2
mickey0
142 100+
Hello,
if I set the border of the body, it works. Is it a good thing to set the border? I actually don't need it...however, I had few go and this seems working:
Expand|Select|Wrap|Line Numbers
  1.         html { background: yellow;  }
  2.         body { 
  3.              background: gray;                         
  4.             margin:0;                                     
  5.             height: 100%; 
  6.             position: absolute; top:0; left:0;
  7.             width: 100%;
  8.         }        
  9.  
  10.  
  11.         #mainblock { background: purple; width: 200px; height: 200px; margin: 20px; }
  12.  
  13.  
It seems that " position: absolute; top:0; left:0;" within 'body' is really needed....any opinion?
Dec 1 '11 #3
drhowarddrfine
7,435 Expert 4TB
Haven't had a chance to think about this. I should know this but can't get my head wrapped around it. Maybe later.
Dec 2 '11 #4
AutumnsDecay
170 100+
Giving the <html> tag any properties makes it act as an element, and elements have width / height properties.

What I don't understand is this: Why set the background color for your HTML tag, when your body tag is 100% width/height and a different color?

Delete " html { background: yellow; } " from your CSS and the space between your html and body on the page will disappear.
Dec 6 '11 #5
Exequiel
288 256MB
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.     <head>
  4.       <title>1 - positioning</title>
  5.       <style type="text/css">
  6.   body{ background:#FFF;}
  7.     #container {  
  8.         background: gray; width: 400px;  margin-top: -20px; margin-left:-20px;
  9.         height: 400px
  10.     }
  11.  
  12.     #block1 { 
  13.         background: pink; display: block; border: Dashed 2px; 
  14.         width: 80px; height: 80px; margin: 20px;
  15.     }
  16.  
  17.     #block2 { 
  18.         background: yellow; display: block; border: Dashed 1px; 
  19.         width: 80px; height: 80px; 
  20.     }
  21.  
  22.     #block3 { background: green;  display: block; border: Dashed 1px; 
  23.         width: 80px; height: 80px; 
  24.     }
  25.  
  26.     #wrapper { 
  27.         background: red;  position: relative; top: 50px; left: 50px;
  28.         width: 300px;
  29.         height: 300px;
  30.     }
  31.  
  32.  
  33.       </style>    
  34.     </head>
  35.  
  36. <body> 
  37. <div id="container">
  38.     <div id="wrapper">
  39.         <div id="block1">
  40.             Block1
  41.         </div>
  42.  
  43.         <div id="block2">
  44.             Block2
  45.         </div>
  46.  
  47.         <div id="block3">
  48.             Block3
  49.         </div>
  50.     </div>
  51. </div><!--end of container--> 
  52.  
  53. </body>
  54. </html>
  55.  
You can try this one...
Jul 19 '12 #6
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.     <head>
  4.       <title>1 - positioning</title>
  5.       <style type="text/css">
  6.   body{ background:#FFF;}
  7.     #container {
  8.         background: gray; width: 400px;  margin-top: -60px;
  9.         height: 400px
  10.     }
  11.  
  12.     #block1 {
  13.         background: pink; display: block; border: Dashed 2px;
  14.         width: 80px; height: 80px; margin-left:-2px;
  15.     }
  16.  
  17.     #block2 {
  18.         background: yellow; display: block; border: Dashed 1px;
  19.         width: 80px; height: 80px;
  20.     }
  21.  
  22.     #block3 { background: green;  display: block; border: Dashed 1px;
  23.         width: 80px; height: 80px;
  24.     }
  25.  
  26.     #wrapper {
  27.         background: red;  position: relative; top: 50px; left: 50px;
  28.         width: 300px;
  29.         height: 300px;
  30.     }
  31.  
  32.  
  33.       </style>
  34.     </head>
  35.  
  36. <body>
  37. <div id="container">
  38.     <div id="wrapper">
  39.         <div id="block1">
  40.             Block1
  41.         </div>
  42.  
  43.         <div id="block2">
  44.             Block2
  45.         </div>
  46.  
  47.         <div id="block3">
  48.             Block3
  49.         </div>
  50.     </div>
  51. </div><!--end of container-->
  52.  
  53. </body>
  54. </html>
DO COPY AND PASTE, NEXT SAVE. RUN, PLEASE.
Jul 19 '12 #7
Exequiel
288 256MB
Gansehchandra. . . You just copy paste my response....
Jul 20 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: GDB | last post by:
How does one completely remove the space between inline elements with CSS? I've set margin-left, margin-right, and border-width to 0px, yet still there is a gap in all browsers I've tried (IE5-6,...
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: Alex Bell | last post by:
I am developing at http://www.members.iinet.net.au/~abell1/test/index.htm with city.css at http://www.members.iinet.net.au/~abell1/test/city.css. Both validate. In index.htm there is a block of...
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...
2
by: JWL | last post by:
Hi I need to create a bunch of sites with slightly dynamic CSS. Basically, all the image paths in the CSS need to be dynamic, depending on the values of certain ASP variables. I can think of...
3
by: xen | last post by:
Hey there, In my html file I use a <div class="quote">. In my css file I define .quote I need to specify the margins for <pinside the <div> Today I learned that I can do this using ..quote p...
10
by: Itaichuk | last post by:
Hi I read in several CSS tutorials that block-level elements, such as <div& <pare rendered with an implicit line break before and after. I set to test this out using the following HTML: I...
18
by: Nik Coughlin | last post by:
I am halfway through writing a tutorial on image slicing for fluid CSS layouts, I would love some feedback on what I've done up until this point: http://nrkn.com/index.html I am still writing...
8
by: Jim | last post by:
Hi, I am hoping that someone here can help me out on this. The following code works just fine and gives me a nice margin between my image to the left and the text that appears adjacent to it...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.