Back
A step-by-step guide to implementing snackbars that are interactive, engaging, and highly customizable.
To start with designing a customizable snackbar, you'll need a simple HTML structure that includes the snackbar itself and a trigger button to display it. The basic HTML element for the snackbar is a <div>
with an ID attribute set to "snackbar". This <div>
will serve as the container for the notification message. Also, a <button>
is needed to trigger the snackbar's display.
<div id="snackbar" class="snackbar">Sample Snackbar Notification</div> <button onclick="showSnackbar()" class="snackbar-button">Show Snackbar</button>
The <div>
element holds the text for your notification and the button initiates the function that shows the snackbar. You can customize the text within the snackbar to fit your application's needs.
The JavaScript function showSnackbar()
handles the display logic for our snackbar.
function showSnackbar() { const snackbar = document.getElementById('snackbar'); snackbar.classList.add('show'); setTimeout(() => { snackbar.classList.remove('show'); }, 3000); }
In this script, the snackbar is displayed by adding the show
class to it, which is predefined in the CSS to make the snackbar visible. The snackbar is automatically hidden after 3000 milliseconds (or 3 seconds) using setTimeout()
, which removes the show
class.
The CSS styles are crucial for the snackbar's appearance and behavior. Below is a CSS snippet that provides both the design and animation for revealing and hiding the snackbar.
.snackbar { visibility: hidden; min-width: 250px; margin-left: -125px; background-color: #333; color: #fff; text-align: center; border-radius: 2px; padding: 16px; position: fixed; left: 50%; bottom: 30px; font-size: 17px; transition: opacity 0.5s, visibility 0.5s; } .snackbar.show { visibility: visible; opacity: 0.9; }
Initially, the snackbar is invisible because the visibility
property is set to hidden
. Once the show
class is added, visibility
changes to visible
and opacity
is set to create a fade-in effect.
Navigating further, the button to trigger the snackbar is designed to be simple and effective:
.snackbar-button { padding: 10px 20px; font-size: 16px; background-color: #333; color: #fff; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } .snackbar-button:hover { background-color: #444; }
The button's style enhances the user's experience with a smooth color transition effect on hover, complementing the snackbar's modern aesthetic.
Your current setup features a sleek and simple snackbar design that can be further developed to fit the style and functionality of your web application.
To enhance the basic snackbar you've just implemented, there are numerous ways in which customization can provide additional utility and flair:
<div>
so that links or formatted text can be displayed.By applying these customization options, you can maximize the versatility and impact of snackbars in your web applications, ensuring both aesthetics and functionality are aligned with user expectations and business goals.
<div id="snackbar" class="snackbar">Sample Snackbar Notification</div><button onclick="showSnackbar()" class="snackbar-button">Show Snackbar</button> <script> function showSnackbar() { const snackbar = document.getElementById('snackbar'); snackbar.classList.add('show'); setTimeout(() => { snackbar.classList.remove('show'); }, 3000); } </script> <style> .snackbar { visibility: hidden; min-width: 250px; margin-left: -125px; background-color: #333; color: #fff; text-align: center; border-radius: 2px; padding: 16px; position: fixed; z-index: 1; left: 50%; bottom: 30px; font-size: 17px; transition: opacity 0.5s, visibility 0.5s; } .snackbar.show { visibility: visible; opacity: 0.9; } .snackbar-button { padding: 10px 20px; font-size: 16px; background-color: #333; color: #fff; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } .snackbar-button:hover { background-color: #444; } </style>Thank you for reading this article.
Comments