# Référence rapide CSS

*Sélecteurs, mise en page, flexbox, grid, animation, responsive*

> Source: MDN Web Docs (developer.mozilla.org) · MIT

## Sélecteurs

### Sélecteurs de base

| Command | Description |
|---------|-------------|
| `*` | Universel — tous les éléments |
| `div` | Type — tous les éléments `<div>` |
| `.class` | Classe — éléments avec cette classe |
| `#id` | ID — élément avec cet identifiant |
| `[attr]` | Attribut — possède l'attribut |
| `[attr="val"]` | Attribut égal à la valeur |

### Combinateurs

| Command | Description |
|---------|-------------|
| `A B` | Descendant (toute profondeur) |
| `A > B` | Enfant direct uniquement |
| `A + B` | Frère adjacent (suivant) |
| `A ~ B` | Frères généraux (tous après) |

### Pseudo-classes

| Command | Description |
|---------|-------------|
| `:hover` | Survol de l'élément |
| `:focus` | Élément avec le focus |
| `:first-child` | Premier enfant du parent |
| `:nth-child(n)` | Nième enfant (base 1, `odd`, `even`, `2n+1`) |
| `:not(sel)` | Négation — exclure les correspondances |
| `:has(sel)` | Sélecteur parent (contient une correspondance) |

### Pseudo-éléments

| Command | Description |
|---------|-------------|
| `::before` | Insérer du contenu avant l'élément |
| `::after` | Insérer du contenu après l'élément |
| `::placeholder` | Styliser le texte placeholder d'un champ |
| `::selection` | Styliser le texte sélectionné |

## Modèle de boîte

### Box Sizing

```
/* Include padding/border in width */
*, *::before, *::after {
  box-sizing: border-box;
}
```

### Propriétés

| Command | Description |
|---------|-------------|
| `margin` | Espace extérieur à la bordure |
| `border` | Bordure autour du padding |
| `padding` | Espace intérieur à la bordure |
| `width / height` | Dimensions du contenu |
| `outline` | Contour extérieur (ne prend pas de place) |

### Raccourci

```
margin: 10px;           /* all sides */
margin: 10px 20px;      /* vertical | horizontal */
margin: 10px 20px 30px; /* top | horiz | bottom */
margin: 10px 20px 30px 40px; /* T R B L */
```

## Flexbox

### Conteneur

```
.flex {
  display: flex;
  justify-content: center; /* main axis */
  align-items: center;     /* cross axis */
  gap: 1rem;
}
```

### Propriétés du conteneur

| Command | Description |
|---------|-------------|
| `flex-direction` | `row` \| `column` \| `row-reverse` \| `column-reverse` |
| `flex-wrap` | `nowrap` \| `wrap` \| `wrap-reverse` |
| `justify-content` | `flex-start` \| `center` \| `space-between` \| `space-around` \| `space-evenly` |
| `align-items` | `stretch` \| `center` \| `flex-start` \| `flex-end` \| `baseline` |
| `align-content` | Alignement axe secondaire multi-lignes |
| `gap` | Espace entre les éléments flex |

### Propriétés des éléments

| Command | Description |
|---------|-------------|
| `flex: 1` | S'étendre pour remplir l'espace |
| `flex: 0 0 200px` | Largeur fixe, sans croissance ni rétrécissement |
| `align-self` | Remplacer align-items pour un élément |
| `order` | Modifier l'ordre visuel (défaut 0) |

## Grid

### Conteneur

```
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto 1fr auto;
  gap: 1rem;
}
```

### Propriétés du conteneur

| Command | Description |
|---------|-------------|
| `grid-template-columns` | Définir les colonnes : `1fr 2fr`, `repeat(3, 1fr)` |
| `grid-template-rows` | Définir les lignes |
| `grid-template-areas` | Zones nommées : `"header header" "nav main"` |
| `gap` | Espacement lignes et colonnes |
| `justify-items` | Aligner les éléments inline (horizontal) |
| `align-items` | Aligner les éléments block (vertical) |

### Placement des éléments

```
.item {
  grid-column: 1 / 3;   /* span cols 1-2 */
  grid-row: 1 / -1;     /* span all rows */
  grid-area: header;    /* named area */
}
```

## Typographie

### Propriétés de police

| Command | Description |
|---------|-------------|
| `font-family` | Pile de polices : `'Inter', sans-serif` |
| `font-size` | Taille : `1rem`, `16px`, `clamp(1rem, 2vw, 2rem)` |
| `font-weight` | `normal` (400) \| `bold` (700) \| `100`-`900` |
| `font-style` | `normal` \| `italic` \| `oblique` |
| `line-height` | Interligne : `1.5` (sans unité recommandé) |
| `letter-spacing` | Espacement des caractères : `0.05em` |

### Propriétés de texte

| Command | Description |
|---------|-------------|
| `text-align` | `left` \| `center` \| `right` \| `justify` |
| `text-decoration` | `none` \| `underline` \| `line-through` |
| `text-transform` | `uppercase` \| `lowercase` \| `capitalize` |
| `text-overflow` | `ellipsis` (avec overflow: hidden) |
| `white-space` | `nowrap` \| `pre` \| `pre-wrap` |
| `word-break` | `break-all` \| `break-word` |

## Couleurs & Arrière-plans

### Formats de couleur

```
color: #ff6600;             /* hex */
color: rgb(255, 102, 0);    /* rgb */
color: hsl(24, 100%, 50%);  /* hsl */
color: oklch(70% 0.15 50);  /* oklch */
```

### Propriétés d'arrière-plan

| Command | Description |
|---------|-------------|
| `background-color` | Couleur unie : `#f0f0f0` |
| `background-image` | `url(img.jpg)` ou dégradé |
| `background-size` | `cover` \| `contain` \| `100px 200px` |
| `background-position` | `center` \| `top right` \| `50% 50%` |
| `background-repeat` | `no-repeat` \| `repeat-x` \| `repeat-y` |

### Dégradés

```
background: linear-gradient(to right, #f00, #00f);
background: radial-gradient(circle, #fff, #000);
background: conic-gradient(red, yellow, green);
```

## Transitions & Animations

### Transitions

```
.btn {
  transition: background 0.3s ease, transform 0.2s;
}
.btn:hover {
  background: #0056b3;
  transform: scale(1.05);
}
```

### Propriétés de transition

| Command | Description |
|---------|-------------|
| `transition-property` | Quelle propriété animer |
| `transition-duration` | Durée : `0.3s`, `300ms` |
| `transition-timing-function` | `ease` \| `linear` \| `ease-in-out` \| `cubic-bezier()` |
| `transition-delay` | Délai avant le démarrage |

### Animation par images clés

```
@keyframes spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}
.icon { animation: spin 1s linear infinite; }
```

### Propriétés d'animation

| Command | Description |
|---------|-------------|
| `animation-name` | Référence au nom @keyframes |
| `animation-duration` | Durée d'un cycle |
| `animation-iteration-count` | `1` \| `3` \| `infinite` |
| `animation-direction` | `normal` \| `alternate` \| `reverse` |
| `animation-fill-mode` | `forwards` \| `backwards` \| `both` |

## Responsive Design

### Media Queries

```
@media (max-width: 768px) {
  .sidebar { display: none; }
}
@media (prefers-color-scheme: dark) {
  body { background: #1a1a1a; color: #eee; }
}
```

### Points de rupture courants

| Command | Description |
|---------|-------------|
| `max-width: 480px` | Téléphones mobiles |
| `max-width: 768px` | Tablettes |
| `max-width: 1024px` | Petits laptops |
| `max-width: 1280px` | Ordinateurs de bureau |

### Unités de viewport

| Command | Description |
|---------|-------------|
| `vw / vh` | % de la largeur / hauteur du viewport |
| `dvh` | Hauteur dynamique du viewport (mobile) |
| `svh / lvh` | Petite / grande hauteur du viewport |
| `cqi` | Taille inline de la container query |

### Container Queries

```
.card-wrapper { container-type: inline-size; }
@container (min-width: 400px) {
  .card { flex-direction: row; }
}
```

## Positionnement

### Valeurs de position

| Command | Description |
|---------|-------------|
| `static` | Défaut — flux normal du document |
| `relative` | Décalé de sa position normale ; conserve l'espace |
| `absolute` | Positionné par rapport au parent positionné |
| `fixed` | Positionné par rapport au viewport ; reste au scroll |
| `sticky` | Alterne relative/fixe selon le scroll |

### Patterns de centrage

```
/* Flex center */
display: flex;
justify-content: center;
align-items: center;

/* Grid center */
display: grid;
place-items: center;
```

### Empilement

| Command | Description |
|---------|-------------|
| `z-index` | Ordre d'empilement (plus élevé = au-dessus) ; nécessite `position` |
| `isolation: isolate` | Crée un nouveau contexte d'empilement |

## Propriétés personnalisées

### Définir & Utiliser

```
:root {
  --color-primary: #3b82f6;
  --spacing-md: 1rem;
}
.btn {
  background: var(--color-primary);
  padding: var(--spacing-md);
}
```

### Valeurs de repli

```
color: var(--accent, #ff6600);
/* uses #ff6600 if --accent is not defined */
```

### Thématisation dynamique

```
[data-theme="dark"] {
  --bg: #1a1a2e;
  --text: #e0e0e0;
}
body { background: var(--bg); color: var(--text); }
```
