Comment créer : message d'alerte
- Page précédente Carte de produit
- Page suivante Boîte de dialogue
Apprendre à utiliser CSS pour créer des messages d'alerte.
Avertissement
Les messages d'alerte peuvent être utilisés pour informer l'utilisateur de certains événements spéciaux : dangereux, réussi, information ou avertissement.
Créer un message d'alerte
Première étape - Ajouter HTML :
<div class="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">×</span> Ceci est une boîte d'alerte. </div>
Si vous souhaitez pouvoir fermer le message d'alerte, ajoutez un onclick
propriété <span>
élément, cette propriété signifie "quand vous cliquez sur moi, masquez mon élément parent" - c'est-à-dire le conteneur <div class="alert">
。
Astuce :Utilisez le symbole HTML "×" pour créer le letter "x".
Deuxième étape - Ajouter CSS :
Set the styles for the warning box and close button:
/* Warning message box */ .alert { padding: 20px; background-color: #f44336; /* Red */ color: white; margin-bottom: 15px; } /* Close button */ .closebtn { margin-left: 15px; color: white; font-weight: bold; float: right; font-size: 22px; line-height: 20px; cursor: pointer; transition: 0.3s; } /* When the mouse moves over the close button */ .closebtn:hover { color: black; }
multiple warnings
If there are multiple warning messages on the page, you can add the following script to <span>
of the element onclick
properties to close different warnings.
And if you want the warnings to fade out slowly when clicked, add opacity and transition to alert
In class:
Example
<style> .alert { opacity: 1; transition: opacity 0.6s; /* 600 milliseconds fade-out */ } </style> <script> // Get all elements with class="closebtn" var close = document.getElementsByClassName("closebtn"); var i; // Loop through all close buttons for (i = 0; i < close.length; i++) { // Loop through all close buttons close[i].onclick = function(){ // Get the parent element of <span class="closebtn"> (<div class="alert">) var div = this.parentElement; // Set the opacity of div to 0 (transparent) div.style.opacity = "0"; // 600 milliseconds later hide div (same as fade-out time) setTimeout(function(){ div.style.display = "none"; }, 600); } } </script>
Pages associées
Tutoriel :Comment créer une étiquette
- Page précédente Carte de produit
- Page suivante Boîte de dialogue