Back
Tutorial on how to create a snackbar with close button.
<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>
function openSnackbar() { let snackbar = document.getElementById('snackbar') snackbar.classList.add('active') } function closeSnackbar() { let snackbar = document.getElementById('snackbar') snackbar.classList.remove('active') }
#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; }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; }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; }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); }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; }
<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> <script> function openSnackbar() { let snackbar = document.getElementById('snackbar') snackbar.classList.add('active') } function closeSnackbar() { let snackbar = document.getElementById('snackbar') snackbar.classList.remove('active') } </script> <style> #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; } .active { display: flex !important; } .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; } .close_snackbar:hover { transition: .2s; background-color: rgba(255, 255, 255, 0.5); } .open_snackbar_button { background-color: #3BBBA0; border: none; border-radius: 3px; color: #fff; padding: 5px 20px; cursor: pointer; font-size: 24px; } </style>Thank you for reading this article.