THE REFERENCE ROOM

CSS, with purpose.

A working reference for the properties that shape a page. Read the highlighted code, edit the values and run your own variation.

CSS basics

Selectors, properties and values

A selector chooses elements. Each declaration pairs a property with a value inside braces.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
<p class="note" id="special">A more specific rule.</p>
Edit HTML
h1 {
  color: #FF1493;
  font-size: 40px;
}
Edit CSS

Try this → Change the selector or value, then run the preview.

Comments and external stylesheets

Comments are ignored by the browser. Keep CSS in a separate file and link it from the HTML head.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
<p class="note" id="special">A more specific rule.</p>
Edit HTML
/* A display heading */
h1 {
  font-weight: 700;
}
Edit CSS

Try this → Change the selector or value, then run the preview.

Imports

@import loads another stylesheet and must come before ordinary rules. This self-contained demo imports a small data stylesheet; in your project use @import url("reset.css").

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
<p class="note" id="special">A more specific rule.</p>
Edit HTML
@import url("data:text/css,h1%7Bcolor%3A%23ff1493%7D");
p {
  line-height: 1.6;
}
Edit CSS

Try this → Change the selector or value, then run the preview.

Cascade and specificity

For ordinary author rules in the same layer, an ID is more specific than a class, and a class is more specific than an element. Equal specificity falls back to source order.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
<p class="note" id="special">A more specific rule.</p>
Edit HTML
p { color: black; }
p { color: #FF1493; }
.note { color: green; }
#special { color: blue; }
Edit CSS

Try this → Change the selector or value, then run the preview.

Inheritance

Some properties, such as color and font-family, pass from parent to child. Margins and borders do not normally inherit.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
<p class="note" id="special">A more specific rule.</p>
Edit HTML
body {
  color: #FF1493;
  font-family: Georgia, serif;
}
p {
  color: black;
}
Edit CSS

Try this → Change the selector or value, then run the preview.

CSS units

px

A CSS pixel is a fixed CSS measurement, useful for small details.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
h1 { font-size: 40px; }
Edit CSS

Try this → Double the value and resize the preview/browser.

%

Percentages depend on a reference size, often the parent width.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p { width: 70%; background: #7FFF00; }
Edit CSS

Try this → Double the value and resize the preview/browser.

em

For font-size, em is relative to the parent font size. For padding, it follows the element’s own font size.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
h1 { font-size: 2em; padding: .5em; }
Edit CSS

Try this → Double the value and resize the preview/browser.

rem

rem follows the root font size, making a consistent document scale.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
h1 { font-size: 3rem; }
Edit CSS

Try this → Double the value and resize the preview/browser.

vw

One vw is one percent of viewport width. In these previews the iframe is the viewport.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
h1 { font-size: 8vw; }
Edit CSS

Try this → Double the value and resize the preview/browser.

vh

One vh is one percent of viewport height.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p { min-height: 40vh; background: #7FFF00; }
Edit CSS

Try this → Double the value and resize the preview/browser.

ch

One ch approximates the width of the zero glyph, useful for controlling line length.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p { max-width: 30ch; }
Edit CSS

Try this → Double the value and resize the preview/browser.

Typography

font-family

Choose a typeface and fallbacks.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  font-family: Georgia, "Times New Roman", serif;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

font-size

Set the size of the letters.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  font-size: 32px;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

font-weight

400 is normal; 700 is bold. The font must provide the requested weight.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  font-weight: 700;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

font-style

Use italic for a different emphasis.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  font-style: italic;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

line-height

A unitless value multiplies the element’s font size.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  line-height: 1.8;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

letter-spacing

Positive values open tracking; negative values tighten it.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  letter-spacing: 2px;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

word-spacing

Adjust gaps between words without changing gaps inside words.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  word-spacing: 6px;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

text-align

Align text inside its box. Try left, center, right or justify.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  text-align: center;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

text-transform

Change visual case without changing the source text.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  text-transform: uppercase;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

text-decoration

Underline, overline or strike through text.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  text-decoration: underline;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

text-shadow

Values are horizontal offset, vertical offset, blur and colour.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  text-shadow: 3px 3px 0 #7FFF00;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

text-indent

Indent the first line of a paragraph.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  text-indent: 2em;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

-webkit-text-stroke

Add an outline as an enhancement; keep a readable fill as a fallback.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  -webkit-text-stroke: 1px #FF1493;
}
Edit CSS

Try this → Change this property and compare the paragraph with the heading.

Colours

Hexadecimal

Six hex digits describe red, green and blue.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  color: #FF1493;
}
Edit CSS

Try this → Change the colour or transparency; keep the text legible.

RGB

Each RGB channel ranges from 0 to 255.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  color: rgb(255, 20, 147);
}
Edit CSS

Try this → Change the colour or transparency; keep the text legible.

RGBA

Alpha controls colour transparency; 0 is transparent and 1 opaque.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  background-color: rgba(127, 255, 0, .4);
}
Edit CSS

Try this → Change the colour or transparency; keep the text legible.

HSL

Hue is an angle; saturation and lightness are percentages.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  color: hsl(328, 100%, 54%);
}
Edit CSS

Try this → Change the colour or transparency; keep the text legible.

Background colour

The background fills the content and padding area.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  background-color: #7FFF00;
  color: #000000;
}
Edit CSS

Try this → Change the colour or transparency; keep the text legible.

Opacity

Opacity affects the whole element, including its text and children.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  opacity: .5;
}
Edit CSS

Try this → Change the colour or transparency; keep the text legible.

Custom properties

Define named values on :root and read them with var().

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
:root {
  --accent: #FF1493;
}
h1, p {
  color: var(--accent);
}
Edit CSS

Try this → Change --accent and watch both elements update.

Spacing and boxes

margin

Space outside the box.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  background: #7FFF00;
  margin: 20px;
}
Edit CSS

Try this → Increase the value and compare the space inside and outside the box.

padding

Space inside the box.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  background: #7FFF00;
  padding: 24px;
}
Edit CSS

Try this → Increase the value and compare the space inside and outside the box.

width

The preferred width of the content box unless border-box is used.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  background: #7FFF00;
  width: 240px;
}
Edit CSS

Try this → Increase the value and compare the space inside and outside the box.

max-width

A ceiling that still allows the box to shrink.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  background: #7FFF00;
  max-width: 28ch;
}
Edit CSS

Try this → Increase the value and compare the space inside and outside the box.

min-width

A minimum width can cause overflow when the viewport is narrower.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  background: #7FFF00;
  min-width: 260px;
}
Edit CSS

Try this → Increase the value and compare the space inside and outside the box.

box-sizing

border-box includes padding and borders in the specified width.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
p {
  background: #7FFF00;
  box-sizing: border-box;
}
Edit CSS

Try this → Increase the value and compare the space inside and outside the box.

Layout

Display: block and inline

Blocks start new lines; inline boxes flow with text.

<div class="layout">
  <div>HTML</div>
  <div>CSS</div>
  <div>TYPE</div>
</div>
Edit HTML
.layout div {
  display: inline;
  background: #7FFF00;
}
Edit CSS

Try this → Change the gap, track count or box size.

Flexbox

Flexbox arranges content along one main axis. Wrap handles extra items.

<div class="layout">
  <div>HTML</div>
  <div>CSS</div>
  <div>TYPE</div>
</div>
Edit HTML
.layout {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  justify-content: space-between;
}
Edit CSS

Try this → Change the gap, track count or box size.

CSS Grid

Grid defines rows and columns. minmax(0, 1fr) permits shrinking tracks.

<div class="layout">
  <div>HTML</div>
  <div>CSS</div>
  <div>TYPE</div>
</div>
Edit HTML
.layout {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}
Edit CSS

Try this → Change the gap, track count or box size.

Positioning and z-index

Absolute positioning uses the nearest positioned ancestor. z-index controls stacking within stacking contexts.

<div class="layout">
  <div>HTML</div>
  <div>CSS</div>
  <div>TYPE</div>
</div>
Edit HTML
.layout { position: relative; height: 140px; }
.layout div { position: absolute; padding: 20px; background: #7FFF00; }
.layout div:nth-child(2) { left: 50px; top: 30px; z-index: 2; background: #FF1493; }
Edit CSS

Try this → Change the gap, track count or box size.

Overflow

Overflow controls content extending beyond a box. auto provides scrollbars when needed.

<div class="layout">
  <div>HTML</div>
  <div>CSS</div>
  <div>TYPE</div>
</div>
Edit HTML
.layout {
  width: 150px;
  height: 90px;
  overflow: auto;
}
.layout div { padding: 30px; background: #7FFF00; }
Edit CSS

Try this → Change the gap, track count or box size.

Responsive CSS

Mobile-first and fluid type

Start with narrow-screen rules, then add a min-width media query. clamp() sets a minimum, preferred value and maximum.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
h1 {
  font-size: clamp(2rem, 6vw, 5rem);
}
p {
  max-width: 60ch;
}
@media (min-width: 600px) {
  body { padding: 40px; }
}
Edit CSS

Try this → Resize the browser; the iframe has its own viewport width. Change 600px to 400px.

Animation

Transitions and keyframes

A transition connects states; keyframes define a sequence. The paragraph has a 1s duration, .2s delay and ease-out timing.

<h1>Down the Rabbit-Hole</h1>
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.</p>
Edit HTML
h1 {
  transition: color .3s ease, transform .3s ease;
}
h1:hover {
  color: #FF1493;
  transform: rotate(-3deg) scale(1.05);
}
p {
  animation: enter 1s ease-out .2s both;
}
@keyframes enter {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  h1 { transition: none; }
  p { animation: none; }
}
Edit CSS

Try this → Hover over the title or rerun the preview. Change the delay.