Guide 02 / 4
CSS essentials
An annotated cheat sheet: what each element is for, how to write it and when to use it.
Choose and use the essential elements to complete this step of the blog.
01 · Linking, selecting and sizing
A stylesheet is written in a .css text file.
link and external stylesheet
Create style.css next to index.html, then add link in head. The path is relative to the HTML document.
Example
<link rel="stylesheet" href="style.css">
Classes and descendants
A reusable class avoids targeting each recipe separately. A space between two selectors means “descendant of”.
Example
.recette { border-radius: 0.75rem; }
.recette h3 { color: #8b351b; }
box-sizing: border-box
border-box includes padding and border in the defined width. margin stays outside. Applying this rule to all boxes simplifies dimensions.
Example
*, *::before, *::after { box-sizing: border-box; }
margin, padding and gap
margin separates a box from its neighbours; padding moves the content away from its border; gap spaces the children of a grid or flex container.
Example
.recette { padding: 1.25rem; }
.recettes { display: grid; gap: 1.25rem; }
section { margin-block: 2rem; }
Fluid width and centring
Limiting the width keeps lines readable. min() picks the smallest value; margin-inline: auto shares the available side margins.
Example
.conteneur {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}
02 · Building a readable presentation
Colour and spacing serve the reading of the recipes.
Fonts and line height
Using the system font limits downloads. A unitless line height adapts to the text size.
Example
body {
font-family: system-ui, sans-serif;
line-height: 1.6;
}
h1, h2, h3, h4 { line-height: 1.25; }
Colours and custom properties
Defining a few values at the root gives a palette that is easy to change. Colour must not be the only way to convey information.
Example
:root { --fond: #fff8ed; --texte: #29251f; --accent: #8b351b; }
body { background: var(--fond); color: var(--texte); }
a { color: var(--accent); }
Keep link underlines and check the contrast when changing the palette.
Flexible images
An image keeps its ratio if its height adjusts automatically. max-width limits its width to that of its container.
Example
img { display: block; max-width: 100%; height: auto; }
figure { margin: 0; }
Mobile grid then three columns
1fr takes up the available space. repeat creates three columns; minmax(0, 1fr) allows them to shrink without imposing the content's minimum width.
Example
.recettes { display: grid; grid-template-columns: 1fr; gap: 1.25rem; }
@media (min-width: 48rem) {
.recettes { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
Visible focus and skip link
Keyboard users must see where they are. The Skip to content link from the HTML course can be hidden off-screen, then appear on focus.
Example
:focus-visible { outline: 3px solid #185ea0; outline-offset: 4px; }
.evitement { position: absolute; transform: translateY(-120%); }
.evitement:focus { transform: translateY(0); }
Do not remove outline without providing an equally noticeable focus indicator.