Learn What is CSS?

CSS (Cascading Style Sheets) controls how a page looks, and HTML holds its content. One CSS declaration is a property (what to change), a colon, a value (what…

Your first HTML page probably looks plain: black text on a white background, bold headings, everything on the left side. The content is all there, but its look has not been set at all.

CSS (Cascading Style Sheets) sets the look: colours, font sizes, spacing, even the layout. HTML decides what is on the page, and CSS decides how it looks. If a web page is a house, HTML is the frame and CSS is the paint and decoration. There are three ways to add CSS to a page.

Way 1: a style attribute on the tag

Write the CSS on the tag you want to change, inside a style attribute.

<h1 style="color: blue;">Hello, World!</h1>
<p>This is the first paragraph.</p>

The result: the heading turns blue and the paragraph stays black. A style attribute only applies to the tag it is written on. This way is called inline CSS.

What goes inside the style attribute has to be a complete declaration: style="color: red;". With only style="red", the browser doesn't know what should turn red.

Way 2: the <style> tag

Write the CSS inside a <style> tag, which goes in <head>.

<head>
  <style>
    h1 {
      color: blue;
    }
  </style>
</head>
<body>
  <h1>Hello, World!</h1>
</body>

Inside <style> you write CSS rules:

The result: every <h1> on this page turns blue, without a single style attribute. This way is called internal CSS. What goes in <style> has to be a complete rule. A declaration with no selector, such as <style>color: red;</style>, applies to no element at all.

Property names and values are English words in American spelling, and every letter has to match. Try to guess: if you write colour: red; (the British spelling), does the heading turn red? No. The browser doesn't know that property, so it skips it silently, with no error message.

Way 3: a CSS file of its own

Write the CSS rules in a separate file, for example style.css. A <link> tag in <head> attaches that file to the page.

<link rel="stylesheet" href="style.css">

rel="stylesheet" tells the browser that the file contains CSS.

This way is called external CSS. One CSS file can be attached to many pages. The browser downloads the file once, then keeps a copy (that stored copy is called the cache). So CSS that many pages use goes in a file of its own. A little CSS for one page can stay in <style>.

What "cascading" means

Several rules can apply to one element and disagree, for example when one rule makes it blue and another makes it red. The rules stack on that element, and the browser picks one winner: that is what cascading means. A more specific selector beats a more general one: a selector for one element beats h1, which picks every <h1>. When two selectors are equally strong, the rule written later wins.

⚠️ Why isn't the style showing up?

TL;DR: CSS (Cascading Style Sheets) controls how a page looks, and HTML holds its content. One CSS declaration is a property (what to change), a colon, a value (what to change it to) and a semicolon. For example, color: red; makes text red. There are three ways to add CSS: a style attribute on one tag (inline), a selector { ... } rule inside a <style> tag (internal), or a .css file attached with a <link> tag (external, the best choice when many pages use it). The selector picks which elements a rule is for. When two rules disagree, the more specific selector wins (a selector for one element beats a selector for every <h1>). When they are equally strong, the rule written later wins.

⚡ Property, colon, value

Write:

<h1 style="color: blue;">Hello, World!</h1>
Guess first: written as colour: blue; (with a u), does the heading turn blue?

No. It has to be spelled exactly color (the American spelling). The browser silently skips a word it doesn't know, with no error message.

Meaning: The style attribute holds CSS, the language that sets how a page looks. color is the property (what you change, here the text colour). blue is the value (what you change it to). The colon separates the property from the value, and the semicolon ends the pair. The pair color: blue; is called a declaration.

⚡ One rule for every heading

Write:

h1 {
  color: blue;
}

Meaning: This rule goes inside a <style> tag. h1 is the selector: the part that picks which elements the rule is for. The result: every <h1> on the page turns blue, without a single style attribute. CSS that many pages use goes in a .css file of its own, attached with a <link> tag.

⚡ When rules disagree

Guess first: two rules give one element different colours. Which rule is used?

The rule with the more specific selector: a selector for one element beats h1, which picks every <h1>. When two selectors are equally strong, the rule written later wins.

Meaning: Several rules can stack on one element, and the browser picks one winner. That is what cascading means in the name CSS.

What you will learn