Difference between revisions of "CSS:BoxModel"

From mi-linux
Jump to navigationJump to search
Line 7: Line 7:
  
 
''HTML document''
 
''HTML document''
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+
<nowiki><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 
<HTML>
 
<HTML>
 
   <HEAD>
 
   <HEAD>
Line 18: Line 18:
 
     <P id="third">contents of section 3</P>
 
     <P id="third">contents of section 3</P>
 
   </BODY>
 
   </BODY>
</HTML>
+
</HTML></nowiki>
  
 
''CSS document (save as style4.css)''
 
''CSS document (save as style4.css)''

Revision as of 12:48, 5 September 2007

The box model

When using CSS, every element can be thought of as an object with several different boundaries. The "content" is separated from the "border" by "padding", and the "border" is seperated from the next object by the "margin." Each of these boundaries are controllable through CSS. The picture below demonstrates the concept well:

The box model - from http://www.w3.org/TR/REC-CSS2/box.html

Let's experiement with some of these spacial concepts with simple paragraphs:

HTML document

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
  <HEAD>
     <TITLE>CSS 2</TITLE>
     <LINK rel="stylesheet" href="style4.css" type="text/css">
  </HEAD>
  <BODY>
     <P id="first">contents of section 1</P>
     <P id="second">contents of section 2</P>
     <P id="third">contents of section 3</P>
  </BODY>
</HTML>

CSS document (save as style4.css)

#first  {padding:10px; margin:10px; border:1px solid #00ff00}
#second {padding:20px; margin:20px; border:2px dashed #0000ff}
#third  {padding:30px; margin:30px; border:3px dotted #ff0000}