CSS Styling
Transform plain HTML into polished web experiences using CSS selectors, property declarations, units, and the cascade.
While HTML constructs the structural skeleton of a web page, raw HTML by itself looks like a 1990s scientific document: unstyled black Times New Roman text on a plain white background with underlined blue links.
CSS (Cascading Style Sheets) is the language of visual presentation. It lets you paint backgrounds, adjust typography, control spacing, position buttons, and build responsive layouts that look stunning on smartphones, tablets, and desktop monitors alike.
The Anatomy of a CSS Rule
A CSS document is a collection of style rules. Each rule consists of a selector and a declaration block:
/* Selector: Who gets styled? */
h1 {
/* Property: What is changing? */
color: #2563eb; /* Value: What should it be? */
font-size: 2.25rem;
text-align: center;
}
h1 { color: #2563eb; }
โ โโโโฌโโ โโโโโฌโโโโ
โ โ โโโโโโโ Value (Must end with a semicolon!)
โ โโโโโโโโโโโโโโโ Property
โโโโโโโโโโโโโโโโโโโโโโโ Selector (Targets all <h1> tags)
[!IMPORTANT] Every CSS declaration must end with a semicolon (
;). Omitting a single semicolon breaks that declaration and prevents subsequent declarations in the block from executing!
Three Ways to Apply CSS
There are three ways to apply CSS to HTML documents:
1. External Stylesheet (The Production Standard)
Write styles in a dedicated .css file (e.g. styles.css) and link it inside the <head> of your HTML document:
<head>
<link rel="stylesheet" href="styles.css" />
</head>
Why this is preferred: Your browser downloads and caches the stylesheet once. You can style 500 pages with a single shared file.
2. Internal Styles (Useful for quick one-off pages)
Place a <style> tag directly inside your HTML <head>:
<head>
<style>
body { background-color: #f8fafc; }
</style>
</head>
3. Inline Styles (Avoid in production)
Write styles directly on an individual tag using the style attribute:
<p style="color: red; font-size: 14px;">Warning message</p>
Why this is discouraged: It clutters your HTML, duplicates code, and is notoriously difficult to maintain or override.
CSS Selectors: Targeting Elements
To style elements effectively, you need to select them accurately:
| Selector | Syntax | Example | What It Targets |
|---|---|---|---|
| Element / Tag | tag | p { } | Every <p> element on the page |
| Class | .classname | .btn { } | Any element with class="btn" (Reusable) |
| ID | #idname | #navbar { } | The single element with id="navbar" (Unique) |
| Descendant | ancestor descendant | .card p { } | Any <p> nested inside a .card |
| Hover State | :hover | button:hover { } | Applies styles when the mouse hovers over it |
<!-- HTML -->
<button class="btn btn-primary">Submit</button>
/* CSS */
.btn {
padding: 8px 16px;
border-radius: 6px;
cursor: pointer;
}
.btn-primary {
background-color: #2563eb;
color: #ffffff;
}
.btn-primary:hover {
background-color: #1d4ed8;
}
What Does โCascadingโ Actually Mean?
Why is CSS called Cascading Style Sheets?
When multiple CSS rules target the same HTML element, the browser needs a way to decide which rule wins. It resolves conflicts using three criteria:
Conflict Resolution Order
โ
โโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโ
โผ โผ
Specificity Order of
(More specific wins: Appearance
ID > Class > Element) (Later rules win)
- Specificity: An ID selector (
#header) is stronger than a class selector (.title), and a class selector is stronger than a generic element selector (h1). - Order of Appearance: If two conflicting rules have the exact same specificity, whichever rule appears later in the stylesheet wins.
- Inheritance: Some properties (like
colorandfont-family) automatically flow down from parent elements (<body>) to children.
[!WARNING] You might see people use
!important(e.g.color: red !important;) to force a rule to win. Avoid this! Overusing!importantcreates โspecificity warsโ that make stylesheets unmaintainable. Fix the selector specificity instead.
Modern Sizing Units: px vs rem
px(Pixels): Fixed, absolute size. Does not scale if a user changes their browserโs default font size.rem(Root EM): Relative to the root<html>font size (default is usually16px). If a user increases their browser text size for visual impairment,1remscales automatically!
body {
font-family: system-ui, -apple-system, sans-serif;
font-size: 1rem; /* 16px */
line-height: 1.5; /* 1.5x font size for comfortable reading */
}
h1 {
font-size: 2rem; /* 32px */
}
Common Traps to Avoid
- Missing Semicolons: If you write
color: blue font-size: 14px;, the browser cannot parse either property. Always end each declaration with;. - Confusing Classes and IDs: Classes use a dot (
.card), while IDs use a hash (#card). Classes are designed for reuse across multiple elements; IDs must be unique to a single element per page. - Relying on Inline Styles: Inline styles override external stylesheets and make responsive mobile redesigns nearly impossible.
- Hardcoding Fixed Pixel Heights: Giving containers fixed heights like
height: 200px;causes text to overflow and spill out when viewed on smaller screens or different languages. Let content dictate height naturally.
Next Steps
Now that you can style text and colors, how do web elements calculate their width, spacing, and boundaries on the screen?
Master the foundation of all CSS layout in the CSS Box Model.