Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14
Chapter 3

Custom Properties, Lists & Spacing

CSS variables (design tokens), list styling and the classic spacing properties.


CSS Custom Properties (Variables)

  • Defined with a -- prefix in :root { } for global scope.
  • Used with var(--name, fallback) throughout the stylesheet.
  • Cascade and inherit like regular CSS properties, and can be changed per-component or per-theme.
  • Enable design tokens — a single source of truth for spacing and colour.
  • Fully dynamic — can be updated with JavaScript at runtime.
/* Design tokens in :root */
:root {
  --color-primary: #00B4D8;
  --color-bg: #001B2A;
  --color-text: #CAD7EE;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --radius: 0.5rem;
}

/* Use variables everywhere */
.button {
  background: var(--color-primary);
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius);
}

/* Dark mode override — change one place */
@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #060D14;
    --color-text: #E8F4FD;
  }
}

List Properties

  • list-style-type — bullet or numbering style. Unordered: disc (default), circle, square, none. Ordered: decimal, upper-roman, lower-alpha, upper-alpha.
  • list-style-image — use a custom image as the bullet.
  • list-style — shorthand combining type, position and image.
  • 2025: the ::marker pseudo-element styles bullets directly.
/* Unordered list bullets */
ul { list-style-type: square; }

/* Custom image bullet */
ul.special { list-style-image: url('dot.svg'); }

/* Ordered list types */
ol { list-style-type: upper-roman; }

/* Per-item override */
li.special { list-style-type: disc; }

/* 2025: style the bullet itself */
li::marker {
  color: #FFB703;
  font-size: 1.2em;
  font-weight: bold;
  content: "> ";
}

/* List shorthand */
ul { list-style: square inside url('img.png'); }

Text Alignment, Float & Margins

PropertyPurpose
text-alignleft | center | right | justify
text-indentIndents the first line of text
floatWraps text around an element (left | right | none)
marginSpace outside the element border
paddingSpace between the border and the content
/* Text alignment */
p { text-align: justify; }
h1 { text-align: center; }
.note { text-indent: 2rem; }

/* Float (legacy — for text wrapping around images) */
img { float: right; margin-left: 1rem; margin-bottom: 1rem; }

/* Clear floats */
.clearfix::after { content: ""; display: block; clear: both; }

/* Margin shorthand: top right bottom left */
.box { margin: 1rem 2rem; }            /* top/bottom 1rem, left/right 2rem */
.box { margin-inline: auto; }          /* centre block horizontally (2025) */
.box { padding-block: 1.5rem; }        /* top + bottom padding */

2025: modern layout uses Flexbox and Grid — float is rarely needed now.

Activity — complete the code

Complete the code correctly to mark this lesson as complete automatically.

Complete the custom property usage and list styling.

:root {
--primary: #2563eb;
}
 
.button {
background: (--primary);
}
 
ul {
list-style-type: ;
}