Introduction
Les formulaires sont au coeur de l'expérience utilisateur sur le web. Un formulaire bien conçu avec des animations fluides et une validation visuelle claire peut transformer une tâche fastidieuse en une interaction agréable.
Dans ce tutoriel, nous allons explorer 5 techniques essentielles pour créer des inputs modernes : floating labels, bordures animées, validation visuelle, checkboxes/radios custom et toggle switches. Chaque exemple est interactif et le code est prêt à être copié.
Toutes ces techniques utilisent principalement CSS avec le sélecteur :focus et le pseudo-sélecteur :placeholder-shown. Le JavaScript n'est nécessaire que pour la validation dynamique.
1. Floating Label Animation
Le floating label est devenu un standard du design moderne. Le label "flotte" au-dessus de l'input lorsque l'utilisateur commence à saisir, offrant un gain de place tout en gardant le contexte visible.
<!-- HTML -->
<div class="floating-input-group">
<input type="text" class="floating-input" placeholder=" " id="email">
<label class="floating-label" for="email">Adresse email</label>
</div>
.floating-input-group {
position: relative;
width: 300px;
}
.floating-input {
width: 100%;
padding: 20px 16px 8px;
font-size: 1rem;
background: rgba(255, 255, 255, 0.05);
border: 2px solid rgba(255, 255, 255, 0.1);
border-radius: 12px;
color: white;
outline: none;
transition: all 0.3s ease;
}
.floating-input:focus {
border-color: #6366f1;
background: rgba(99, 102, 241, 0.05);
box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}
.floating-label {
position: absolute;
left: 16px;
top: 50%;
transform: translateY(-50%);
font-size: 1rem;
color: #a1a1aa;
pointer-events: none;
transition: all 0.3s ease;
}
/* Animation au focus ou si rempli */
.floating-input:focus ~ .floating-label,
.floating-input:not(:placeholder-shown) ~ .floating-label {
top: 8px;
transform: translateY(0);
font-size: 0.75rem;
color: #6366f1;
}
Comment ça fonctionne
La magie repose sur deux sélecteurs CSS :
:focus: détecte quand l'input est actif:not(:placeholder-shown): détecte quand l'input contient du texte (le placeholder n'est plus visible)
Le placeholder=" " (espace) est crucial : il permet au sélecteur :placeholder-shown de fonctionner sans afficher de texte visible.
2. Input avec Border Animée
Cette technique utilise une ligne animée qui s'étend du centre vers les bords lorsque l'input reçoit le focus. C'est un effet élégant inspire du Material Design.
.border-input-group {
position: relative;
width: 300px;
}
.border-input {
width: 100%;
padding: 16px;
font-size: 1rem;
background: transparent;
border: none;
border-bottom: 2px solid rgba(255, 255, 255, 0.2);
color: white;
outline: none;
}
/* Ligne animee */
.border-input-line {
position: absolute;
bottom: 0;
left: 50%;
width: 0;
height: 2px;
background: linear-gradient(135deg, #6366f1, #8b5cf6);
transition: all 0.4s ease;
}
.border-input:focus ~ .border-input-line {
left: 0;
width: 100%;
}
/* Label anime */
.border-input-label {
position: absolute;
left: 0;
top: 16px;
color: #a1a1aa;
pointer-events: none;
transition: all 0.3s ease;
}
.border-input:focus ~ .border-input-label,
.border-input:not(:placeholder-shown) ~ .border-input-label {
top: -12px;
font-size: 0.8rem;
color: #6366f1;
}
3. Validation Visuelle (Success/Error)
La validation visuelle donne un feedback immédiat à l'utilisateur. Nous allons créer un input qui affiche des états de succès et d'erreur avec des animations subtiles.
.validation-input {
width: 100%;
padding: 16px 48px 16px 16px;
background: rgba(255, 255, 255, 0.05);
border: 2px solid rgba(255, 255, 255, 0.1);
border-radius: 12px;
color: white;
transition: all 0.3s ease;
}
/* Etat succes */
.validation-input.success {
border-color: #10b981;
background: rgba(16, 185, 129, 0.05);
}
/* Etat erreur avec shake */
.validation-input.error {
border-color: #ef4444;
background: rgba(239, 68, 68, 0.05);
animation: shake 0.5s ease;
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
20%, 60% { transform: translateX(-5px); }
40%, 80% { transform: translateX(5px); }
}
/* Icones de validation */
.validation-icon {
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%) scale(0);
width: 24px;
height: 24px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s ease;
}
.validation-input.success ~ .success-icon {
transform: translateY(-50%) scale(1);
background: #10b981;
}
.validation-input.error ~ .error-icon {
transform: translateY(-50%) scale(1);
background: #ef4444;
}
4. Checkbox et Radio Custom Animés
Les checkboxes et radios natifs sont difficiles à styliser. Voici comment créer des versions custom avec des animations satisfaisantes.
/* Custom Checkbox */
.custom-checkbox {
display: flex;
align-items: center;
gap: 12px;
cursor: pointer;
color: #a1a1aa;
transition: color 0.2s;
}
.custom-checkbox:hover {
color: white;
}
/* Cacher l'input natif */
.custom-checkbox input {
display: none;
}
.checkbox-box {
width: 24px;
height: 24px;
border: 2px solid rgba(255, 255, 255, 0.2);
border-radius: 6px;
position: relative;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
}
/* Coche en CSS */
.checkbox-box::after {
content: '';
width: 6px;
height: 12px;
border: 2px solid white;
border-top: none;
border-left: none;
transform: rotate(45deg) scale(0);
margin-bottom: 3px;
transition: transform 0.2s ease;
}
/* Etat coche */
.custom-checkbox input:checked ~ .checkbox-box {
background: #6366f1;
border-color: #6366f1;
animation: checkPop 0.3s ease;
}
.custom-checkbox input:checked ~ .checkbox-box::after {
transform: rotate(45deg) scale(1);
}
@keyframes checkPop {
0% { transform: scale(1); }
50% { transform: scale(1.2); }
100% { transform: scale(1); }
}
/* Custom Radio */
.custom-radio {
display: flex;
align-items: center;
gap: 12px;
cursor: pointer;
}
.custom-radio input {
display: none;
}
.radio-circle {
width: 24px;
height: 24px;
border: 2px solid rgba(255, 255, 255, 0.2);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s ease;
}
.radio-circle::after {
content: '';
width: 12px;
height: 12px;
background: #6366f1;
border-radius: 50%;
transform: scale(0);
transition: transform 0.2s ease;
}
.custom-radio input:checked ~ .radio-circle {
border-color: #6366f1;
}
.custom-radio input:checked ~ .radio-circle::after {
transform: scale(1);
animation: radioPop 0.3s ease;
}
@keyframes radioPop {
0% { transform: scale(0); }
50% { transform: scale(1.3); }
100% { transform: scale(1); }
}
5. Toggle Switch Moderne
Le toggle switch est idéal pour les paramètres on/off. Voici une version moderne avec une animation fluide utilisant un cubic-bezier pour un effet de rebond.
.toggle-switch {
display: flex;
align-items: center;
gap: 12px;
cursor: pointer;
}
.toggle-switch input {
display: none;
}
.toggle-track {
width: 56px;
height: 30px;
background: rgba(255, 255, 255, 0.1);
border-radius: 100px;
position: relative;
transition: background 0.3s ease;
}
.toggle-thumb {
position: absolute;
top: 3px;
left: 3px;
width: 24px;
height: 24px;
background: white;
border-radius: 50%;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
/* Effet de rebond */
transition: left 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
/* Etat active */
.toggle-switch input:checked ~ .toggle-track {
background: #6366f1;
}
.toggle-switch input:checked ~ .toggle-track .toggle-thumb {
left: 29px;
}
Bonnes Pratiques et Accessibilité
Créer des inputs beaux ne suffit pas : ils doivent aussi être accessibles et performants.
Accessibilité
- Toujours associer label et input avec
foretid - Ne jamais cacher l'input avec
display: nonesans alternative (utilisezopacity: 0avecposition: absolutepour garder la navigation clavier) - Contrast suffisant : les couleurs de validation doivent être visibles
- Focus visible : gardez toujours un indicateur de focus clair
/* Respect des preferences utilisateur */
@media (prefers-reduced-motion: reduce) {
.floating-input,
.floating-label,
.border-input-line,
.checkbox-box,
.toggle-thumb {
transition: none;
}
.validation-input.error {
animation: none;
}
}
/* Focus visible pour navigation clavier */
.custom-checkbox input:focus-visible ~ .checkbox-box,
.custom-radio input:focus-visible ~ .radio-circle,
.toggle-switch input:focus-visible ~ .toggle-track {
outline: 2px solid #6366f1;
outline-offset: 2px;
}
Performance
- Préférez
transformàtop/leftpour les animations - Utilisez
will-changeavec parcimonie sur les éléments animés - Évitez les box-shadow complexes sur de nombreux éléments
Pour les checkboxes et radios custom, la meilleure pratique est d'utiliser position: absolute et opacity: 0 plutôt que display: none pour conserver l'accessibilité clavier.
Conclusion
Les inputs animés transforment l'expérience utilisateur de vos formulaires. Avec ces 5 techniques, vous avez tout ce qu'il faut pour créer des interfaces modernes et engageantes.
N'oubliez pas que l'animation doit servir l'UX, pas la desservir. Gardez vos effets subtils, performants et accessibles. Vos utilisateurs vous remercieront !
Retrouvez des dizaines de composants de formulaire prêts à l'emploi dans notre bibliothèque d'effets, avec code copiable en un clic.





