Page 1 of 1

Did you know that TreinaWeb is the most complete

Posted: Sun Feb 02, 2025 3:36 am
by monira444
Creating a snackbar component with JavaScript and CSS
The snackbar component is one of the most used in Front-end. With it, we developers can send important information to users. It is an elegant way to notify, for example, that the user has correctly submitted the information to a form, or even notifications about registration. In this article, I will teach you in a simple way how you can create your own snackbar.


First, you need a structure, you will need to create the HTML , after this structure, we will style and create the events with JavaScript . For this example, I will create a button, it will be the trigger that will fire the event that will show the snackbar component. I will create the component with only a text field.

This structure was created with the idea of ​​how to capture these elements in JavaScript in mind, which is why I've already added the IDs to each of the elements.


Styling the element
Now that we have the basic HTML structure, let's style it to linkedin data create an attractive look for our snackbar. Here is a basic styling example. Keep in mind that styling varies from project to project, so you are free to make the snackbar component look however you see fit.

Let's understand better what is happening here. Initially, the snackbar component needs to be hidden, so the property is used visibility: hidden;, since we only want to show it when the button is clicked.


The position, bottom, leftand properties transformare used to ensure that the element always remains in the center of the screen at the bottom. One point here is that you can, if necessary, leave the snackbar component wherever you prefer. Moving it only as necessary.

If you want to have complete mastery of position types and how to control them, in the HTML5 + CSS3 - Box model and Element Positioning course I show you in detail how to do it.

After that, I set a fixed width and changed the background color, text color, text size, positioning, inner padding and rounded the edges. Again, these stylings do not impact the behavior of the component, they just serve to make it look nicer.

Styled the way I wanted, just change the visibility of the element when it has the class show. This class will change the visibility and add an animation that will make the snackbar component appear and go away smoothly.

Adding Behavior with JavaScript

Now, let's add the JavaScript logic to make the snackbar appear when the button is clicked:

This JavaScript code selects the button by its ID and adds a click event. When the button is clicked, it adds the “show” class to the snackbar, making it appear. Remember that, when it appears, the component will start the animation we created with CSS. Then, the setTimeout function is used to remove the “show” class after 3 seconds, making the snackbar disappear.

With that, you now have a basic snackbar working on your page. Of course, you can customize the style and behavior as needed to suit your specific needs. If you followed the steps below, you should have something similar to this:

In short, the snackbar is one of the most widely used front-end components, offering developers an elegant way to communicate crucial information to users. In this article, we start by creating the HTML structure and styling, and then add dynamic behavior with JavaScript.

This simplified approach not only provides an understanding of the snackbar creation process, but also the flexibility to customize it according to the specific requirements of the project. This gives the developer the ability to effectively integrate this fundamental component into their web interfaces, enhancing the user experience in an efficient and enjoyable way.