← Retour au cours : CSS de Zéro à Héros
⚠️ Mode Visiteur : Vous lisez ce cours gratuitement. Créez un compte pour sauvegarder votre progression et obtenir des badges ! 🏆

Transitions et Animations

Animer vos pages web

Les animations CSS rendent vos sites vivants et engageants.

Transitions CSS

Les transitions animent le changement de propriétés CSS.

Syntaxe de base

.bouton {
    background-color: blue;
    transition: background-color 0.3s;
}

.bouton:hover {
    background-color: red;
}

Propriétés transition

/* Propriété à animer */
transition-property: background-color;
transition-property: all;
transition-property: width, height;

/* Durée */
transition-duration: 0.3s;
transition-duration: 300ms;

/* Fonction de timing */
transition-timing-function: ease; /* Défaut */
transition-timing-function: linear;
transition-timing-function: ease-in;
transition-timing-function: ease-out;
transition-timing-function: ease-in-out;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);

/* Délai */
transition-delay: 0.5s;

Notation courte

.element {
    transition: property duration timing-function delay;
    transition: all 0.3s ease 0s;
    transition: width 0.5s, height 0.3s;
}

Exemples de transitions

/* Bouton hover */
.btn {
    background: #007bff;
    color: white;
    padding: 10px 20px;
    border: none;
    transition: all 0.3s ease;
}

.btn:hover {
    background: #0056b3;
    transform: translateY(-2px);
    box-shadow: 0 5px 10px rgba(0,0,0,0.2);
}

/* Carte qui grandit */
.card {
    transition: transform 0.3s ease;
}

.card:hover {
    transform: scale(1.05);
}

/* Lien souligné */
.link {
    position: relative;
    text-decoration: none;
}

.link::after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    width: 0;
    height: 2px;
    background: blue;
    transition: width 0.3s ease;
}

.link:hover::after {
    width: 100%;
}

Animations avec @keyframes

Les keyframes permettent des animations complexes.

Syntaxe de base

@keyframes nom-animation {
    from {
        /* État de départ */
    }
    to {
        /* État final */
    }
}

/* Ou avec pourcentages */
@keyframes nom-animation {
    0% { }
    50% { }
    100% { }
}

Appliquer l'animation

.element {
    animation-name: nom-animation;
    animation-duration: 2s;
    animation-timing-function: ease;
    animation-delay: 0s;
    animation-iteration-count: infinite;
    animation-direction: normal;
    animation-fill-mode: forwards;
    
    /* Notation courte */
    animation: nom-animation 2s ease 0s infinite normal forwards;
}

Exemples d'animations

Fondu

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.element {
    animation: fadeIn 1s ease;
}

Slide In

@keyframes slideInLeft {
    from {
        transform: translateX(-100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

.element {
    animation: slideInLeft 0.5s ease;
}

Rotation

@keyframes spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

.loader {
    animation: spin 1s linear infinite;
}

Pulsation

@keyframes pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
    }
}

.button {
    animation: pulse 2s ease infinite;
}

Bounce

@keyframes bounce {
    0%, 20%, 50%, 80%, 100% {
        transform: translateY(0);
    }
    40% {
        transform: translateY(-30px);
    }
    60% {
        transform: translateY(-15px);
    }
}

.element {
    animation: bounce 2s ease infinite;
}

Transform (sans animation)

/* Translation */
transform: translateX(100px);
transform: translateY(-50px);
transform: translate(50px, 100px);

/* Rotation */
transform: rotate(45deg);
transform: rotateX(45deg);
transform: rotateY(45deg);

/* Échelle */
transform: scale(1.5);
transform: scaleX(2);
transform: scaleY(0.5);
transform: scale(1.2, 0.8);

/* Inclinaison */
transform: skew(10deg, 5deg);
transform: skewX(20deg);

/* Combinaisons */
transform: rotate(45deg) scale(1.2) translateX(50px);

Animation au scroll (avec classes JS)

.fade-in {
    opacity: 0;
    transform: translateY(30px);
    transition: all 0.6s ease;
}

.fade-in.visible {
    opacity: 1;
    transform: translateY(0);
}

Performance

Propriétés performantes (GPU accelerated) :

  • transform
  • opacity

Éviter d'animer :

  • width, height (provoque reflow)
  • top, left (utilisez translate à la place)
  • background-color sur de gros éléments

Exemple complet : carte animée

.card {
    transition: all 0.3s ease;
    transform-origin: center;
}

.card:hover {
    transform: translateY(-10px) scale(1.02);
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

.card-image {
    overflow: hidden;
}

.card-image img {
    transition: transform 0.5s ease;
}

.card:hover .card-image img {
    transform: scale(1.1);
}

@keyframes cardEntrance {
    from {
        opacity: 0;
        transform: translateY(50px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card {
    animation: cardEntrance 0.6s ease;
}

Points clés

  • transition pour changements simples
  • @keyframes pour animations complexes
  • transform: translate() pour déplacer
  • transform: scale() pour agrandir
  • Animer transform et opacity (performant)
  • ease-in-out pour fluidité

📝 Vous avez terminé cette leçon ?

Créez un compte gratuit pour sauvegarder votre progression !

Créer un compte gratuit