CSS:BoxModel

From mi-linux
Jump to navigationJump to search

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:

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;

}