How CSS Works: Rulesets, Selectors, & Styling
If HTML is the raw skeleton of a webpage, CSS (Cascading Style Sheets) is the interior designer.
CSS controls:
- Colors & Backgrounds (text color, gradients, dark mode palettes)
- Typography (fonts, font sizes, line height, letter spacing)
- Spacing & Sizing (margins, padding, widths, heights)
- Layouts & Animations (grids, flexbox columns, smooth transitions)
Without CSS, every webpage on the internet would look like a plain black-and-white 1993 research paper!
1. The Anatomy of a CSS Ruleset
A CSS document is a collection of rulesets. Each ruleset tells the browser: "Find these HTML elements, and apply these visual styles to them."
/* ┌──────── Selector (Which elements to target) │ ┌── Property (What visual feature to change) │ │ ┌── Value (What setting to apply)┌──┴──┐┌─┴─┐ ┌──┴───┐ */h1.title { color: crimson; font-size: 28px;}- Selector: Points to the HTML element(s) you want to style (e.g.
p,.button,#navbar). - Declaration Block: Enclosed in curly braces
{ ... }, containing one or more declarations. - Declaration: A pair consisting of a Property and a Value, separated by a colon (
:) and terminated with a semicolon (;).
2. Interactive CSS Ruleset & Live Styler Studio
Inspect how selectors, properties, and values connect, test the 3 ways to link CSS, and adjust live colors, typography, and border radius in real time.
CSS Syntax, Selectors, & Styling Studio
Beginner CoreUnderstand CSS rulesets, selectors, linking methods, and live visual properties.
Anatomy of a CSS Ruleset
A CSS ruleset targets one or more HTML elements using a Selector and applies visual styles using a block of Declarations (Property: Value pairs).
Points to which HTML element(s) to style. Here, it targets any <h1> that has the class class="title".
3. The 3 Ways to Add CSS to HTML
| Method | Syntax | Where It Goes | Industry Recommendation |
|---|---|---|---|
| External Stylesheet | <link rel="stylesheet" href="style.css"> | Inside <head> | Best Practice (100% recommended for real websites) |
| Internal Style Tag | <style> h1 { color: red; } </style> | Inside <head> | Good for quick single-page prototypes or email templates |
| Inline Style | <h1 style="color: red;"> | On the HTML tag | Avoid (hard to maintain, overrides stylesheets) |
<!-- Inside index.html: The Best Practice --><!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <title>My Website</title> <!-- Linking external stylesheet: --> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>Hello World</h1> </body></html>4. The 3 Core Selectors Every Beginner Must Know
1. Element Selector (Tag Name)
Targets all elements with that tag name on the entire page:
p { color: #334155; line-height: 1.6;}2. Class Selector (Preceded by a dot .)
Targets elements with a matching class="..." attribute. Classes can be reused multiple times across any number of elements:
/* Styles any element with class="highlight" */.highlight { background-color: #fef08a; font-weight: bold;}3. ID Selector (Preceded by a hash #)
Targets the single element with a matching id="..." attribute. In standard HTML, an ID must be completely unique on the entire page:
/* Styles only the single element with id="main-header" */#main-header { background-color: #0f172a; color: white;}5. Essential Beginner Properties
Colors: 3 Ways to Declare
.card { color: royalblue; /* 1. Named keyword */ color: #2563eb; /* 2. Hex code (Standard in design tools) */ color: rgb(37, 99, 235); /* 3. RGB (Red, Green, Blue) */ color: rgba(37, 99, 235, 0.5); /* 4. RGBA (with 50% opacity/alpha) */}Basic Typography
body { font-family: 'Inter', system-ui, sans-serif; /* Font face */ font-size: 16px; /* Base size */ font-weight: 400; /* 400 = normal, 700 = bold */ text-align: center; /* left, center, right, justify */}6. Hands-On Live Coding Playground
Put your CSS skills to work right away! Edit properties like color, background, border-radius, or box-shadow in styles.css and watch the live preview react instantaneously:
Interactive CSS Ruleset Sandbox
Live SandboxSelect presets, switch between desktop/tablet/mobile preview modes, or toggle the canvas background.
7. Flashcards & Core Concepts
What is the difference between a class selector (.) and an ID selector (#) in CSS?
Why is an external stylesheet (<link>) preferred over inline styles?