CSS Introduction
Today we learn CSS: how a rule picks an HTML element with a selector, then sets a property to a value so the browser can paint the page.
What you'll learn in this lesson
- 01HTML is the skeleton, CSS is the style & interior
- 02Anatomy of a rule: Selector, Property, and Value
- 03Why the semicolon (;) is the vital punctuation
- 04How the browser reads and applies declarations
- 05First live rule: styling headings and text
;.- 1
Why CSS Was Created: The Separation of Meaning and Style
In early HTML, developers used tags like
<font color="red">and<center>scattered across every line. If a company decided to change its brand color, engineers had to manually update hundreds of pages and thousands of tags.Cascading Style Sheets (CSS) was invented to separate content from design. HTML dictates what an element is (a heading, a paragraph, a button). CSS dictates how it should look (color, size, spacing, animations).
- 2
The 3 Essential Ingredients of Every CSS Rule
Every line of CSS you will ever write follows this exact pattern:
- Selector: Tells the browser which elements to target (e.g.,
p,h1, or.card). - Declaration Block: Everything inside curly braces
{ ... }. - Property & Value: Each instruction is written as
property: value;, with a colon in between and a mandatory semicolon at the end.
- Selector: Tells the browser which elements to target (e.g.,
- 3
How the Browser Reads and Paints Your Rules
When a browser parses an HTML document, it simultaneously constructs the CSS Object Model (CSSOM). It matches each selector against the DOM elements, calculates computed styles, and paints the pixels directly onto the display canvas.
If a single semicolon is omitted, the browser cannot know where that declaration ends, causing subsequent styles in that block to quietly fail.
- 4
Hands-On Practice: Styling Text with Pure CSS
Look at the code below. Notice how the selector
ptargets all paragraphs, setting a clean electric blue color,16pxfont size, and centering the text alignment. - 5
The Golden Rule: Never Forget the Semicolon
In CSS, semicolons are not decorative—they are statement terminators. Treat every line inside
{ }as an imperative command that must be sealed with;.
Line by line
p {The selector. This rule tells the browser to find and style every
<p>element on the page. The opening curly brace starts the declaration block.color: #0284c7;Sets the foreground text color to a modern ocean blue. The semicolon terminates the command.
font-size: 16px;Sets the typography height.
16pxis standard default base size on the web.text-align: center;Aligns text horizontally along the center of its container. The closing
}completes the rule.
Try it yourself
In the interactive editor above, write a CSS rule that targets every h1, makes its color #4f46e5 (electric indigo), sets its font-size to 32px, and centers it.
Hint: Use the same anatomy: h1 { color: ...; font-size: ...; text-align: ...; }
💡 View Solution (if stuck)
h1 {
color: #4f46e5;
font-size: 32px;
text-align: center;
}What happens if you accidentally omit the semicolon (;) at the end of a CSS property value?
Common mistakes
- 1.Forgetting the semicolon. The next declaration merges with it and both fail silently.
- 2.Writing properties without curly braces or without a selector.
- 3.Using equal signs
color = red;instead of a coloncolor: red;.
Interview prep (Click to reveal)
Q: What does CSS stand for and what does 'Cascading' mean?▼
CSS stands for Cascading Style Sheets. 'Cascading' means styles flow down and inherit from parent to child, and when multiple rules compete for the same element, the cascade calculates specificity and source order to decide the winner.
Q: What are the three parts of a CSS rule?▼
1. Selector (identifies the element), 2. Declaration Block (enclosed in curly braces), and 3. Declarations (composed of property: value; pairs).
Q: What is the fundamental difference between HTML and CSS?▼
HTML defines the structure, semantic meaning, and content of a page. CSS defines the visual presentation, layout, colors, typography, and responsive adjustments.