Back
A comprehensive tutorial on creating an elegant dark-themed snackbar that appears on command and automatically dismisses after 3 seconds.
<button onclick="openSnackbar()">Open snackbar</button> <div id="snackbar"> <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit</span> </div>
function openSnackbar() { let snackbar = document.getElementById('snackbar') // Activate snackbar snackbar.classList.add('active') setTimeout(() => { // Deactivate snackbar (after 3000ms - 3 seconds) snackbar.classList.remove('active') }, 3000); }
#snackbar { position: absolute; bottom: 0; left: 50%; transform: translateX(-50%); margin: 10px; background-color: #333; padding: 10px 20px; width: 200px; border-radius: 5px; color: rgb(249, 129, 129); display: none; }Finally, we need to style the "active" class that will be toggled by our JavaScript function. When this class is applied, we want to override the display property and make the snackbar visible by setting it to block. The !important rule is necessary to ensure our style takes precedence over the ID selector's higher specificity.
.active { display: block !important; }And with that, you have a fully functional, elegant dark snackbar notification system that can be easily integrated into any web application.
<button onclick="openSnackbar()">Open snackbar</button> <div id="snackbar"> <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit</span> </div> <script> function openSnackbar() { let snackbar = document.getElementById('snackbar') snackbar.classList.add('active') setTimeout(() => { snackbar.classList.remove('active') }, 3000); } </script> <style> #snackbar { position: absolute; bottom: 0; left: 50%; transform: translateX(-50%); margin: 10px; background-color: #333; padding: 10px 20px; width: 200px; border-radius: 5px; color: rgb(249, 129, 129); display: none; } .active { display: block !important; } </style>Thank you for reading this article.
Comments
No comments yet. Be the first to comment!