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>