There are three key concepts in stylesheets that you must understand in order to use them most effectively.
The third key concept is The CSS Box Model, which specifies how, in visual browsers, elements in the document tree are displayed in rectangular boxes.
There is a very detailed, complete discussion of the Box Model in the official W3C documentation Visual Formatting Model.
In visual media, elements in the document tree are displayed in rectangular boxes. The diagram is adapted from the standards documentation:
![[missing image]](graphics/painting_with_frame.gif)
The box model is like framed artwork.
The picture (content) is the central part.
It is surrounded by a mat (padding)
and displayed within a frame (border).
It is hung (rendered)
with some blank wall space (margin) around it.
>Basic width and height designations apply to the content area.
Padding,border and margin are added to that.
In the following example, the content area is 200 pixels wide, the padding 25, the border 5, and the margin 10, so the width of the entire box should be 10 + 5 + 25 + 200 + 25 + 5 + 10 or 280 pixels.
#example1 {
width: 200px;
padding:25px;
margin:10px;
border-width: 5px;
border-style: solid;
border-color: blue;
background-color: silver;
text-align: justify;
}
Content surrounded by 25 pixels padding
Same except with zero padding
Notice that
Note: Some older browsers, such as IE 5, will incorrectly generate a box that is 200x100 pixels from border to border, subtracting the padding and border rather than adding, and leaving a content area only 146 pixels wide. IE 6 gets the box model right, but still misinterprets some other specifications. you should always test in multiple browsers.
Box properties and values can be expressed in a variety of ways. Cascading rules apply, so the latest value designated is used.
Default border style is none. You must specify a border style if you want a visible border.
#example2 {
margin-top: 10px;
margin-right: 0px;
margin-bottom: 25px;
margin-left: 100px;
padding: 25px 50px 10px 0px; /* top right bottom left */
border: dashed 2px #0f0; /* all sides */
border-right-color: #f00;
border-left-style: double;
border-left-width: 10px;
text-align: justify;
}
The properties and values can usually be expressed for each side or all in one, in a variety of ways. Most unspecified values are inherited from the enclosing block.
display propertyAll elements have a display property. Possible values include
| display value | description |
|---|---|
| block | element is displayed as a block, separate from the following block |
| inline | element is displayed in line (no line breaks) with following content |
| none | element is not displayed. Effectively, it is not there. |
display:blockmargin properties. display:inlinedisplay:noneThe blocks in early web pages were simply displayed in the order they occurred in the source code. Images were block elements separated from text. As Web use moved beyond the scientific research community, designers looked for way to reproduce the layout and appearance of printed documents. They used (misused) structural HTML elements such as tables and lists and tricks such as inivisible images (for spacing) to create layout effects. Unfortunately these techniques often make documents less accessible.
In CSS, positioning schemes provide the tools for controlling layout without destroying meaningful structure. A block may be laid out according to three positioning schemes:
Normal flow is the default layout that is used when nothing else is specified.
The float property allows blocks to be positioned to the side, with text flowing around them. It is used for images and to create complex layouts.
float property are left, right, and none.Floated image in a paragraph box
This is a bunch of text. This is some text. This is a another bit of text.
This is more text. This is text. This is text. This is text.
In the stylesheet:
#example3 {
border: solid thin #f00;
padding: 15px;
width: 200px;
float: right;
}
#example3_image {
display: block;
float: right;
width:50px; height:50px;
}
In the HTML:
<p id="example3">
<img id="example3_image"
src="graphics/yelblock.gif"
alt="[missing image]" />
This is a bunch of text. This is some text. This is a another bit of text.
This is more text. This is text. This is text. This is text.
</p>
The <center> tag is not now, and has never been, standard. HTML 3.2 included the align property, but it is , and has been for many years, deprecated. It is possible to center blocks using only style properties.
In addition to length and percentage values, a block's margin can have a value of 'auto'. In normal flow, if both the left and right margins are 'auto', their computed values will be equal; hence the block will be centered.
The CSS rule
#example4{
width: 50%;
border: outset 10px;
margin-left: auto;
margin-right:auto;
}
The HTML
<p id="example4"> </p>
An image is by default an inline element; in order to to center it you should also set its display value to block. If necessary, also unfloat it (style rune float:none) for normal flow.
The CSS rule
#example5{
display: block;
margin-left: auto;
margin-right:auto;
width: 50px; height: 50px;
}
The HTML
<img id="example5"
src="graphics/yelblock.gif"
alt="[missing image]" />
clear propertyThe clear property "indicates which sides of an element's box(es) may not be adjacent to an earlier floating box" (in the same block element).
Possible values for the clear property are left, right, both, and none.
The standard specifies exactly how this is to be implemented: if an element is declared clear on the right, its top border edge must be below any right-floating boxes above it.
Not just text, but images and other boxes flow around a floated box.
img.f1 { float: right;
border: solid thin red; }
This is some text with images stuck in the middle.
LThis is some text with images stuck in the middle. This is some text with images stuck in the middle.
This is some text with images stuck in the middle. This is some text with images stuck in the middle.
This is some text with images stuck in the middle.This is some text with images stuck in the middle.This is some text with images stuck in the middle.
This is some text with images stuck in the middle. This is some text with images stuck in the middle. This is some text with images stuck in the middle. This is some text with images stuck in the middle.
Without text separating them, the images float right up beside each other, as far to the right as possible



img.f2 { float: right;
border: solid thin red;
clear: right; }
This is some text with images stuck in the middle.
This is some text with images stuck in the middle. This is some text with images stuck in the middle.
This is some text with images stuck in the middle. This is some text with images stuck in the middle.
This is some text with images stuck in the middle.This is some text with images stuck in the middle.This is some text with images stuck in the middle.
This is some text with images stuck in the middle. This is some text with images stuck in the middle. This is some text with images stuck in the middle. This is some text with images stuck in the middle.
You can float an image or block to the right or left, with the content that follows it in the document wrapping around it. There is no simple way to float an image in the center with text flowing on both sides.
Floated divs provide a way to make columns in text without resorting to tables. The resulting layouts can be simple (two vertical columns) or more complex. Of course the more complex layouts are more challenging to create.
The strategy in this example is to give one div a specific width (in this case in pixels) and float it (in this case to the left). The other column simply flows up beside the first one.
The large image is floated to the right within in the wider column.
The underlying structure is easily seen in a stripped-down example. View the source code to see how it's done.
The illusion of a different background for the left column is created with a single background image. The background is fixed in position so it does not scroll.
To be continued...
Hood College Department of Computer Science: Course materials © 1997-2006 by Elizabeth Chang.