html - css margin and padding newbie question -
i'm struggling under behaviour of following html , css code. i'm aware of box layouts , margin collapsing doesn't seem explain following:
1) using code below, unmodified, logo shown 10px of white space between , top of page , 4 or 5 pixels below of color given #allcontent.background-color. interestingly if set font-size property within body 0px removes white space above logo , 4 or 5 pixels below it.
2) if padding value within #allcontent changed 0px 1px 10px of padding added above , below logo color specified #allcontent.background-color.
index.html file:
<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>home</title> <link rel="stylesheet" type="text/css" href="my.css" /> </head> <body> <div id="allcontent"> <div id="header"> <p> <img width="200" height="60" src="images/logo.gif" alt="logo" /> </p> </div> </div> </body> </html> my.css file:
body { margin: 0px; padding: 0px; } #allcontent{ background-color: #dddddd; padding: 0px; margin: 0px; } #header { padding: 0px; margin: 0px; }
this code: http://jsbin.com/eronaq
starting easier issue:
the logo shown [..] 4 or 5 pixels below
that gap space reserved descenders in text letters g , p.
you can fix in few ways. example, setting display: block or vertical-align top or bottom on img.
display: block: http://jsbin.com/eronaq/2
the logo shown 10px of white space between , top of page
see: http://reference.sitepoint.com/css/collapsingmargins
that space margin-top on p collapsing through way body, because there nothing stop doing defined rules of collapsing margins.
by “touch,” mean places @ no padding, borders, or content exist between adjacent margins.
Comments
Post a Comment