C'est quoi Flexbox ?
Flexbox (Flexible Box Layout) est un système de mise en page CSS conçu pour aligner et distribuer des éléments dans un conteneur. Avant Flexbox, centrer un élément verticalement était un cauchemar. Maintenant, c'est une ligne.
Le principe : on déclare display: flex sur le conteneur, et ses enfants directs deviennent des flex items qui se positionnent automatiquement.
Direction et alignement
flex-direction: row— en ligne (par défaut, de gauche à droite)flex-direction: column— en colonne (de haut en bas)
L'axe principal (flex-direction) définit deux propriétés d'alignement :
justify-content— alignement sur l'axe principal (horizontal en row)align-items— alignement sur l'axe transversal (vertical en row)
Valeurs de justify-content : flex-start, center, flex-end, space-between, space-around, space-evenly.
Valeurs de align-items : stretch (par défaut), center, flex-start, flex-end.
Gap, flex-wrap et flex
gap: 16px— espace entre les flex items (remplace les margin)flex-wrap: wrap— les items passent à la ligne si pas assez de placeflex: 1— raccourci pourflex-grow: 1; flex-shrink: 1; flex-basis: 0(l'item prend l'espace disponible)
Astuce : display: flex; justify-content: center; align-items: center; — ces 3 lignes centrent parfaitement n'importe quel contenu, horizontalement et verticalement. C'est la technique de centrage la plus utilisée.
What is Flexbox?
Flexbox (Flexible Box Layout) is a CSS layout system designed to align and distribute elements in a container. Before Flexbox, vertically centering an element was a nightmare. Now it's one line.
The principle: you declare display: flex on the container, and its direct children become flex items that position themselves automatically.
Direction and alignment
flex-direction: row— in a row (default, left to right)flex-direction: column— in a column (top to bottom)
The main axis (flex-direction) defines two alignment properties:
justify-content— alignment on the main axis (horizontal in row)align-items— alignment on the cross axis (vertical in row)
justify-content values: flex-start, center, flex-end, space-between, space-around, space-evenly.
align-items values: stretch (default), center, flex-start, flex-end.
Gap, flex-wrap and flex
gap: 16px— space between flex items (replaces margins)flex-wrap: wrap— items wrap to the next line if there's not enough roomflex: 1— shorthand forflex-grow: 1; flex-shrink: 1; flex-basis: 0(item takes available space)
Tip: display: flex; justify-content: center; align-items: center; — these 3 lines perfectly center any content, horizontally and vertically. It's the most used centering technique.
Pour pratiquer Flexbox de manière ludique, essayez :
Flexbox Froggy : https://flexboxfroggy.com/ — Un jeu gratuit pour apprendre Flexbox en guidant des grenouilles vers leurs nénuphars.
L'IA devait centrer le contenu de la boîte, mais elle a utilisé justify-content: flex-start au lieu de center. Le texte est collé à gauche au lieu d'être centré. Corrigez !
flex-wrap: wrap ?