HTML and CSS

Lesson 5 of 10

Lesson ยท 10 min

Colors and fonts

What CSS does

HTML says what each part of a page is. CSS says how it looks. A CSS file is a list of rules. Each rule picks some elements and gives them new properties.

h1 {
  color: navy;
  font-size: 32px;
}

A rule has three parts:

  • The selector says which elements to change. Here it is h1.
  • A property is the thing to change, such as color.
  • A value is the new setting, such as navy. The property and the value are separated by a colon, and the pair ends with a semicolon.

The rules go between braces { }. The index.html file in this lesson is already linked to style.css, so you only write in style.css.

Colors

You can write a color in three common ways:

  • By name: red, navy, tomato.
  • As a hex code: #1d4ed8. The six digits are the amounts of red, green, and blue.
  • With rgb(): rgb(29, 78, 216).

color changes the color of text. background-color changes the color behind it.

Fonts and sizes

  • font-family lists fonts to try, in order. Always end the list with a general family such as serif, sans-serif, or monospace.
  • font-size sets the size of text. The unit px means pixels. Normal text is 16px.
  • text-align moves text to the left, center, or right.

The preview cannot load fonts from other websites. Use fonts that are already on the computer, such as Georgia, Arial, or Courier New.

Classes

To style one element and not the others, give the element a class in the HTML, then use the class name in the selector with a dot in front. In this lesson, the first paragraph has class="lead", so the selector .lead finds only that paragraph.

Your turn

Write your rules in style.css. The index.html tab is read-only so that you can read it, but not change it.

Tasks

5 to do

  1. Task 1, not checked yet. Give the body a light yellow background. Set background-color to #fef9c3.

  2. Task 2, not checked yet. Make the h1 dark red. Set its color to #991b1b.

  3. Task 3, not checked yet. Center the h1. Set text-align to center.

  4. Task 4, not checked yet. Make all p elements use a serif font. Set font-family to Georgia, serif.

  5. Task 5, not checked yet. Make only the paragraph with the class lead bigger. Use a class selector, .lead, and set font-size to 22px.

Task 1 of 5

Give the body a light yellow background. Set background-color to #fef9c3.

All tasks

You can read this file, and you cannot change it. Write your code in style.css.

Preview

Your page shows here