WebDev Story

WebDev Story: CSS Summary

definition, selectors, cascade, units, references

7.0 What is CSS?

Cascading Style Sheets (CSS) provide a declarative language for controlling the presentation of HTML elements. HTML gives a page its structure and semantic meaning; CSS defines colors, layout, typography, spacing, and overall visual style. Separating content from presentation lets a single stylesheet apply a consistent design across many pages. CSS works with selectors and rules. A selector targets specific elements; each rule is a list of property: value declarations:
selectors { property: value; ... }

/* examples */
body  { padding: 5%; background-color: #ddd; }
h1    { font-size: 2rem; color: steelblue; }
.note { font-style: italic; margin-left: 1rem; }
Selectors are built from combinations of:
htmltags:         body, div, span, img, ...
custom-tags:      nav-keys, spacer-10, ...
.classnames:      .indent, .undent, .photo, ...
#idnames:         #placeHolder, #mastheadPhoto, ...
pseudo-classes:   a:link, a:hover, ...
pseudo-elements:  p::first-letter, p::first-line, ...
attributes:       [attr], [attr=value], ...
Selectors can be combined with operators:
s1, s2, ...   applies to both s1 and s2
s1 s2         applies to s2 anywhere inside s1 (descendant)
s1 > s2       applies to s2 only when s1 is its direct parent
s1 + s2       applies to s2 if s1 is its adjacent preceding sibling
s1 ~ s2       applies to s2 if s1 is any preceding sibling
Note: if any part of a style declaration is syntactically invalid, the entire declaration is silently discarded by the browser.

7.1 CSS Styles Keyword Reference

Many CSS properties accept predefined keywords as values. The table below lists the most commonly used keywords grouped by property category.
Table 1. — CSS Property Keywords
Group Property Predefined Keywords
Global (work on almost all properties)
Global(any property)inherit, initial, unset, revert, revert-layer
Display & positioning
Displaydisplaynone, block, inline, inline-block, flex, inline-flex, grid, inline-grid, flow-root, contents, list-item, table, table-row, table-cell
Positioningpositionstatic, relative, absolute, fixed, sticky
Overflowoverflowvisible, hidden, clip, scroll, auto
Visibilityvisibilityvisible, hidden, collapse
Box sizingbox-sizingcontent-box, border-box
Borders, outlines & text decoration
Border/Outline styleborder-style, outline-stylenone, hidden, dotted, dashed, solid, double, groove, ridge, inset, outset
Text decoration styletext-decoration-stylesolid, double, dotted, dashed, wavy
Text decoration linetext-decoration-linenone, underline, overline, line-through
Backgrounds & images
Background repeatbackground-repeatrepeat, repeat-x, repeat-y, no-repeat, space, round
Background attachmentbackground-attachmentscroll, fixed, local
Background sizebackground-sizeauto, cover, contain
Object fitobject-fitfill, contain, cover, none, scale-down
Typography
Font familyfont-familyserif, sans-serif, monospace, cursive, fantasy, system-ui, ui-serif, ui-sans-serif, ui-monospace
Font stylefont-stylenormal, italic, oblique
Font weightfont-weightnormal (400), bold (700), lighter, bolder, 100900
Text transformtext-transformnone, capitalize, uppercase, lowercase
Text aligntext-alignstart, end, left, right, center, justify
White spacewhite-spacenormal, nowrap, pre, pre-wrap, pre-line, break-spaces
Text overflowtext-overflowclip, ellipsis
Lists
List style typelist-style-typenone, disc, circle, square, decimal, lower-roman, upper-roman, lower-alpha, upper-alpha
List positionlist-style-positioninside, outside
Floats & clears
Floatfloatleft, right, none, inline-start, inline-end
Clearclearleft, right, both, none
Table layout
Border collapseborder-collapseseparate, collapse
Table layouttable-layoutauto, fixed
Effects & misc
Cursorcursorauto, default, pointer, text, move, wait, crosshair, not-allowed, help, progress
Resizeresizenone, both, horizontal, vertical
Pointer eventspointer-eventsauto, none
Mix blend modemix-blend-modenormal, multiply, screen, overlay, darken, lighten, difference, exclusion, hue, saturation, color, luminosity

7.2 Selectors

Selectors target elements based on tag name, class, ID, attribute, DOM position, and meta-descriptors like :hover and :first-child.
Table 2. — CSS Selector Reference
Selector Description Example
Basic selectors
Universal (*)All elements* { margin: 0; }
Type (element)All instances of an elementp { color: blue; }
Class (.class)Elements with a given class.note { font-style: italic; }
ID (#id)Single element with given ID#header { background: gray; }
Attribute selectors
[attr]Elements with the given attribute[title] { border: 1px solid; }
[attr=value]Attr equals value exactlyinput[type="text"] { width: 200px; }
[attr^=value]Attr starts with stringa[href^="https"] { color: green; }
[attr$=value]Attr ends with stringa[href$=".pdf"] { color: red; }
[attr*=value]Attr contains substring[class*="warning"] { color: orange; }
Combinators
Descendant (A B)B inside A at any depthdiv p { line-height: 1.5; }
Child (A > B)B is direct child of Aul > li { list-style: none; }
Adjacent sibling (A + B)B immediately after Ah1 + p { font-size: 1.2em; }
General sibling (A ~ B)B preceded by A (any distance)h1 ~ p { color: gray; }
Pseudo-classes
:hoverElement under mouse pointera:hover { text-decoration: underline; }
:focusElement that has keyboard focusinput:focus { outline: 2px solid blue; }
:first-childFirst child of its parentp:first-child { font-weight: bold; }
:last-childLast child of its parentp:last-child { color: blue; }
:nth-child(n)nth child, pattern-basedli:nth-child(2n) { background: #eee; }
:not(selector)Excludes matching elementsp:not(.note) { color: black; }
:checkedChecked radio/checkboxinput:checked { outline: 2px solid blue; }
:disabled / :enabledForm element stateinput:disabled { opacity: 0.4; }
:emptyElement with no childrentd:empty { background: #fdd; }
Pseudo-elements
::beforeGenerated content before elementp::before { content: "→ "; }
::afterGenerated content after elementp::after { content: " ★"; }
::first-lineFirst rendered line of textp::first-line { font-weight: bold; }
::first-letterFirst rendered letterp::first-letter { font-size: 2em; }
::selectionUser-selected text::selection { background: yellow; }
::placeholderForm input placeholder textinput::placeholder { color: #aaa; }

7.3 Cascade & Specificity

When multiple CSS rules target the same element, the browser uses a cascading algorithm to decide which declaration wins. The cascade evaluates four factors in order: importance → origin → specificity → source order.
Table 3. — Cascade Rules & Specificity
Rule Description Example
Importance Declarations marked !important override all other rules at the same origin. Use sparingly — hard to override. p { color: blue !important; }
Origin Styles come from browser defaults, user styles, or author (your) styles. Author styles normally override browser defaults. body { margin: 0; } overrides browser default margin
Specificity Each selector has a weight score: inline styles (1000) > ID (100) > class/attribute/pseudo-class (10) > type/pseudo-element (1). Higher specificity wins. #id {} beats .class {};
div.note {} beats div {}
Source Order When specificity and importance are equal, the later rule in the CSS source wins. p { color: red; } then p { color: blue; } → blue
Cascade The combined process (importance → origin → specificity → order) that determines the final applied style. Think: "last resort tiebreaker is order"
Inheritance Properties like font and color propagate from parent to child. Layout properties like margin and border do not inherit. body { color: green; } → all child text is green unless overridden

7.4 Units of Measure

CSS sizes and positions are expressed in many units. Absolute units (px) are device-pixel-independent. Relative units (em, rem, %) scale with context. Viewport units (vw, vh, dvh) adapt to the browser window size.
Table 4. — CSS Units of Measure
Unit Category Description / Example
Absolute lengths
pxAbsoluteCSS reference pixel (device-independent). width: 200px;
pt, pcAbsolutePoints (1/72 in), picas (12 pt). font-size: 12pt;
Relative lengths
emRelative to elementMultiple of element's own font-size. padding: 2em;
remRelative to rootMultiple of the root (html) font-size. font-size: 1.5rem;
chRelative to fontWidth of the "0" glyph in current font. width: 40ch;
%RelativePercentage of the parent's value for the same property. width: 50%;
Viewport units
vw, vhViewport1% of viewport width/height. height: 100vh;
vmin, vmaxViewport1% of smaller/larger viewport dimension. font-size: 5vmin;
dvh, dvwDynamic viewport1% of current viewport — adjusts as browser UI (mobile toolbar) appears or disappears. min-height: 100dvh;
lvh, lvwLarge viewport1% of the largest possible viewport (mobile toolbar hidden). header { height: 100lvh; }
svh, svwSmall viewport1% of the smallest possible viewport (mobile toolbar shown). aside { height: 100svh; }
Angle & time
deg, rad, turnAngleDegrees, radians, full rotations. transform: rotate(45deg);
s, msTimeSeconds / milliseconds. transition: all 300ms;
Functional
calc()MathMixed-unit arithmetic. width: calc(100% - 2rem);
min(), max()ConstraintChoose the smaller/larger of given values. width: min(100%, 60rem);
clamp()RangeConstrain to a min–max range. font-size: clamp(1rem, 2vw, 2rem);

7.5 References & Tutorials

Reference Description
CSS Reference — MDN Authoritative property-by-property reference with examples and browser support notes.
CSS Reference — w3schools Concise reference with interactive try-it examples.
cssreference.io Visual reference — each property shown with live rendering demos.
Intro to CSS — MDN Guided introduction covering fundamentals through layout.
CSS Tutorial — w3schools Interactive beginner-to-intermediate tutorials with live examples.
Cascade & Inheritance — MDN Deep dive into how style conflicts are resolved.
Selectors Specification (CSSWG) Living draft for CSS selectors module.
CSS Specifications — W3C Index of all CSS module specifications from the W3C.
Units of Measure — W3C Authoritative reference for CSS length and measurement units.
Timeless Web Dev Articles — CSS-Tricks Curated collection of essential CSS articles.
CSS Reference — Codrops Well-illustrated property reference with live demos.