retour
01/04
section 01
Transitions CSS
Connectez-vous pour sauvegarder votre progression et acceder aux quiz
Se connecterLes transitions CSS permettent d'animer le passage d'un etat a un autre de maniere fluide. Elles sont declenchees par un changement d'etat (hover, focus, ajout de classe, etc.).
Syntaxe des transitions
css.bouton {
background: #8b5cf6;
color: white;
padding: 12px 24px;
border: none;
cursor: pointer;
/* Transition detaillee */
transition-property: background, transform;
transition-duration: 0.3s;
transition-timing-function: ease;
transition-delay: 0s;
/* Raccourci (recommande) */
transition: background 0.3s ease, transform 0.2s ease;
}
.bouton:hover {
background: #6d28d9;
transform: translateY(-2px);
}Les fonctions de timing
css.element {
/* Fonctions predefinies */
transition-timing-function: ease; /* Lent-rapide-lent (defaut) */
transition-timing-function: linear; /* Vitesse constante */
transition-timing-function: ease-in; /* Lent au debut */
transition-timing-function: ease-out; /* Lent a la fin */
transition-timing-function: ease-in-out; /* Lent debut et fin */
/* Courbe de Bezier personnalisee */
transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
/* Effet "bounce" */
}Proprietes animables
Toutes les proprietes ne sont pas animables. Les principales :
| Animables (performant) | Animables (moins performant) | Non animables |
|---|---|---|
transform | width, height | display |
opacity | margin, padding | font-family |
color, background-color | border-width | position |
box-shadow | top, left, right, bottom | float |
Performance : Privilegiez les transitions sur
transform et opacity. Ces proprietes sont gerees par le GPU (composition) et ne declenchent pas de recalcul du layout.Exemples pratiques de transitions
css/* Carte avec elevation au survol */
.card {
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
transition: box-shadow 0.3s ease, transform 0.3s ease;
}
.card:hover {
box-shadow: 0 8px 25px rgba(0,0,0,0.15);
transform: translateY(-4px);
}
/* Lien avec soulignement anime */
.link {
position: relative;
text-decoration: none;
color: #8b5cf6;
}
.link::after {
content: '';
position: absolute;
bottom: -2px;
left: 0;
width: 0;
height: 2px;
background: #8b5cf6;
transition: width 0.3s ease;
}
.link:hover::after {
width: 100%;
}
/* Input avec bordure animee au focus */
.input {
border: 2px solid #ddd;
outline: none;
transition: border-color 0.3s ease, box-shadow 0.3s ease;
}
.input:focus {
border-color: #8b5cf6;
box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.2);
}Astuce UX : Une transition de 200-300ms est ideale pour les interactions utilisateur. En dessous de 100ms, elle est a peine perceptible. Au-dessus de 500ms, elle donne une impression de lenteur.
// voir aussi