When browsers render CSS, they use something called the box model. The box model is broken up into three parts. From the outside in those parts are:
I'll start with the border first.
All of the above groups of properties include declarations that affect only the top, right side, bottom, or left side, and a property that allows all sides to be expressed with a declaration that affects all sides at once.
Those that affect a single side have -top
, -right
, -bottom
, or -left
somewhere in the property name.
If the side is not specified, then the declaration affects all sides. You may use anywhere from one to four values, which apply like so:
Number of Values | Which Values | Which Side |
---|---|---|
One | First | Top |
Right | ||
Bottom | ||
Left | ||
Two | First | Top |
Bottom | ||
Second | Right | |
Left | ||
Three | First | Top |
Second | Right | |
Left | ||
Third | Bottom | |
Four | First | Top |
Second | Right | |
Third | Bottom | |
Fourth | Left |
The border is a line around the elements' content. When a border is not specified, it has a width of 0, and is thus invisible.
There are several properties dealing with borders. You can set each aspect of the border on each side individually using the following properties:
There are some simple shorthand properties as well, which I'll explain shortly.
Properties: |
|
---|---|
Default value: | medium |
Inherited: | No |
This can be set to a specific length or to any of the following:
Properties: |
|
---|---|
Default value: | The value of the color property. |
Inherited: | No |
A border can be set to any colour. Borders may also be set to transparent
.
Properties: |
|
---|---|
Default value: | none |
Inherited: | No |
The type of border you want is also important. You have the following options:
You can set each aspect of the border so that they apply to all sides at once (as described in Short-hand Expressions
earlier in this chapter) using the following properties:
You may also lump style, width, and colour into a single declaration for each side with these properties:
In this case, you would state the width, style, and colour of the border. The first four apply to only one side, the fifth creates a uniform border all around. The width, style, and colour must all be included in each declaration, but may go in any order.
The webpage below not only demonstrates the borders, but also demonstrates the shorthand notation mentioned in the introduction to this chapter.
And here is the stylesheet:
Properties: |
|
---|---|
Default value: | 0 (all padding properties) |
Inherited: | No (all padding properties) |
Padding creates a space between the content of an element and the border. It is expressed as a length or as a percentage of the element's width.
The padding
property allows you to define the padding of the four sides in a single declaration, as described above.
And the style sheet of the above:
Properties: |
|
---|---|
Default value: | 0 |
Inherited: | No (all margin properties) |
The margin is the space between the border and the rest of the page. It is expressed as a length or as a percentage of the element's width.
The margin
property allows you to define the margin of the four sides in a single declaration, as described above. Unlike borders and paddings, a margin may be set to a negative length.
Below is the style sheet for the above page: