CSS Selectors & Units Cheat Sheet Web Dev Topics Learn The Web
CSS Selectors & Units Cheat Sheet Web Dev Topics Learn The Web
https://learn-the-web.algonquindesign.ca/topics/css-selectors-units-cheat-sheet/ 1/8
1/5/2020 CSS selectors & units cheat sheet · Web Dev Topics · Learn the Web
Selectors
Tag . — Class # — ID
Select all the tags of the same type. Select an element that has a class. Select an element that has the ID.
html {} .masthead {} #heading-1 {}
h1 {} .nav {} #github {}
p {} .contact {} #top {}
There needs to be a matching class in the There needs to be a matching ID in the HTML:
HTML: <a id="top" href="#top">Top</a>
<header class="masthead">
<nav class="nav">
⋮
</nav>
</header>
https://learn-the-web.algonquindesign.ca/topics/css-selectors-units-cheat-sheet/ 2/8
1/5/2020 CSS selectors & units cheat sheet · Web Dev Topics · Learn the Web
Pseudo classes
https://learn-the-web.algonquindesign.ca/topics/css-selectors-units-cheat-sheet/ 3/8
1/5/2020 CSS selectors & units cheat sheet · Web Dev Topics · Learn the Web
Pseudo elements
Learn more about ::before. (/topics/before- Learn more about ::after. (/topics/before-after/)
after/)
::first-letter ::selection
Select the first character in the text. Style an element when it has been selected and
Good for drop caps. highlighted.
p::first-letter {} ::selection {
color: red;
}
https://learn-the-web.algonquindesign.ca/topics/css-selectors-units-cheat-sheet/ 4/8
1/5/2020 CSS selectors & units cheat sheet · Web Dev Topics · Learn the Web
Interaction selectors
:focus :active
For styling an element for when the keyboard For styling an element when the mouse button
focuses it. is clicked down on it.
Only works on <a> , <button> , and form a:active {
inputs by default. color: #f33;
}
button:focus {
outline: 3px solid #000;
outline-offset: 2px;
}
https://learn-the-web.algonquindesign.ca/topics/css-selectors-units-cheat-sheet/ 5/8
1/5/2020 CSS selectors & units cheat sheet · Web Dev Topics · Learn the Web
Attribute selectors
[class~="s-1"] {}
[class|="duper"] {}
https://learn-the-web.algonquindesign.ca/topics/css-selectors-units-cheat-sheet/ 6/8
1/5/2020 CSS selectors & units cheat sheet · Web Dev Topics · Learn the Web
Colours
https://learn-the-web.algonquindesign.ca/topics/css-selectors-units-cheat-sheet/ 7/8
1/5/2020 CSS selectors & units cheat sheet · Web Dev Topics · Learn the Web
Units
px em rem
CSS pixels—different sizes for every device. Based on the font-size of the parent (or current Based on the font-size set in the html
100px is exactly 100 pixels in all situations. element). element.
1em is 1 × the parent element's size. 1.5rem is 1.5 × the html element's font
0.5em is 0.5 × the parent element's size. size.
% vh vw
Percentage Viewport height Viewport width
A percentage of the parent element. Like percentage, but based on the height of the Like percentage, but based on the width of the
100% is to whole width of the parent window. window.
element. 100vh is the whole height of the window. 75vw is three-quarters the width of the
If the parent element is 50% wide, and this 50vh is half the height of the window. window.
element is 50% wide, then it only takes up
25% of the original grand parent element.
Unit rules
Use rem for font-size Use rem or em for paddings and margins Use % for widths
Always use rem for font sizes because it's The idea being that we want the margins and Most often use % for widths because we want
easier to manage. paddings to increase when the font size most things to be flexible.
Never—ever—user px for font sizes. increases. Sometimes using px or em for widths is
Most often I use em for padding and okay too.
rem for margin
https://learn-the-web.algonquindesign.ca/topics/css-selectors-units-cheat-sheet/ 8/8