Home / Learn HTML / HTML Styles and CSS

HTML Styles and CSS

CSS controls colours, spacing and layout. You can add it in three places.

Definition

CSS (Cascading Style Sheets) controls how HTML looks. There are three ways to add it.

WayWhereUse when
Inlinestyle="..." attribute on an elementA single quick change
Internal<style> in the headOne page only
ExternalA .css file linked with <link rel="stylesheet">Whole websites (best)

When rules compete the more specific wins; with equal strength the later rule wins. Inline styles beat style sheets.

Theory in plain words

Three ways, from weakest to strongest (in practice)External file: shared by all pagesstyle element: one pagestyle attribute: one element
Three ways, from weakest to strongest (in practice)

What it is

CSS can be added three ways: inline style attribute, a style element, or an external file with link.

Why we use it

HTML gives structure, CSS gives design. Splitting them keeps code clean and lets one file style the whole site.

How it works

The browser matches each CSS rule to elements. If rules conflict, more specific rules and later rules win.

Where it is used

  • External style.css for real sites
  • Inline styles for emails
  • style block for quick demos
1. Inline styleTry it Yourself
2. Internal style (in the head)Try it Yourself
3. External file (shown as a link tag)Try it Yourself

Best for real websites because many pages share one file.

Colours: names, hex, rgb, hslTry it Yourself
Background and gradientTry it Yourself
FontsTry it Yourself
Text styleTry it Yourself
Box model (margin, border, padding)Try it Yourself
Border and radiusTry it Yourself
ShadowTry it Yourself
Hover effectTry it Yourself
AnimationTry it Yourself
Dark mode switchTry it Yourself
Responsive with media queryTry it Yourself

Open full screen and make the window narrower.

CSS variablesTry it Yourself

Quick quiz

1. Which is inline CSS?

2. Best for many pages?

3. Which is the hover selector?

Common questions

Which is inline CSS?

style attribute. style="..." on the element.

Best for many pages?

External file. One file shared by all pages.

Which is the hover selector?

:hover. a:hover styles on mouse over.

My notes
Ask a question about this lesson