CSS Cheat Sheet
6 groups · 21 topics · Updated
CSS controls how a page looks. The content itself is written in HTML.
Basics
How rules are written and which one applies.
How a rule works
h1 {
color: navy;
font-size: 32px;
}| Part | Meaning |
|---|---|
h1 |
Selector: what to style |
color |
Property: what to change |
navy |
Value: what to change it to |
Adding CSS to a page
<!-- 1. a separate file: best -->
<link rel="stylesheet" href="styles.css" />
<!-- 2. inside the page -->
<style>
p { color: gray; }
</style>
<!-- 3. on one element: avoid -->
<p style="color: gray;">Hello</p>Selectors
| Selector | Selects |
|---|---|
p |
Every <p> |
.card |
Every class="card" |
#header |
The one id="header" |
* |
Everything |
h1, h2 |
Both <h1> and <h2> |
nav a |
<a> anywhere inside <nav> |
ul > li |
<li> directly inside <ul> |
h2 + p |
<p> right after an <h2> |
p.note |
<p> with class="note" |
[type="email"] |
Elements with that attribute |
States and positions
| Selector | Selects |
|---|---|
a:hover |
Link under the mouse |
input:focus |
Input being typed in |
:disabled |
Switched-off control |
:checked |
Ticked checkbox or radio |
li:first-child |
First item |
li:last-child |
Last item |
li:nth-child(2) |
Second item |
li:nth-child(odd) |
Every other item |
p:not(.intro) |
All except .intro |
::before, ::after |
Added content |
Which rule wins
p { color: black; } /* weakest */
.note { color: blue; } /* stronger */
#intro { color: green; } /* strongest */!importantbeats everything. Avoid it.- The more specific selector wins: tag, then class, then ID, then a
styleattribute. - If equal, the one written later wins.
Text properties such as color and font-family pass down to children. Box properties such as margin do not.
Boxes and sizes
How much space an element takes.
The box model
.card {
width: 300px;
padding: 16px; /* space inside */
border: 1px solid #ccc; /* the edge */
margin: 24px; /* space outside */
}
margin: 10px; /* all sides */
margin: 10px 20px; /* up-down, sides */
margin: 1px 2px 3px 4px; /* clockwise */
margin: 0 auto; /* centre a block */
/* make width include padding and border */
* { box-sizing: border-box; }Units
| Unit | Relative to |
|---|---|
px |
Nothing: a fixed size |
% |
The parent element |
rem |
Root font size (16px). Best default. |
em |
The element's own font size |
vw, vh |
1% of screen width or height |
fr |
A share of free grid space |
ch |
Width of the character 0 |
1rem = 16px, 1.5rem = 24px, 0.5rem = 8px.
Size and overflow
max-width: 800px; /* never wider */
min-height: 100vh; /* at least 1 screen */
overflow: hidden; /* cut off extra */
overflow: auto; /* scroll if needed */
aspect-ratio: 16 / 9; /* keep the shape */
object-fit: cover; /* image fills box */
/* images never overflow */
img { max-width: 100%; height: auto; }Display
| Value | Behaviour |
|---|---|
block |
New line, full width |
inline |
In the text line; no width or height |
inline-block |
In the line, with width and height |
none |
Removed from the page |
flex |
Children in a row or column |
grid |
Children in rows and columns |
visibility: hidden hides it but keeps the space.
Colour and text
How things look.
Colours
color: red; /* name */
color: #ff0000; /* hex */
color: #f00; /* short hex */
color: rgb(255 0 0); /* r, g, b */
color: rgb(255 0 0 / 50%); /* 50% opacity */
color: hsl(0 100% 50%); /* h, s, l */
background-color: #f5f5f5; /* fill */
opacity: 0.8; /* fade it all */Text
font-family: Arial, sans-serif;
font-size: 1.25rem;
font-weight: 700; /* 400 is normal */
font-style: italic;
line-height: 1.6; /* line spacing */
text-align: center;
text-decoration: none; /* no underline */
text-transform: uppercase;
letter-spacing: 0.05em;
/* cut long text off with ... */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;Backgrounds and borders
background-image: url("photo.jpg");
background-size: cover; /* fill the box */
background-position: center;
background-repeat: no-repeat;
background: linear-gradient(
to right, #06b6d4, #3b82f6);
border: 1px solid #ddd;
border-radius: 8px; /* 50% = circle */
box-shadow: 0 2px 8px rgb(0 0 0 / 15%);Layout
Putting elements where you want them.
Flexbox: the parent
Lays children out in one direction.
.row {
display: flex;
flex-direction: row; /* or column */
justify-content: center; /* along */
align-items: center; /* across */
flex-wrap: wrap; /* may wrap */
gap: 16px; /* spacing */
}| Property | Values |
|---|---|
justify-content |
flex-start, center, flex-end, space-between, space-around |
align-items |
stretch, flex-start, center, flex-end |
Flexbox: the items
.item {
flex: 1; /* fill free space */
flex-shrink: 0; /* never squeeze */
flex-basis: 200px; /* starting size */
align-self: flex-end; /* just this one */
order: -1; /* move earlier */
margin-left: auto; /* push right */
}Grid
Lays children out in rows and columns.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
/* fixed sidebar + flexible main */
grid-template-columns: 200px 1fr;
/* responsive, no media query */
grid-template-columns:
repeat(auto-fit, minmax(200px, 1fr));
/* on an item */
grid-column: span 2; /* two columns wide */
grid-column: 1 / -1; /* full width */
place-items: center; /* centre in cells */Flexbox for one line of things. Grid for a whole layout.
Position
| Value | Where it sits |
|---|---|
static |
Normal place (the default) |
relative |
Nudged from its normal place |
absolute |
Inside the nearest positioned parent |
fixed |
Stuck to the browser window |
sticky |
Normal, then sticks on scroll |
.card { position: relative; }
.badge {
position: absolute;
top: 8px;
right: 8px;
z-index: 10; /* higher is on top */
}Centring
/* a block, left to right */
.box { max-width: 800px; margin: 0 auto; }
/* text */
.title { text-align: center; }
/* anything, both ways */
.parent {
display: flex;
justify-content: center;
align-items: center;
}
/* the same with grid */
.parent {
display: grid;
place-items: center;
}Responsive and motion
Adapting to screens and adding movement.
Responsive design
/* phones first */
.layout { grid-template-columns: 1fr; }
/* then wider screens */
@media (min-width: 768px) {
.layout { grid-template-columns: 1fr 1fr; }
}
@media (prefers-color-scheme: dark) {
body { background: #111; color: #eee; }
}
/* smallest, preferred, largest */
h1 { font-size: clamp(1.5rem, 4vw, 3rem); }Common widths: 640px phones, 768px tablets, 1024px laptops, 1280px desktops.
Variables
:root {
--brand: #047857;
--space: 16px;
}
.button {
background: var(--brand);
padding: var(--space);
}Change the value once and every use updates.
Transitions and animation
.button {
transition: transform 0.2s ease;
}
.button:hover {
transform: scale(1.05);
}
transform: translateX(20px); /* move */
transform: rotate(45deg); /* turn */
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
.modal { animation: fade-in 0.3s ease; }Watch out
The slips that catch everyone.
Common mistakes
| Mistake | Fix |
|---|---|
Missing ; |
End every line with ; |
card instead of .card |
. for class, # for ID |
| Width on an inline element | Use inline-block |
| Padding makes boxes too wide | box-sizing: border-box |
absolute jumps to the page corner |
position: relative on the parent |
z-index does nothing |
Give the element a position |
| Vertical margins look merged | Use gap or padding |
Reaching for !important |
Use a more specific selector |
| Fixed pixel widths | Use max-width, %, rem, fr |