Alert boxes are useful for providing important messages or notifications to users that are time-sensitive or temporary. Instead of requiring users to manually dismiss the alert, the auto-close feature automatically hides the alert after a specified duration. With the help of Bootstrap, you can display any kind of alert notifications but users need to dismiss them manually.
This lightweight code snippet allows you to automatically hide a Bootstrap alert with a fading effect. You just need to add the class ‘auto-close’ to the Bootstrap alert component, and it will disappear smoothly. You can set the closing time according to your needs.
How to Create Bootstrap 5 Alert with Auto Close Feature
1. First of all, load the Bootstrap 5 CSS by adding the following CDN link into the <head> tag of your website.
<!-- Bootstrap 5 CSS --> <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
2. Create a Bootstrap alert component, add your content inside it, and add an additional class “auto-close” to it.
<div class="auto-close alert alert-success" role="alert"> <h4 class="alert-heading">Well done!</h4> <p>Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.</p> <hr> <p class="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p> </div>
3. If you need to apply additional styling, use the following CSS styles:
.auto-close{ /* Your custom CSS goes here.. */ }
4. Now, load the Bootstrap 5 JS by adding the following CDN link just before closing the <body> element.
<!-- Bootstrap 5 JS --> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
5. Finally, use the following JavaScript function to automatically close the alert message with fading animation. You can set the custom time duration in setTimeOut function after that the alert message should be closed.
// Get all elements with class "auto-close" const autoCloseElements = document.querySelectorAll(".auto-close"); // Define a function to handle the fading and sliding animation function fadeAndSlide(element) { const fadeDuration = 500; const slideDuration = 500; // Step 1: Fade out the element let opacity = 1; const fadeInterval = setInterval(function () { if (opacity > 0) { opacity -= 0.1; element.style.opacity = opacity; } else { clearInterval(fadeInterval); // Step 2: Slide up the element let height = element.offsetHeight; const slideInterval = setInterval(function () { if (height > 0) { height -= 10; element.style.height = height + "px"; } else { clearInterval(slideInterval); // Step 3: Remove the element from the DOM element.parentNode.removeChild(element); } }, slideDuration / 10); } }, fadeDuration / 10); } // Set a timeout to execute the animation after 5000 milliseconds (5 seconds) setTimeout(function () { autoCloseElements.forEach(function (element) { fadeAndSlide(element); }); }, 5000);
That’s it! You’ve now successfully implemented Bootstrap 5 Alert Auto Close. Feel free to comment below if you have any questions or suggestions.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.
Excelente contribución, muchas gracias!!!
This demo works when the page loads with the alert already in place, which doesn’t happen as often as an alert that pops up due to a trigger event while the page is already loaded. Refactoring the code to adapt it for a wider use case will add more value.