
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to create a snackbar / toast with CSS and JavaScript?
To create a snackbar/toast with CSS and JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .snackText { visibility: hidden; min-width: 250px; margin-left: -125px; background-color: rgb(110, 26, 106); color: #fff; text-align: center; border-radius: 2px; padding: 16px; position: fixed; z-index: 1; left: 50%; bottom: 30px; font-size: 17px; } .snackBtn { border: none; padding: 10px; font-size: 18px; background-color: rgb(92, 0, 128); color: white; } .snackText.show { visibility: visible; animation: fadein 0.5s, fadeout 0.5s 2.5s; } @keyframes fadein { from { bottom: 0; opacity: 0; } to { bottom: 30px; opacity: 1; } } @keyframes fadeout { from { bottom: 30px; opacity: 1; } to { bottom: 0; opacity: 0; } } </style> </head> <body> <h1>Snackbar / Toast Example</h1> <button class="snackBtn">Show Snackbar</button> <h2>Click on the above button to see snackbar message</h2> <div class="snackText">Some text some message..</div> <script> document .querySelector(".snackBtn") .addEventListener("click", showSnackBar); function showSnackBar() { var x = document.querySelector(".snackText"); x.className += " show"; setTimeout(function() { x.className = x.className.replace("show", ""); }, 3000); } </script> </body> </html>
Output
The above code will produce the following output −
On clicking the “Show Snackbar” button −
- Related Articles
- Creating Snackbar using HTML, CSS and Javascript
- How to create tabs with CSS and JavaScript?
- How to create popups with CSS and JavaScript?
- How to create a responsive slideshow with CSS and JavaScript?
- How to create a Modal Box with CSS and JavaScript?
- How to create a scroll indicator with CSS and JavaScript?
- How to create a collapsible section with CSS and JavaScript?
- How to create a tree view with CSS and JavaScript?
- How to create a quotes slideshow with CSS and JavaScript?
- How to create a "to-do list" with CSS and JavaScript?
- How to create tab headers with CSS and JavaScript?
- How to create a vertical tab menu with CSS and JavaScript?
- How to create a clickable dropdown menu with CSS and JavaScript?
- How to create a curtain navigation menu with CSS and JavaScript?
- How to create a collapsible sidebar menu with CSS and JavaScript?

Advertisements