Skip to main content
Albero Academy

CSS Fundamentals & Selectors

How CSS attaches to HTML, the rule syntax, the selectors you use daily, and how the cascade & specificity decide winners.

Chapter 1CSS14 min readBeginner friendly
  • #CSS
  • #Selectors

HTML gives a page structure; CSS — Cascading Style Sheets — gives it a look. Colours, spacing, fonts, layout, animation: all CSS. The word cascading is the key idea — many rules can target the same element, and CSS has a precise system for deciding which wins. Get selectors and the cascade right and everything else falls into place.

What is CSS?

CSS is a list of rules. Each rule says "for elements matching this selector, apply these styles". The browser gathers every rule that matches an element and resolves conflicts using the cascade. You never change the HTML's meaning — only its presentation.

Three ways to attach CSS

You can hook styles onto HTML three ways — but in real projects you'll use the third almost exclusively:

inline · internal · external
<!-- 1. Inline (avoid — hard to reuse and maintain) -->
<p style="color: navy;">Hello</p>
<!-- 2. Internal (fine for a one-off page) -->
<head>
<style>
p { color: navy; }
</style>
</head>
<!-- 3. External (do this) -->
<head>
<link rel="stylesheet" href="styles.css">
</head>

Rule syntax

Every rule is a selector plus a block of declarations. Each declaration is a property: value; pair:

anatomy of a rule
selector {
property: value; /* one declaration */
color: #2563eb; /* another */
}
/* Real example */
button {
background: #2563eb;
color: white;
padding: 12px 24px;
border-radius: 8px;
}

Selectors

Selectors decide what a rule targets. The five you'll use constantly:

SelectorMatchesExample
tagEvery element of that typep { … }
.classElements with class="…".btn { … }
#idThe one element with that id#hero { … }
a, bGroup — match a OR bh1, h2 { … }
a bDescendant — b inside anav a { … }

Classes are the workhorse. They're reusable and low-specificity, which keeps your CSS predictable. You'll also lean on pseudo-classes for state and attribute selectors for targeting by attribute:

pseudo-classes & attribute selectors
a:hover { text-decoration: underline; } /* on hover */
input:focus { outline: 2px solid #2563eb; } /* when focused */
li:first-child { font-weight: 600; } /* the first item */
tr:nth-child(2n) { background: #f6f6f3; } /* zebra stripes */
a[target="_blank"]::after { content: " ↗"; } /* by attribute */

Cascade & specificity

When several rules set the same property on one element, three things decide the winner, in order:

  • Importance & origin — an author rule beats a browser default; !important overrides normal rules (use it rarely).
  • Specificity — #id (100) beats .class (10) beats tag (1). More specific wins.
  • Source order — if specificity ties, the rule written later wins.
specificity decides
p { color: black; } /* specificity 1 */
.intro { color: gray; } /* specificity 10 — wins over the tag */
#lead { color: navy; } /* specificity 100 — wins over both */

Practice

Take the "About me" page from the HTML course and link an external stylesheet. Give the page a background colour, style your headings, and make links change colour on :hover — using classes, not ids.

Key takeaways

  1. 01CSS is rules: a selector plus property: value declarations. Prefer one external stylesheet.
  2. 02Class selectors are the workhorse — reusable and low-specificity.
  3. 03Pseudo-classes (:hover, :focus, :nth-child) target state and position.
  4. 04Conflicts resolve by importance, then specificity (#id > .class > tag), then source order.
  5. 05Avoid ids and !important for styling — they create specificity you can't override cleanly.