Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2272

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T03:52:07+00:00 2024-11-26T03:52:07+00:00

How to create a snackbar wth close button – Tutorial

  • 61k

Tutorial on how to create a snackbar with close button.

HTML

For HTML, first we need a button to open the snackbar. We'll set onclick event and run “openSnackbar()” fuction.

And we'll need a snackbar element, with snackbar text and button to close the snackbar.

We'll set onclick lister to a button and fire the “closeSnackbar()” function.

Inside the button, we'll place the “X” svg.

<button class="open_snackbar_button" onclick="openSnackbar()">Open snackbar</button>  <div id="snackbar">     <p>You didn't share this video</p>     <button class="close_snackbar" onclick="closeSnackbar()">         <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="#fff" width="20" height="20">             <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />         </svg>     </button> </div> 
Enter fullscreen mode Exit fullscreen mode

Javascript

For javascript, we need to create two functions, one to open and the other to close the snackbar.

We'll just remove the “active” class from the snackbar in “closeSnackbar()” function and add it in “openSnackbar()” function.

We'll use this class to hide/show the snackbar.

function openSnackbar() {     let snackbar = document.getElementById('snackbar')     snackbar.classList.add('active') }  function closeSnackbar() {     let snackbar = document.getElementById('snackbar')     snackbar.classList.remove('active') } 
Enter fullscreen mode Exit fullscreen mode

CSS

For CSS, first we'll style the snackbar.

We'll set it's position to fixed with bottom of 20px and left of 50% with transform translateX -50% which will place the snackbar in the middle, bottom of the page.

Now we'll set background color to red, to indicate that this in an error message.

Then we'll position the text and button using flexbox.

We'll add some paddings, set border radius to 5 pixels, width to 300 pixels, and change font and set it's color to white.

And set display property to none to hide the element.

#snackbar {     position: fixed;     bottom: 20px;     left: 50%;     transform: translateX(-50%);     background-color: #FF5058;     justify-content: space-between;     align-items: center;     padding: 5px 20px;     border-radius: 5px;     width: 300px;     font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;     color: #fff;     display: none; } 
Enter fullscreen mode Exit fullscreen mode

Now we'll overwrite the display property of the snackbar using “active” class and set it to flex with “!important” rule in order to overwrite the value.

.active {     display: flex !important; } 
Enter fullscreen mode Exit fullscreen mode

Now we'll style the close button.

We'll set width and height to 20 pixels, border radius to 50% to create a circle and box-sizing to content-box.

Also, we'll add 2px solid white border and set background color to transparent white.

Lastly, we'll set cursor to pointer and add a little transition (because we'll add hover effect).

.close_snackbar {     width: 20px;     height: 20px;     border-radius: 50%;     box-sizing: content-box;     padding: 6px;     border: 2px solid #fff;     background-color: rgba(255, 255, 255, 0.2);     cursor: pointer;     transition: .2s; } 
Enter fullscreen mode Exit fullscreen mode

Now we'll style the close button on hover.

We'll add a little transition and decrease the transparency of the background.

.close_snackbar:hover {     transition: .2s;     background-color: rgba(255, 255, 255, 0.5); } 
Enter fullscreen mode Exit fullscreen mode

Lastly, we'll style the button which opens the snackbar.

We'll set background color to green, remove the border, set border radius to 3 pixels, text color to white and size to 24 pixels and cursor to pointer.

.open_snackbar_button {     background-color: #3BBBA0;     border: none;     border-radius: 3px;     color: #fff;     padding: 5px 20px;     cursor: pointer;     font-size: 24px; } 
Enter fullscreen mode Exit fullscreen mode

And that's it.

You can find video tutorial and full code here.

Thank you for reading. ❤️

cssjavascripttutorialwebdev
  • 0 0 Answers
  • 9 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.