Aprender a crear mensajes de alerta con CSS.
alertas
Los mensajes de alerta se pueden utilizar para notificar al usuario acerca de algo especial: peligro, el éxito, la información o advertencia.
Crear una alerta de mensaje
Paso 1) Añadir HTML:
Ejemplo
<div class="alert">
<span class="closebtn"
onclick="this.parentElement.style.display='none';">×</span>
This is an alert box.
</div>
Si desea que la capacidad de cerrar el mensaje de alerta, añadir un <span> elemento con un onclick
atributo que dice "when you click on me, hide my parent element" - que es el contenedor <div> (class="alert") .
Consejo: Utilice la entidad HTML " ×
" para crear la letra "x" .
Paso 2) Añadir CSS:
Estilo del cuadro de alerta y el botón de cierre:
Ejemplo
/* The alert message box */
.alert {
padding: 20px;
background-color: #f44336; /* Red */
color: white;
margin-bottom: 15px;
}
/* The close button */
.closebtn {
margin-left: 15px;
color: white;
font-weight: bold;
float: right;
font-size: 22px;
line-height: 20px;
cursor: pointer;
}
.closebtn:hover {
color: black;
}
Inténtalo tú mismo " muchos alertas
Si tiene muchos mensajes de alerta en una página, puede añadir la siguiente secuencia de comandos para cerrar diferentes alertas sin usar el onclick atributo en cada <span> elemento.
Y, si se desea que las alertas a desvanecerse lentamente cuando se hace clic en ellos, agrega opacity
y la transition
a la alert
de clase:
Ejemplo
<style>
.alert {
opacity: 1;
transition: opacity
0.6s; // 600ms to 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++) {
// When someone clicks on a close button
close[i].onclick =
function(){
// Get the
parent of <span class="closebtn"> (<div class="alert">)
var div = this.parentElement;
// Set the opacity of div to
0 (transparent)
div.style.opacity = "0";
// Hide the div after 600ms
(the same amount of milliseconds it takes to fade out)
setTimeout(function(){ div.style.display = "none"; }, 600);
}
}
</script>
Inténtalo tú mismo "