MODULE H03
CSS Avancé
Animations, transformations 3D, glassmorphism, container queries et architecture CSS moderne.
1. Animations & Transitions
Transitions
/* ── Transition simple ── */
.btn {
background: #3b82f6;
transition: background 0.2s ease, transform 0.15s ease;
}
.btn:hover {
background: #1d4ed8;
transform: translateY(-2px);
}
/* ── Transition complète ── */
/* transition: propriété durée timing-function délai */
transition: all 0.3s ease;
transition: opacity 0.4s ease-in-out, transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
/* ── Timing functions ── */
transition-timing-function: linear;
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; /* commence et finit lentement */
transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
Animations @keyframes
/* ── Définir une animation ── */
@keyframes fadeInUp {
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 gradientShift {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
/* ── Appliquer une animation ── */
.card {
animation: fadeInUp 0.5s ease forwards;
/* animation: nom durée timing délai iterations direction fill-mode */
}
/* ── Propriétés d'animation ── */
.loader {
animation-name: spin;
animation-duration: 1s;
animation-timing-function: linear;
animation-delay: 0s;
animation-iteration-count: infinite; /* ou nombre */
animation-direction: normal; /* reverse | alternate | alternate-reverse */
animation-fill-mode: forwards; /* none | forwards | backwards | both */
animation-play-state: running; /* ou paused */
}
/* ── Accessibilité : respecter prefers-reduced-motion ── */
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
2. Transformations 2D & 3D
/* ── 2D ── */
.el {
transform: translateX(50px);
transform: translateY(-20px);
transform: translate(50px, -20px); /* x, y */
transform: scale(1.2); /* uniforme */
transform: scaleX(2) scaleY(0.5);
transform: rotate(45deg);
transform: skewX(15deg);
transform: skewY(10deg);
/* Combinaisons (de droite à gauche) */
transform: translateY(-4px) scale(1.02);
/* Origine de la transformation */
transform-origin: center center; /* défaut */
transform-origin: top left;
transform-origin: 50% 50%;
}
/* ── 3D ── */
.scene {
perspective: 1000px; /* sur le parent */
perspective-origin: 50% 50%;
}
.card-3d {
transform-style: preserve-3d; /* sur le parent 3D */
transform: rotateY(30deg);
transform: rotateX(15deg) translateZ(50px);
}
/* ── Flip card (exemple complet) ── */
.flip-container {
perspective: 1000px;
width: 300px; height: 200px;
}
.flipper {
transform-style: preserve-3d;
transition: transform 0.6s;
position: relative; width: 100%; height: 100%;
}
.flip-container:hover .flipper {
transform: rotateY(180deg);
}
.front, .back {
position: absolute; inset: 0;
backface-visibility: hidden; /* cache la face inversée */
border-radius: 12px;
display: flex; align-items: center; justify-content: center;
}
.back { transform: rotateY(180deg); background: #3b82f6; color: white; }
3. Pseudo-éléments avancés
/* ── ::before et ::after ── */
.badge {
position: relative;
}
.badge::before {
content: '';
position: absolute;
inset: -2px;
background: linear-gradient(135deg, #f78166, #d2a8ff);
border-radius: inherit;
z-index: -1; /* derrière l'élément */
}
/* ── Effet underline animé ── */
.nav-link {
position: relative;
text-decoration: none;
}
.nav-link::after {
content: '';
position: absolute;
bottom: -2px; left: 0;
width: 0; height: 2px;
background: #3b82f6;
transition: width 0.3s ease;
}
.nav-link:hover::after { width: 100%; }
/* ── Compteur CSS ── */
ol.fancy {
counter-reset: item;
list-style: none;
}
ol.fancy li {
counter-increment: item;
}
ol.fancy li::before {
content: counter(item, decimal-leading-zero) '.';
color: #3b82f6;
font-weight: 700;
margin-right: 0.5rem;
}
/* ── Sélection de texte ── */
::selection {
background: rgba(59, 130, 246, 0.3);
color: inherit;
}
4. Glassmorphism
Le glassmorphism crée un effet de verre dépoli — très utilisé dans les interfaces modernes.
/* ── Fond nécessaire (gradient ou image) ── */
body {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
/* ── Verre de base ── */
.glass {
background: rgba(255, 255, 255, 0.15);
backdrop-filter: blur(16px) saturate(180%);
-webkit-backdrop-filter: blur(16px) saturate(180%);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 16px;
}
/* ── Variante sombre ── */
.glass-dark {
background: rgba(10, 14, 26, 0.72);
backdrop-filter: blur(14px);
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 14px;
}
/* ── Carte glassmorphisme complète ── */
.glass-card {
background: rgba(255, 255, 255, 0.12);
backdrop-filter: blur(20px);
border: 1px solid rgba(255, 255, 255, 0.25);
border-radius: 20px;
padding: 2rem;
box-shadow:
0 8px 32px rgba(0, 0, 0, 0.2),
inset 0 1px 0 rgba(255, 255, 255, 0.3);
}
/* ── Bouton glassmorphisme ── */
.glass-btn {
background: rgba(255, 255, 255, 0.15);
border: 1px solid rgba(255, 255, 255, 0.3);
backdrop-filter: blur(8px);
color: white;
transition: background 0.2s;
}
.glass-btn:hover {
background: rgba(255, 255, 255, 0.25);
}
Support navigateur backdrop-filter est bien supporté (Chrome, Firefox, Safari), mais vérifiez le fallback pour les anciens navigateurs.
5. Scrollbar personnalisée
/* ── Webkit (Chrome, Safari, Edge) ── */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.2);
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: rgba(255, 255, 255, 0.35);
}
/* ── Standard CSS (Firefox) ── */
* {
scrollbar-width: thin;
scrollbar-color: rgba(255,255,255,0.2) transparent;
}
/* ── Scroll snap ── */
.carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
gap: 1rem;
scrollbar-width: none; /* cacher la scrollbar */
}
.carousel::-webkit-scrollbar { display: none; }
.carousel-item {
flex-shrink: 0;
width: 80%;
scroll-snap-align: start;
}
6. Clip-path & Mask
/* ── clip-path ── */
.circle { clip-path: circle(50%); }
.triangle { clip-path: polygon(50% 0%, 0% 100%, 100% 100%); }
.hexagon { clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%); }
.diagonal { clip-path: polygon(0 0, 100% 0, 100% 80%, 0 100%); }
/* Inset (rectangle avec marges et radius) */
.card-clip { clip-path: inset(0 round 12px); }
/* Animation de clip-path */
.reveal-clip {
clip-path: inset(0 100% 0 0);
transition: clip-path 0.6s cubic-bezier(0.76, 0, 0.24, 1);
}
.reveal-clip.visible { clip-path: inset(0 0% 0 0); }
/* ── mask-image ── */
.gradient-fade {
mask-image: linear-gradient(to right, black 60%, transparent 100%);
-webkit-mask-image: linear-gradient(to right, black 60%, transparent 100%);
}
/* Mask avec image */
.masked {
mask-image: url('mask-shape.svg');
mask-size: cover;
-webkit-mask-image: url('mask-shape.svg');
}
7. Filtres CSS
/* ── filter ── */
img.effect {
filter: blur(4px);
filter: brightness(1.5);
filter: contrast(200%);
filter: grayscale(100%);
filter: hue-rotate(90deg);
filter: invert(100%);
filter: opacity(50%);
filter: saturate(300%);
filter: sepia(100%);
filter: drop-shadow(4px 4px 8px rgba(0,0,0,0.5));
/* Combinaison */
filter: contrast(150%) brightness(1.1) saturate(120%);
}
/* Hover effet cinématique */
.photo {
filter: grayscale(80%) contrast(110%);
transition: filter 0.4s ease;
}
.photo:hover { filter: none; }
/* ── backdrop-filter ── */
.frosted-glass {
backdrop-filter: blur(16px) saturate(1.8) brightness(1.1);
/* uniquement sur l'arrière-plan derrière l'élément */
}
/* ── mix-blend-mode ── */
.text-overlay {
mix-blend-mode: multiply; /* screen | overlay | difference | exclusion */
color: white;
}
8. Container Queries (CSS moderne)
Les Container Queries permettent de styler un composant en fonction de la taille de son conteneur plutôt que de la fenêtre.
/* ── Définir un conteneur ── */
.card-wrapper {
container-type: inline-size;
container-name: card; /* optionnel */
}
/* ── Styler selon la taille du conteneur ── */
@container card (min-width: 400px) {
.card {
display: flex;
flex-direction: row;
}
}
@container card (max-width: 300px) {
.card h2 { font-size: 0.9rem; }
.card img { display: none; }
}
/* ── Utilisation avec sidebar ── */
.sidebar-widget { container-type: inline-size; }
@container (min-width: 250px) {
.widget-content {
display: grid;
grid-template-columns: 1fr 1fr;
}
}
/* ── Container query units ── */
.element {
font-size: 3cqi; /* cqi = 1% de la taille inline du conteneur */
padding: 2cqb; /* cqb = 1% de la taille block */
width: 50cqw; /* cqw = 1% de la largeur du conteneur */
}
Container Queries vs Media Queries Media queries = taille de fenêtre globale. Container queries = taille locale du composant. Idéal pour les design systems réutilisables.
9. :has() :is() :where()
/* ── :is() — groupe de sélecteurs ── */
:is(h1, h2, h3, h4) { margin-bottom: 1rem; }
/* équivalent à : h1, h2, h3, h4 { ... } */
:is(article, section, aside) p { color: #64748b; }
/* équivalent à : article p, section p, aside p */
/* ── :where() — comme :is() mais spécificité 0 ── */
:where(h1, h2, h3) { font-weight: 700; }
/* Utile dans les resets — facile à écraser */
/* ── :has() — parent selector ── */
/* Styler un parent selon son contenu */
.card:has(img) {
/* La carte a une image → padding différent */
padding: 0;
}
.card:has(img) .card-body {
padding: 1rem;
}
/* Styler un label selon son input */
label:has(input:checked) {
font-weight: 700;
color: #3b82f6;
}
/* Formulaire valide / invalide */
form:has(input:invalid) button[type="submit"] {
opacity: 0.5;
pointer-events: none;
}
/* Section sans image */
.hero:not(:has(img)) { text-align: center; }
/* ── :not() avancé ── */
.card:not(:last-child) { border-bottom: 1px solid #e2e8f0; }
a:not([href^="http"]):not([href^="mailto"]) { color: inherit; }
10. Architecture CSS moderne
Méthodes BEM
/* ── BEM (Block Element Modifier) ── */
/* block__element--modifier */
/* Block */
.card { ... }
/* Elements (appartiennent au block) */
.card__header { ... }
.card__body { ... }
.card__footer { ... }
.card__title { ... }
.card__image { ... }
/* Modifiers (variantes) */
.card--featured { border-color: gold; }
.card--dark { background: #1e293b; }
.card__title--sm { font-size: 0.9rem; }
/* Usage HTML */
/* <div class="card card--featured">
<div class="card__header">
<h2 class="card__title">Titre</h2>
</div>
<div class="card__body">...</div>
</div> */
Architecture en couches (Cascade Layers)
/* ── @layer ── */
@layer reset, base, components, utilities;
@layer reset {
*, *::before, *::after { box-sizing: border-box; margin: 0; }
}
@layer base {
body { font-family: system-ui; color: #1e293b; }
h1, h2, h3 { font-weight: 700; line-height: 1.2; }
}
@layer components {
.btn { padding: 0.5rem 1rem; border-radius: 6px; }
.card { background: white; border-radius: 12px; padding: 1.5rem; }
}
@layer utilities {
.text-center { text-align: center; }
.flex { display: flex; }
.hidden { display: none; }
}
/* ── Nesting CSS (natif, sans Sass) ── */
.card {
background: white;
border-radius: 12px;
& h2 { font-size: 1.1rem; } /* .card h2 */
& .card-body { padding: 1rem; } /* .card .card-body */
&:hover { box-shadow: 0 4px 16px rgba(0,0,0,0.1); }
@media (max-width: 768px) {
padding: 1rem; /* media query nested */
}
}