...
Sign in

How CSS Works: Rulesets, Selectors, & Styling

Start styling websites from scratch: understand CSS ruleset anatomy, element vs class vs ID selectors, the 3 ways to link CSS to HTML, and fundamental typography and color properties.

Share:

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;}
  1. Selector: Points to the HTML element(s) you want to style (e.g. p, .button, #navbar).
  2. Declaration Block: Enclosed in curly braces { ... }, containing one or more declarations.
  3. 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 Core

Understand 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).

{
:;
}
Inspected Component:
Selector: h1.title

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

MethodSyntaxWhere It GoesIndustry 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 tagAvoid (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 Sandbox

Select presets, switch between desktop/tablet/mobile preview modes, or toggle the canvas background.

index.htmlLive Auto-Compile
Live Output Browser View100% Fluid

7. Flashcards & Core Concepts

Active Recall
Card

What is the difference between a class selector (.) and an ID selector (#) in CSS?

Click or Press Space to Flip
Key Takeaway
A class (.name) can be reused across multiple elements throughout the page. An ID (#name) must be unique to a single element per HTML document.
Click to Flip Back
Active Recall
Card

Why is an external stylesheet (<link>) preferred over inline styles?

Click or Press Space to Flip
Key Takeaway
External stylesheets separate design from HTML markup, allow styles to be shared across hundreds of pages, and are cached by web browsers for faster page loads.
Click to Flip Back

8. Knowledge Check

CheckpointCheckpoint
Question 1 of 3Score: 0/3

Which character precedes a class name in a CSS selector?

Lesson Milestone

Finished Reading This Lesson?

Mark this lesson complete. A chapter counts as done once all its lessons are, and that adds to your course progress.

TH

Written by

ThinkWisely Team

Have a Question or Suggestion?

We'd love to hear from you. Drop your thoughts below to help us improve ThinkWisely!