Bootstrap Toast Example on CodePen

Ruth Jul 09, 2026

Bootstrap, a popular CSS framework, simplifies responsive web development by providing pre-defined classes and components. One such component is the toast, a lightweight notification that appears briefly at the bottom of the screen. Let's explore a Bootstrap toast example using CodePen, a web development editor that allows real-time collaboration and sharing.

Jquery Toast Type Notification Popup Design
Jquery Toast Type Notification Popup Design

Before diving into the CodePen example, ensure you have a basic understanding of Bootstrap's grid system and components. Familiarize yourself with the Bootstrap documentation to grasp the framework's fundamentals.

an image of a computer screen with text
an image of a computer screen with text

Setting Up the Bootstrap Toast Example on CodePen

To get started, sign up for a free CodePen account and create a new Pen. CodePen provides a simple, three-panel interface: the editor (HTML, CSS, and JavaScript), the preview, and the settings panel.

two screens showing different types of text on the left and right sides of each screen
two screens showing different types of text on the left and right sides of each screen

For this example, we'll use the Bootstrap 5 CDN links provided by the Bootstrap official website. Add the following lines in the section of the HTML panel:

```html ```

Creating the Toast Notification

Tooltip Bootstrap 5 #shorts #coding #html
Tooltip Bootstrap 5 #shorts #coding #html

Now, let's create a simple toast notification using Bootstrap's toast component. In the HTML panel, add the following code within the section:

```html

```

In this example, we've created a button that triggers the toast notification. The toast itself is hidden by default and positioned at the bottom-right corner of the screen. The toast header contains the title and a close button, while the toast body holds the notification message.

Initializing the Toast and Adding Event Listener

Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5

Next, we'll initialize the toast using JavaScript and add an event listener to the button. In the JavaScript panel, add the following code:

```javascript const toastTrigger = document.getElementById('showToastBtn') const toastLiveExample = document.getElementById('liveToast') if (toastTrigger) { toastTrigger.addEventListener('click', () => { const toast = new bootstrap.Toast(toastLiveExample) toast.show() }) } ```

Here, we're selecting the button and toast elements using their IDs. When the button is clicked, we create a new toast instance and call the show() method to display the toast.

Customizing the Toast Notification

an image of a computer screen with some text on it
an image of a computer screen with some text on it

Bootstrap toasts are highly customizable. You can change their appearance, delay, and behavior using various options and data attributes. Let's explore some customization options.

Changing the Toast Header and Body

Progress bar using Bootstrap 5 #shorts #coding #html
Progress bar using Bootstrap 5 #shorts #coding #html
Warning
Warning
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
A Beginner's Guide to the Latest Bootstrap 5 Utilities
A Beginner's Guide to the Latest Bootstrap 5 Utilities
two screens showing the different settings for an email address to someone on their phone or laptop
two screens showing the different settings for an email address to someone on their phone or laptop
Toast with TailwindCss design
Toast with TailwindCss design
two screenshots showing the same texting and an uploading option for another one
two screenshots showing the same texting and an uploading option for another one
Building a toast component
Building a toast component
three different types of conversation bubbles with the text, did you know? here is something that you might like to know
three different types of conversation bubbles with the text, did you know? here is something that you might like to know
three options to choose the right one for your website's privacy policy and security
three options to choose the right one for your website's privacy policy and security
two texts are shown in the same language as one is being sent to another person
two texts are shown in the same language as one is being sent to another person
GoSnippets — Free Code Snippets for Bootstrap, Tailwind, React, CSS, JavaScript & more
GoSnippets — Free Code Snippets for Bootstrap, Tailwind, React, CSS, JavaScript & more
Bootstrap 5 Classes List Cheat Sheet
Bootstrap 5 Classes List Cheat Sheet
Toast Message With Close Animation Using CSS & jQuery | Info Messages
Toast Message With Close Animation Using CSS & jQuery | Info Messages
an email form with the text paste contact next to it, and then click on
an email form with the text paste contact next to it, and then click on
the screens are displaying different types of buttons
the screens are displaying different types of buttons
ICONS IN CSS
ICONS IN CSS
Bootstrap 4 Cheat Sheet - The ultimate list of Bootstrap classes
Bootstrap 4 Cheat Sheet - The ultimate list of Bootstrap classes
HTML/CSS Bootstrap Simple Success Confirmation Popup
HTML/CSS Bootstrap Simple Success Confirmation Popup
Difference Between Bootstrap and CSS
Difference Between Bootstrap and CSS

To modify the toast header and body content, simply update the toast-header and toast-body elements in the HTML. For example, change the title and message as follows:

```html

Custom Toast 5 seconds ago
This is a custom toast notification with a different title and message.
```

Adjusting the Toast Delay and Autohiding

By default, toasts are displayed for 5 seconds and then automatically hide. You can adjust the delay or prevent the toast from hiding automatically using the data-bs-delay and data-bs-autohide attributes. Update the toast element as follows:

```html

```

In this example, the toast will remain visible for 3 seconds (3000 milliseconds) and won't hide automatically. To hide the toast manually, add an event listener for the close button:

```javascript document.querySelector('.toast .btn-close').addEventListener('click', () => { toastLiveExample.classList.add('hide') }) ```

Now that you've learned how to create and customize Bootstrap toasts using CodePen, you can integrate this functionality into your web projects. Experiment with different toast options and data attributes to create engaging and informative notifications for your users.

Happy coding! Don't forget to share your Bootstrap toast creations on CodePen and showcase your skills to the web development community.