Skills / Web pages with HTML and CSS / Styling with CSS

Web pages with HTML and CSS, lesson 2 of 8

Styling with CSS

Most of what makes a page look professional comes from four things in CSS: colour, type, spacing and the box model.

Colour. Set text colour with color and backgrounds with background-color. Colours can be names (navy), hex codes (#1a73e8) or rgb(26, 115, 232). Pick two or three colours and use them consistently.

Type. font-family chooses the font, font-size its size and line-height the space between lines. Body text is easiest to read at around 16 to 18 pixels with a line height of about 1.5.

The box model. The browser treats every element as a box made of four layers, from the inside out:

  1. Content: the text or image itself
  2. Padding: space inside the box, around the content
  3. Border: an optional line around the padding
  4. Margin: space outside the box, pushing other boxes away

So padding: 20px gives a button room to breathe inside, while margin-bottom: 24px keeps the next paragraph at a distance. Add box-sizing: border-box to all elements so a box's width includes its padding and border; layouts become far easier to predict.

Classes. To style only some elements, give them a class in the HTML (<p class="note">) and select it in CSS with a dot: .note { background-color: #fff6e0; }.

Good CSS is consistent: the same spacing steps (8, 16, 24, 32 pixels) and the same few colours everywhere.

Try it: Write a CSS rule for a class called .card that gives it 24px of padding, a 1px solid grey border and 16px of space below it.

Check yourself

1. Which layer of the box model is the space INSIDE the border, around the content?

2. How do you select every element with class="note" in CSS?

3. What does box-sizing: border-box change?