Difference between revisions of "CSS:BoxModel"

From mi-linux
Jump to navigationJump to search
Line 2: Line 2:
 
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:
 
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:
  
[[Image:boxmodel.gif|thumb|200|The box model - from http://www.w3.org/TR/REC-CSS2/box.html]]
+
[[Image:boxmodel.gif|thumb|300|The box model - from http://www.w3.org/TR/REC-CSS2/box.html]]
  
 
Let's experiment with some of these spacial concepts with simple paragraphs:
 
Let's experiment with some of these spacial concepts with simple paragraphs:

Revision as of 12:56, 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:

Error creating thumbnail: File missing

Let's experiment 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}
  • TIME TO PRACTICE
    • Practice changing some of the values of the pading, margins, borders, etc. to achieve some effects you might like to see in your website
    • For example, change the #first line in the CSS to:
<nowiki>#first

{

 background-color:#aaaaff;
 color: #0000ff;
 text-align: center;
 font-size: 150%;
 padding:10px;
 margin:20px 250px 75px 250px;
 border:4px solid #00bbff;

}