CSS Cascading Style Sheets
Sélecteurs, Box Model, Flexbox, Grid, animations et tout ce qu'il faut pour styliser vos pages.
Sélecteurs
Sélecteurs de base
| Sélecteur | Exemple | Cible |
|---|---|---|
* | * { box-sizing: border-box; } | Tous les éléments |
élément | p { color: red; } | Tous les <p> |
.classe | .btn { padding: 8px; } | Éléments avec class="btn" |
#id | #header { height: 60px; } | Élément avec id="header" |
el1, el2 | h1, h2 { font-weight: 700; } | h1 ET h2 (liste) |
el1 el2 | nav a { color: white; } | Tous les <a> descendants de <nav> |
el1 > el2 | ul > li { list-style: none; } | Enfants directs uniquement |
el1 + el2 | h2 + p { margin-top: 0; } | Frère adjacent immédiat |
el1 ~ el2 | h2 ~ p { color: gray; } | Tous les frères suivants |
[attr] | [required] { border: red; } | Éléments avec cet attribut |
[attr="val"] | [type="email"] { … } | Attribut égal à val |
[attr^="val"] | [href^="https"] { … } | Attribut commence par val |
[attr$="val"] | [href$=".pdf"] { … } | Attribut finit par val |
[attr*="val"] | [class*="btn"] { … } | Attribut contient val |
Spécificité CSS
La spécificité détermine quelle règle s'applique en cas de conflit. Score calculé en (inline, id, classes, éléments).
CSS
/* Spécificité : (0,0,0,1) */
p { color: black; }
/* Spécificité : (0,0,1,0) */
.texte { color: blue; }
/* Spécificité : (0,1,0,0) */
#titre { color: green; }
/* Spécificité : (1,0,0,0) — style inline */
<p style="color: red;">
/* !important — écrase tout (à éviter) */
p { color: purple !important; }
Box Model
Margin, Padding, Border, Box-sizing
Chaque élément HTML est une boîte : content → padding → border → margin. Avec box-sizing: border-box, width et height incluent padding et border.
CSS
/* Reset universel (bonne pratique) */
*, *::before, *::after { box-sizing: border-box; }
.box {
width: 300px;
height: 150px;
/* Padding : intérieur (top right bottom left) */
padding: 20px; /* tous côtés */
padding: 10px 20px; /* vertical horizontal */
padding: 10px 20px 15px 5px; /* top right bottom left */
padding-top: 10px;
/* Margin : extérieur */
margin: 0 auto; /* centrage horizontal */
margin: 20px;
margin-top: 10px;
margin: 0 0 0 auto; /* pousse à droite */
/* Border */
border: 2px solid #333;
border-radius: 8px; /* coins arrondis */
border-radius: 50%; /* cercle */
border-top: 3px dashed red;
border: none;
/* Outline (n'affecte pas le box model) */
outline: 2px solid blue;
outline-offset: 4px;
}
Width, Height & Overflow
CSS
.element {
/* Dimensions */
width: 300px;
width: 50%;
width: 100vw; /* viewport width */
min-width: 200px;
max-width: 1200px;
height: 200px;
height: 100vh; /* viewport height */
min-height: 100vh;
max-height: 400px;
/* Overflow */
overflow: visible; /* défaut */
overflow: hidden; /* coupe le contenu */
overflow: scroll; /* scrollbar toujours visible */
overflow: auto; /* scrollbar si nécessaire */
overflow-x: hidden;
overflow-y: scroll;
/* Texte qui déborde */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; /* "Texte trop long..." */
}
Display & Position
Propriété
display| Valeur | Comportement |
|---|---|
block | Prend toute la largeur, saute à la ligne (div, p, h1…) |
inline | Coule dans le texte, width/height ignorées (span, a…) |
inline-block | Inline MAIS accepte width/height |
flex | Active Flexbox sur le conteneur |
inline-flex | Flex mais se comporte comme inline |
grid | Active CSS Grid sur le conteneur |
none | Cache l'élément (pas de place réservée) |
contents | L'élément disparaît, ses enfants restent |
Propriété
position| Valeur | Description |
|---|---|
static | Défaut — flux normal, top/left ignorées |
relative | Décalé par rapport à sa position normale, garde sa place |
absolute | Positionné par rapport au parent positionné le plus proche |
fixed | Positionné par rapport au viewport, reste fixe au scroll |
sticky | Relatif jusqu'au scroll, puis fixe (navbar sticky) |
CSS
/* Centrage absolu classique */
.parent { position: relative; }
.enfant {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
}
/* Navbar fixe */
.navbar {
position: fixed;
top: 0; left: 0; right: 0;
z-index: 100;
}
/* Sidebar sticky */
.sidebar {
position: sticky;
top: 80px; /* colle à 80px du haut au scroll */
}
/* z-index (ne fonctionne qu'avec position != static) */
.modal { position: fixed; z-index: 1000; }
.overlay { position: fixed; z-index: 999; }
.dropdown { position: absolute; z-index: 50; }
Flexbox
Propriétés du conteneur Flex
CSS
.container {
display: flex; /* Active Flexbox */
/* Direction */
flex-direction: row; /* → défaut */
flex-direction: row-reverse; /* ← */
flex-direction: column; /* ↓ */
flex-direction: column-reverse; /* ↑ */
/* Retour à la ligne */
flex-wrap: nowrap; /* défaut — pas de retour */
flex-wrap: wrap; /* retour à la ligne */
flex-wrap: wrap-reverse;
/* Raccourci direction + wrap */
flex-flow: row wrap;
/* Alignement axe principal (horizontal si row) */
justify-content: flex-start; /* défaut */
justify-content: flex-end;
justify-content: center;
justify-content: space-between; /* espace entre les items */
justify-content: space-around; /* espace autour */
justify-content: space-evenly; /* espace égal partout */
/* Alignement axe secondaire (vertical si row) */
align-items: stretch; /* défaut — s'étire */
align-items: flex-start;
align-items: flex-end;
align-items: center;
align-items: baseline;
/* Alignement des lignes (si flex-wrap: wrap) */
align-content: flex-start;
align-content: center;
align-content: space-between;
/* Espacement entre items */
gap: 16px; /* row-gap + column-gap */
gap: 10px 20px; /* row column */
row-gap: 10px;
column-gap: 20px;
}
Propriétés des items Flex
CSS
.item {
/* Taille de base avant distribution de l'espace */
flex-basis: auto; /* défaut */
flex-basis: 200px;
flex-basis: 33.33%;
/* Facteur de croissance (prend l'espace disponible) */
flex-grow: 0; /* défaut — ne grandit pas */
flex-grow: 1; /* grandit proportionnellement */
/* Facteur de rétrécissement */
flex-shrink: 1; /* défaut — peut rétrécir */
flex-shrink: 0; /* ne rétrécit pas */
/* Raccourci : grow shrink basis */
flex: 0 1 auto; /* défaut */
flex: 1; /* flex: 1 1 0 */
flex: auto; /* flex: 1 1 auto */
flex: none; /* flex: 0 0 auto */
/* Alignement individuel (écrase align-items) */
align-self: auto;
align-self: flex-start;
align-self: center;
align-self: flex-end;
align-self: stretch;
/* Ordre d'affichage */
order: 0; /* défaut */
order: -1; /* passe en premier */
order: 2; /* passe en 3ème */
}
/* ===== PATTERNS COURANTS ===== */
/* Centrage parfait */
.center { display: flex; justify-content: center; align-items: center; }
/* Barre de navigation */
.navbar { display: flex; align-items: center; justify-content: space-between; }
/* Colonnes égales */
.cols > * { flex: 1; }
/* Sidebar + content */
.layout { display: flex; }
.sidebar { flex: 0 0 250px; }
.content { flex: 1; }
/* Coller le footer en bas */
body { display: flex; flex-direction: column; min-height: 100vh; }
main { flex: 1; }
CSS Grid
Propriétés du conteneur Grid
CSS
.grid {
display: grid;
/* Définir les colonnes */
grid-template-columns: 200px 1fr 1fr; /* fixe + fractions */
grid-template-columns: repeat(3, 1fr); /* 3 colonnes égales */
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); /* responsive */
grid-template-columns: auto 1fr auto;
/* Définir les lignes */
grid-template-rows: 60px 1fr 80px;
grid-template-rows: repeat(3, 100px);
/* Espacement */
gap: 16px;
row-gap: 20px;
column-gap: 16px;
/* Alignement horizontal des items dans leur cellule */
justify-items: start | end | center | stretch;
/* Alignement vertical des items dans leur cellule */
align-items: start | end | center | stretch;
/* Alignement de la grille dans le conteneur */
justify-content: start | end | center | space-between | space-around;
align-content: start | end | center | space-between;
/* Zones nommées */
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
Propriétés des items Grid + Layout complet
CSS
/* Placement par lignes */
.item {
grid-column: 1 / 3; /* de la ligne 1 à 3 (2 colonnes) */
grid-column: 1 / -1; /* toute la largeur */
grid-column: span 2; /* occupe 2 colonnes */
grid-row: 1 / 3;
grid-row: span 2;
/* Alignement individuel */
justify-self: start | end | center | stretch;
align-self: start | end | center | stretch;
/* Placement par nom de zone */
grid-area: header;
}
/* ===== LAYOUT COMPLET AVEC ZONES ===== */
.page {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: 60px 1fr 50px;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
gap: 0;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
/* ===== GRILLE RESPONSIVE SANS MEDIA QUERY ===== */
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 20px;
}
/* Les cartes s'adaptent automatiquement : 1, 2, 3 colonnes selon la place */
Typographie
Propriétés de texte
CSS
body {
/* Police */
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
font-family: 'Georgia', Times, serif;
font-family: 'SFMono-Regular', Consolas, monospace;
/* Taille */
font-size: 16px; /* base recommandée */
font-size: 1rem; /* relatif à la racine */
font-size: 1.2em; /* relatif au parent */
font-size: clamp(14px, 2vw, 18px); /* responsive */
/* Graisse */
font-weight: 400; /* normal */
font-weight: 700; /* bold */
font-weight: 300; /* light */
/* Style */
font-style: normal | italic | oblique;
/* Interlignage */
line-height: 1.6; /* sans unité = recommandé */
line-height: 24px;
/* Espacement */
letter-spacing: 0.5px; /* entre les lettres */
word-spacing: 2px; /* entre les mots */
/* Alignement */
text-align: left | center | right | justify;
/* Décoration */
text-decoration: none;
text-decoration: underline;
text-decoration: line-through;
text-decoration: underline wavy red;
/* Transformation */
text-transform: uppercase | lowercase | capitalize | none;
/* Indentation */
text-indent: 2em;
/* Ombre de texte */
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
text-shadow: 0 0 10px rgba(88,166,255,0.6); /* glow effect */
/* Variables CSS pour typo responsive */
--fs-sm: clamp(0.8rem, 0.17vw + 0.76rem, 0.89rem);
--fs-base: clamp(1rem, 0.34vw + 0.91rem, 1.19rem);
--fs-lg: clamp(1.2rem, 0.61vw + 1.1rem, 1.58rem);
--fs-xl: clamp(1.56rem, 1vw + 1.31rem, 2.11rem);
--fs-2xl: clamp(1.95rem, 1.56vw + 1.56rem, 2.81rem);
}
Couleurs & Fonds
Formats de couleurs
CSS
.element {
/* Nommées */
color: red; color: transparent; color: currentColor;
/* Hexadécimal */
color: #ff0000; /* rouge */
color: #f00; /* raccourci */
color: #ff000080; /* avec transparence (8 chiffres) */
/* RGB / RGBA */
color: rgb(255, 0, 0);
color: rgba(255, 0, 0, 0.5);
color: rgb(255 0 0 / 50%); /* syntaxe moderne */
/* HSL / HSLA (hue, saturation, lightness) */
color: hsl(0, 100%, 50%); /* rouge */
color: hsl(200, 80%, 60%); /* bleu */
color: hsla(200, 80%, 60%, 0.8);
color: hsl(200 80% 60% / 80%); /* syntaxe moderne */
}
Propriété
backgroundCSS
.element {
/* Couleur */
background-color: #1c2128;
/* Image */
background-image: url('image.jpg');
background-size: cover; /* couvre tout (peut rogner) */
background-size: contain; /* tout visible (peut laisser de la place) */
background-size: 100% auto;
background-position: center;
background-position: top right;
background-repeat: no-repeat | repeat | repeat-x | repeat-y;
background-attachment: fixed; /* parallax simple */
/* Raccourci */
background: url('bg.jpg') center/cover no-repeat #1c2128;
/* Dégradés */
background: linear-gradient(to right, #1a1a2e, #16213e);
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
background: radial-gradient(circle at center, #58a6ff, #0d1117);
background: conic-gradient(red 0deg, yellow 120deg, green 240deg);
/* Plusieurs couches */
background:
linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),
url('photo.jpg') center/cover;
/* Clip du texte (effet dégradé sur texte) */
background: linear-gradient(135deg, #667eea, #764ba2);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
Bordures & Ombres
Border, Border-radius, Box-shadow
CSS
.card {
/* Border */
border: 1px solid #30363d;
border-top: 2px solid #58a6ff;
border-left: none;
border-style: solid | dashed | dotted | double | none;
/* Border-radius */
border-radius: 8px; /* tous les coins */
border-radius: 8px 0 8px 0; /* top-left, top-right, bottom-right, bottom-left */
border-radius: 50%; /* cercle (élément carré) */
border-radius: 20px 5px; /* top-left+bottom-right, top-right+bottom-left */
border-top-left-radius: 16px;
/* Box-shadow : offset-x offset-y blur spread color */
box-shadow: 0 2px 8px rgba(0,0,0,0.3);
box-shadow: 0 4px 24px rgba(0,0,0,0.5);
/* Ombre interne */
box-shadow: inset 0 2px 4px rgba(0,0,0,0.4);
/* Plusieurs ombres */
box-shadow:
0 1px 3px rgba(0,0,0,0.12),
0 1px 2px rgba(0,0,0,0.24);
/* Effet de glow */
box-shadow: 0 0 15px rgba(88, 166, 255, 0.4);
/* Pas d'ombre */
box-shadow: none;
}
Transitions & Animations
Transitions
CSS
.btn {
background: #58a6ff;
transition: background 0.3s ease;
/* Syntaxe complète : propriété durée timing délai */
transition: background 0.3s ease 0s;
transition: all 0.2s ease;
/* Plusieurs transitions */
transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.3s ease;
}
.btn:hover {
background: #79c0ff;
transform: translateY(-2px);
box-shadow: 0 8px 24px rgba(88,166,255,0.4);
}
/* Timing functions */
.elem {
transition-timing-function: ease; /* défaut */
transition-timing-function: ease-in; /* commence lentement */
transition-timing-function: ease-out; /* finit lentement */
transition-timing-function: ease-in-out; /* les deux */
transition-timing-function: linear; /* vitesse constante */
transition-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
transition-timing-function: steps(5, end); /* par étapes */
}
Animations avec
@keyframesCSS
/* Définir l'animation */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
@keyframes spinner {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
/* Appliquer l'animation */
.element {
animation: fadeIn 0.5s ease forwards;
/* Propriétés détaillées */
animation-name: fadeIn;
animation-duration: 0.5s;
animation-timing-function: ease;
animation-delay: 0.2s;
animation-iteration-count: 1; /* infinite = infini */
animation-direction: normal; /* reverse | alternate | alternate-reverse */
animation-fill-mode: forwards; /* garde l'état final */
animation-play-state: running; /* paused = met en pause */
}
/* Loader rotatif */
.loader {
width: 30px; height: 30px;
border: 3px solid rgba(255,255,255,0.1);
border-top-color: #58a6ff;
border-radius: 50%;
animation: spinner 0.8s linear infinite;
}
Transformations
Propriété
transformCSS
.element {
/* Translation (déplacement) */
transform: translateX(50px);
transform: translateY(-20px);
transform: translate(50px, -20px); /* x, y */
transform: translateZ(100px); /* 3D */
/* Mise à l'échelle */
transform: scale(1.2); /* 120% */
transform: scale(1.5, 0.8); /* x, y */
transform: scaleX(2);
transform: scaleY(0.5);
/* Rotation */
transform: rotate(45deg);
transform: rotateX(180deg); /* 3D */
transform: rotateY(45deg); /* 3D */
/* Inclinaison */
transform: skewX(20deg);
transform: skewY(10deg);
transform: skew(20deg, 10deg);
/* Combinaison */
transform: translateY(-4px) scale(1.02) rotate(3deg);
/* Point d'origine de la transformation */
transform-origin: center center; /* défaut */
transform-origin: top left;
transform-origin: 50% 100%;
}
Variables CSS (Custom Properties)
Déclarer et utiliser des variables
CSS
/* Déclaration globale sur :root */
:root {
--color-primary: #58a6ff;
--color-bg: #0d1117;
--color-text: #e6edf3;
--spacing-sm: 8px;
--spacing-md: 16px;
--spacing-lg: 32px;
--border-radius: 8px;
--font-size-base: 16px;
--shadow: 0 4px 20px rgba(0,0,0,0.3);
}
/* Utilisation avec var() */
.btn {
background: var(--color-primary);
padding: var(--spacing-sm) var(--spacing-md);
border-radius: var(--border-radius);
font-size: var(--font-size-base);
}
/* Valeur de fallback */
.element {
color: var(--color-accent, #ff6b6b); /* #ff6b6b si --color-accent n'existe pas */
}
/* Thème sombre/clair */
:root { --bg: #ffffff; --text: #000000; }
[data-theme="dark"] { --bg: #0d1117; --text: #e6edf3; }
body { background: var(--bg); color: var(--text); }
/* Modifier via JS */
/* document.documentElement.style.setProperty('--color-primary', '#ff6b6b'); */
/* Variables locales */
.card {
--card-padding: 20px;
padding: var(--card-padding);
}
.card.compact { --card-padding: 10px; }
Media Queries & Responsive
Breakpoints & Media Queries
Approche Mobile First : écrire le CSS de base pour mobile, puis surcharger avec des media queries pour les écrans plus grands.
CSS
/* === MOBILE FIRST === */
/* Base : mobile (< 576px) */
.container { padding: 16px; }
.grid { grid-template-columns: 1fr; }
/* Petit (≥ 576px) */
@media (min-width: 576px) {
.grid { grid-template-columns: repeat(2, 1fr); }
}
/* Moyen (≥ 768px) — tablette */
@media (min-width: 768px) {
.container { padding: 24px; }
.grid { grid-template-columns: repeat(3, 1fr); }
.sidebar { display: block; }
}
/* Large (≥ 1024px) — desktop */
@media (min-width: 1024px) {
.container { max-width: 1200px; margin: 0 auto; }
.grid { grid-template-columns: repeat(4, 1fr); }
}
/* Très large (≥ 1280px) */
@media (min-width: 1280px) { ... }
/* === AUTRES CONDITIONS === */
/* Orientation */
@media (orientation: portrait) { ... }
@media (orientation: landscape) { ... }
/* Préférence de mouvement réduit */
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
/* Thème sombre système */
@media (prefers-color-scheme: dark) { :root { --bg: #0d1117; } }
@media (prefers-color-scheme: light) { :root { --bg: #ffffff; } }
/* Impression */
@media print {
.sidebar, .navbar { display: none; }
body { font-size: 12pt; color: black; }
}
/* Combinaison */
@media (min-width: 768px) and (max-width: 1023px) { ... }
Pseudo-classes & Pseudo-éléments
Pseudo-classes
| Pseudo-classe | Description |
|---|---|
:hover | Survol de la souris |
:focus | Élément en focus (clavier/clic) |
:focus-visible | Focus visible uniquement au clavier |
:active | Pendant le clic |
:visited | Lien déjà visité |
:checked | Checkbox/radio cochée |
:disabled | Input désactivé |
:required | Input requis |
:valid / :invalid | Validation de formulaire |
:nth-child(n) | Nème enfant |
:nth-child(odd/even) | Impair / pair |
:first-child | Premier enfant |
:last-child | Dernier enfant |
:not(sel) | Tout sauf ce sélecteur |
:is(sel1, sel2) | Raccourci pour listes de sélecteurs |
:has(sel) | Parent contenant le sélecteur |
CSS
/* Zebra striping */
tr:nth-child(even) { background: rgba(255,255,255,0.03); }
/* Styles de formulaire */
input:focus { border-color: #58a6ff; box-shadow: 0 0 0 3px rgba(88,166,255,0.2); }
input:invalid:not(:placeholder-shown) { border-color: #f85149; }
input:valid { border-color: #3fb950; }
/* Sélectionner tous sauf le dernier */
li:not(:last-child) { border-bottom: 1px solid #30363d; }
/* Parent ayant une image */
.card:has(img) { padding: 0; }
/* ===== PSEUDO-ÉLÉMENTS ===== */
/* Avant/Après un élément */
.btn::before { content: "→ "; }
.btn::after { content: ""; display: block; width: 0; height: 2px; background: #58a6ff; transition: width 0.3s; }
.btn:hover::after { width: 100%; }
/* Première lettre / première ligne */
p::first-letter { font-size: 2em; float: left; margin-right: 4px; }
p::first-line { font-weight: bold; }
/* Sélection de texte */
::selection { background: #58a6ff; color: white; }
/* Placeholder */
::placeholder { color: #7d8590; font-style: italic; }
/* Scrollbar personnalisée */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-thumb { background: #30363d; border-radius: 3px; }
Aucun résultat pour votre recherche.