CSS selectors & units cheat sheet
Selectors
-
TagSelect all the tags of the same type.
html {} h1 {} p {}
-
.— ClassSelect an element that has a class.
.masthead {} .nav {} .contact {}There needs to be a matching class in the HTML:
<header class="masthead"> <nav class="nav"> ⋮ </nav> </header>
-
#— IDSelect an element that has the ID.
#heading-1 {} #github {} #top {}There needs to be a matching ID in the HTML:
<a id="top" href="#top">Top</a>
-
Space— DescendantSelect an element that’s a descendant of another element.
ul li {} nav a {} ul li {}
-
>— ChildSelect an element directly inside another element.
ul > li {} h1 > span {} footer > .copyright {}
-
+— Adjacent siblingSelect an element immediately beside another element.
h1 + p {} hr + p {} li + li {}
-
~— General siblingSelect an element that’s at the same level.
p ~ p {} h1 ~ p {} dd ~ dt {}
-
[]— AttributeSelect an element by it’s attribute.
Good for styling links differently if they’re external.
[data-state="active"] {} [href^="http"] {} [download] {}
Pseudo classes
-
:first-childSelect the element when it’s the first inside its parent.
p:first-child {} ul li:first-child {} .person:first-child {}
-
:last-childSelect the element when it’s the last inside its parent.
li:last-child {} p:last-child {} .item:last-child {}
-
:only-childSelect an element when it’s the only thing inside its parent.
li:only-child {}
-
:nth-child()Select an element by it’s number.
Good for zebra-striping table rows.
li:nth-child(2) {} tr:nth-child(odd) {} div:nth-child(5n) {}
-
:nth-last-child()Select an element by it’s number, counting backwards from the end.
/* Third from the bottom */ li:nth-last-child(3) {}
-
:nth-of-type()Select an element by it’s number, but only counting others that match—not all children.
/* Second <p> element in its parent */ p:nth-of-type(2) {}
-
:nth-last-of-type()Select an element by it’s number, counting backwards from the end.
/* Second <p> from the bottom */ p:nth-last-of-type(2) {}
-
:first-of-typeSelect an element that’s the first of its kind within its parent.
p:first-of-type {}
-
:last-of-typeSelect an element that’s the last of its kind within its parent.
p:last-of-type {}
-
:only-of-typeSelect an element when it’s the only child of its parent of a specific kind.
p:only-of-type {} section div:only-of-type {}
-
:emptySelect an element it has no children.
ul:empty {}
-
:disabledSelect an element when its
disabledattribute is set.button:disabled {}
-
:checkedSelect an
<input>when itscheckedattribute is set.input:checked {}
-
:targetSelect an element when the URL matches its ID.
li:target {}
-
:not()Select matching elements that do not match the selection inside the
()p:not(.mammal) {} input:not(:checked) {}
Pseudo elements
-
::beforeA hidden element before the content of most elements.
blockquote::before { content: "“"; font-size: 5rem; }
-
::afterA hidden element after the content of most elements.
blockquote::after { content: "”"; font-size: 5rem; }
-
::first-lineSelect the first line of text.
Good for highlighting the first line of a paragraph.
p::first-line {}
-
::first-letterSelect the first character in the text.
Good for drop caps.
p::first-letter {}
-
::selectionStyle an element when it has been selected and highlighted.
::selection { color: red; }
Interaction selectors
-
:linkFor styling a link that hasn’t been visited.
a:link { color: #4484c2; }
-
:visitedFor styling a link that has been visited.
a:visited { color: #ccc; }
-
:hoverFor styling an element when the mouse hovers over it.
a:hover { color: #00f; }
-
:focusFor styling an element for when the keyboard focuses it.
Only works on
<a>,<button>, and form inputs by default.button:focus { outline: 3px solid #000; outline-offset: 2px; }
-
:activeFor styling an element when the mouse button is clicked down on it.
a:active { color: #f33; }
Attribute selectors
-
Has an attribute
Select when an element has a specific attribute.
[download] {}
-
Exact match
Select when an attribute that’s exactly the same.
[rel="external"] {} [data-state="visible"] {}
-
Starts with
Select when an attribute starts with some text.
[href^="http://"] {}
-
Ends with
Select when an attribute ends with some text.
[src$=".jpg"] {}
-
Contains
Select when an attribute contains some text anywhere.
[class*="unit"] {}
-
Contains when separated by spaces
Select an element when its attribute matches one item from a space separated list.
<p class="unit xs-1 s-1 m-1"></p>[class~="s-1"] {}
-
Contains when separated by dashes
Select an element when its attribute matches one item from a dash separated list.
<p class="super-duper-long-class-name"></p>[class|="duper"] {}
-
Case insensitive
Allows the search to ignore upper vs. lower case letters.
[lang="en-ca" i] {}
Colours
-
Keywords
Standardized, named colours. Color keywords.
background-color: red; color: darkorange; border-color: hotpink;
-
Hexadecimal
Hex colours start with a hash:
#.Three separate numbers: red, green, blue.
background-color: #000000; color: #ff3333; border-color: #b95f4; outline-color: darkorange;Simplify pairs:
#000,#fff,#f33background-color: #000; border-color: #fff; color: #f22;
-
RGB
Specify colours using red, green & blue numbers.
background-color: rgb(255, 255, 255); color: rgb(255, 0, 0); border-color: rgb(124, 65, 99);
-
RGBA
RGB with semi-transparent/opacity.
background-color: rgba(0, 0, 0, .5); color: rgba(255, 0, 0, .75); border-color: rgba(124, 65, 99, .8);
-
HSL
Specify colours using the hue, saturation, lightness system.
Different from Photoshop’s HSB system.
background-color: hsl(0, 100%, 100%); color: hsl(53, 100%, 50%); border-color: hsl(167, 38%, 59%);
-
HSLA
HSL with semi-transparent/opacity.
background-color: hsla(0, 100%, 100%, .5); color: hsla(53, 100%, 50%, .7); border-color: hsla(167, 38%, 59%, .3);
-
Transparency
The
transparentkeyword can be used to remove a colour.background-color: transparent;
-
Current colour
The
currentColorkeyword can be used to capture thecolorof the same element.color: red; border-color: currentColor; /* Will be red */
Units
-
pxCSS pixels—different sizes for every device.
100pxis exactly 100 pixels in all situations.
-
emBased on the font-size of the parent (or current element).
1emis 1 × the parent element’s size.0.5emis 0.5 × the parent element’s size.
-
remBased on the font-size set in the
htmlelement.1.5remis 1.5 × thehtmlelement’s font size.
-
%Percentage
A percentage of the parent element.
100%is to whole width of the parent element.If the parent element is
50%wide, and this element is50%wide, then it only takes up25%of the original grand parent element.
-
vhViewport height
Like percentage, but based on the height of the window.
100vhis the whole height of the window.50vhis half the height of the window.
-
vwViewport width
Like percentage, but based on the width of the window.
75vwis three-quarters the width of the window.
Unit rules
-
Use
remforfont-sizeAlways use
remfor font sizes because it’s easier to manage.Never—ever—user
pxfor font sizes.
-
Use
remoremfor paddings and marginsThe idea being that we want the margins and paddings to increase when the font size increases.
Most often I use
emforpaddingandremformargin
-
Use
%for widthsMost often use
%for widths because we want most things to be flexible.Sometimes using
pxoremfor widths is okay too.
-
Use
emfor maximum widthsMaximum widths required a fixed measurement—so
emformax-widthworks because we want themax-widthto increase as the font size increases.
-
Use
pxfor borders and accuracyUse pixels for accuracy—when things should always be the same size: logos as an example.
Or use pixels for borders—though sometimes
emforborderis cool too.Never—ever—user
pxfor font sizes.