Complete CSS Tutorial
First Step - Beginner CSS | Second Step - Intermediate CSS | Third & Final Step - Advanced CSS |
---|---|---|
1. Applying CSS | 1. Class and ID Selectors | 1. Display |
2. Selectors, Properties, and Values | 2. Grouping and Nesting | 2. Page Layout |
3. Colors | 3. Pseudo Classes | 3. At-Rules |
4. Text | 4. Shorthand Properties | 4. Pseudo Elements |
5. Margins and Padding | 5. Background-images | 5. Specificity |
6. Borders | ||
7. Putting It All Together |
Margin
and Padding
are the two most commonly used properties for spacing-out elements. A margin is the space outside of the element, whereas padding is the space inside the element.
Change the CSS code for
You will see that this leaves one-character width space around the secondary header and the header itself is fat from the three-character width padding.
The four sides of an element can also be set individually.
h2
to the following:Teach Yourself by BH24 HTML Editor Tool!h2 { font-size: 1.5em; background-color: #ccc; margin: 1em; padding: 3em; }
You will see that this leaves one-character width space around the secondary header and the header itself is fat from the three-character width padding.
The four sides of an element can also be set individually.
margin-top
, margin-right
, margin-bottom
, margin-left
, padding-top
, padding-right
, padding-bottom
and padding-left
are the self-explanatory properties you can use.![]() |
CSS Tutorial by BH24 |
The Box Model
Margins, padding and borders (see next page) are all part of what's known as the Box Model. The Box Model works like this: in the middle you have the content area (let's say an image), surrounding that you have the padding, surrounding that you have the border and surrounding that you have the margin. It can be visually represented like this:
Margin box
You don't have to use all of these, but it can be helpful to remember
that the box model can be applied to every element on the page, and
that's a powerful thing!
Border box
Padding box
Element box