Tutelage

Lessons › coding and computer science · taught by Leo

CSS basics: style a webpage

Learn how CSS selectors, properties, and values work, plus the box model for padding and margin.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

Why CSS changes HTML's look

CSS is the styling language for web pages. HTML gives the page its parts, and CSS makes those parts look good.

Think of it like a room. HTML is the furniture, and CSS is the paint, colors, and spacing.

You usually keep them separate so the structure stays clean.

The CSS rule pattern

A CSS rule has three parts: selector, property, and value.

The selector points to the HTML element you want.

The property says what you want to change, like color or font-size.

The value is the exact setting, like blue or 16 pixels.

Predict the rule parts

In this CSS, what is the selector? button { color: red; }

  1. button
  2. color
  3. red
Show answer

button: Yes. The selector is button, because it names the element to style.

Write one CSS rule

Let’s build one rule from scratch. Suppose you want all paragraphs to be blue.

First you write p, because p means paragraph.

Then you write color: blue, because you want text color changed to blue.

Braces hold the rule together, and the semicolon ends the declaration.

What should come next?

You want to style all h1 headings. What selector should you start with?

  1. h1
  2. heading
  3. title
Show answer

h1: Correct. h1 targets all level-one headings.

Why the selector works

The selector works because CSS matches on names. If the HTML says h1, CSS can grab every h1 on the page.

CSS is a matcher. It looks for the element name, class, or id you wrote.

That is why the selector must match something real in the HTML.

If nothing matches, nothing gets styled.

Class and id selectors

Two very common selectors are class and id.

A class can be reused on many elements, so it is good for repeated styles.

An id should be unique, so it is good for one special element.

A class and an id in use

If two buttons should look alike, give them the same class.

If one button needs a unique style, give it an id.

That way you can reuse style without copying code.

Pick the better selector

You want to style three cards the same way. Which is the better choice?

  1. class
  2. id
  3. inline only
Show answer

class: Yes. A class is best because it can be reused on many elements.

Spacing with box model

CSS also controls space around elements. The box model helps you see it.

Content is inside. Padding is the space between content and border.

Margin is the space outside the border, between this box and others.

A simple box-model example

Let’s use the box model on a paragraph.

margin pushes the paragraph away from nearby elements.

padding gives the text breathing room inside the box.

border draws the edge so you can see the box.

Which space is inside?

If you want more room between the text and the border, what do you change?

  1. padding
  2. margin
  3. selector
Show answer

padding: Correct. Padding is inside the border.

Recap: CSS starter checklist

Here is your quick checklist for CSS.

First pick the selector that matches the HTML.

Then choose the property, set the value, and check spacing if needed.

If you remember those four steps, you can write basic CSS with confidence.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app