How to use Toasts component in
Bootstrap? The following article will introduce to you the usage of the toast message Toasts component in Bootstrap5. I hope it will be helpful to you!
1 Toasts Example
Toasts is a lightweight notification designed to mimic mobile and desktop operating systems Push notifications have become popular. They are built with flexbox so they are easy to align and position. [Related recommendations: "bootstrap tutorial"]
Like the pop-up prompt, the toast message also needs to be initialized by itself. I don’t know why the initialization method on the official website is invalid. I found a feasible method on a foreign website. method.
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>Popovers</title> </head> <body> <div> <br><br><br><br> <button type="button" class="btn btn-primary" id="liveToastBtn">显示吐司消息</button> <div class="position-fixed bottom-0 end-0 p-3" style="z-index: 5"> <div id="liveToast" class="toast hide" data-bs-animation="false" role="alert" aria-live="assertive" aria-atomic="true"> <div> <strong>吐司消息提示</strong> <small>11 mins ago</small> <button type="button" data-bs-dismiss="toast" aria-label="Close"></button> </div> <div> 你有一条短消息! </div> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> <script> document.querySelector("#liveToastBtn").onclick = function() { new bootstrap.Toast(document.querySelector('.toast')).show(); } </script> </body> </html>
2 Setting options
Options can be passed through data attributes or JavaScript. For data attributes, append the option name to data-bs-, such as: data-bs-animation="".
- data-bs-animation="true" Apply CSS fade transition effect to toast
- data-bs-autohide="true" Automatically hide toast
- data-bs-delay="5000", delay hiding toast for 5s (default unit: milliseconds)
The above values are the default values. If you are satisfied with the grinding effect, there is no need to write it at all. Well, in the 27.3.1 example, I set data-bs-autohide="false" to not automatically hide the toast, so that it is convenient to take screenshots, otherwise the message box will disappear as long as the mouse is clicked anywhere.
3 Translucent
Toasts can also be translucent and therefore blend in with anything they may appear on. Browsers that support the CSS attribute background-filter will also try to blur the elements below the toast.
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>吐司消息</title> </head> <body> <div> <br><br><br><br> <button type="button" class="btn btn-primary" id="liveToastBtn">显示吐司消息</button> <div role="alert" aria-live="assertive" aria-atomic="true"> <div> <strong>半透明吐司</strong> <small>11 mins ago</small> <button type="button" data-bs-dismiss="toast" aria-label="Close"></button> </div> <div> 你有一条短消息! </div> </div> </div> <script ></script> <script> document.querySelector("#liveToastBtn").onclick = function() { new bootstrap.Toast(document.querySelector('.toast')).show(); } </script> </body> </html>
4 Stacking
You can stack toasts by packaging them in toast-container
containers, which will Add some spacing vertically.
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>吐司消息</title> </head> <body> <div> <br><br><br><br> <button type="button" class="btn btn-primary" id="liveToastBtn1">显示吐司消息1</button> <button type="button" class="btn btn-primary" id="liveToastBtn2">显示吐司消息2</button> <div> <div id="toast1" role="alert" aria-live="assertive" aria-atomic="true"> <div> <strong>吐司消息</strong> <small>刚刚发送</small> <button type="button" data-bs-dismiss="toast" aria-label="Close"></button> </div> <div> 第一条消息 </div> </div> <div id="toast2" role="alert" aria-live="assertive" aria-atomic="true"> <div> <strong>吐司消息</strong> <small>2分钟前</small> <button type="button" data-bs-dismiss="toast" aria-label="Close"></button> </div> <div> 第二条消息 </div> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> <script> document.querySelector("#liveToastBtn1").onclick = function() { new bootstrap.Toast(document.querySelector('#toast1')).show(); } document.querySelector("#liveToastBtn2").onclick = function() { new bootstrap.Toast(document.querySelector('#toast2')).show(); } </script> </body> </html>
5 Custom content
Customize toast by removing child components, adjusting common classes, or adding markup.
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>吐司消息</title> </head> <body> <div> <br><br><br><br> <button type="button" class="btn btn-primary" id="liveToastBtn">显示吐司消息</button> <div role="alert" aria-live="assertive" aria-atomic="true"> <div> 邀请你穿越到三国! <div class="mt-2 pt-2 border-top"> <button type="button" class="btn btn-primary btn-sm">接受邀请</button> <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="toast">关闭</button> </div> </div> </div> </div> <script ></script> <script> document.querySelector("#liveToastBtn").onclick = function() { new bootstrap.Toast(document.querySelector('.toast')).show(); } </script> </body> </html>
6 Color Scheme
Based on the above example, you can also create different toast color schemes through our color general categories. Next, we add bg-danger and text-white to toast, and then add text-white to the close button. In order to make the edges appear clearly, the default borders are removed via border-0.
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>吐司消息</title> </head> <body> <div> <br><br><br><br> <button type="button" class="btn btn-primary" id="liveToastBtn">显示吐司消息</button> <div class="toast align-items-center text-white bg-danger border-0" role="alert" aria-live="assertive" aria-atomic="true"> <div> <div> 这里是红色背景的 </div> <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> <script> document.querySelector("#liveToastBtn").onclick = function() { new bootstrap.Toast(document.querySelector('.toast')).show(); } </script> </body> </html>
7 Set the display position
The default toast message is displayed in the lower right corner of the browser. According to requirements, use custom CSS to specify the toast position. The top right corner is usually used for notifications, as is the top middle. If you only want to display one toast at a time, put the positioning style on the toast.

The above is the official example, Bootstrap5 ToastsI didn’t find the driver js code. But it can be given as a reference for everyone. If you are interested, you can study it. Here I have modified it to be displayed in the upper left corner based on the above code.
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content=""> <meta name="description" content=""> <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet"> <title>吐司消息</title> </head> <body> <div> <br><br><br><br> <button type="button" class="btn btn-primary" id="liveToastBtn">显示吐司消息</button> <div class="position-fixed top-0 start-0 p-3" style="z-index: 5"> <div id="liveToast" class="toast hide" data-bs-animation="false" role="alert" aria-live="assertive" aria-atomic="true"> <div> <strong>吐司消息提示</strong> <small>11 mins ago</small> <button type="button" data-bs-dismiss="toast" aria-label="Close"></button> </div> <div> 你有一条短消息! </div> </div> </div> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> <script> document.querySelector("#liveToastBtn").onclick = function() { new bootstrap.Toast(document.querySelector('.toast')).show(); } </script> </body> </html>
For more knowledge about bootstrap, please visit: bootstrap basic tutorial! !
The above is the detailed content of How to use Toasts component in Bootstrap? (example explanation). For more information, please follow other related articles on the PHP Chinese website!

The reason for combining Bootstrap and React is their complementarity: 1. Bootstrap provides predefined styles and components to simplify UI design; 2. React improves efficiency and performance through component development and virtual DOM. Use it in conjunction to enjoy fast UI construction and complex interaction management.

Bootstrap is an open source front-end framework based on HTML, CSS and JavaScript, designed to help developers quickly build responsive websites. Its design philosophy is "mobile first", providing a wealth of predefined components and tools, such as grid systems, buttons, forms, navigation bars, etc., simplifying the front-end development process, improving development efficiency, and ensuring the responsiveness and consistency of the website. Using Bootstrap can start with a simple page and gradually add advanced components such as cards and modal boxes. Best practices for optimizing performance include customizing Bootstrap, using CDNs, and avoiding overuse of class names.

React and Bootstrap can be seamlessly integrated to enhance user interface design. 1) Install dependency package: npminstallbootstrapreact-bootstrap. 2) Import the CSS file: import'bootstrap/dist/css/bootstrap.min.css'. 3) Use Bootstrap components such as buttons and navigation bars. With this combination, developers can leverage React's flexibility and Bootstrap's style library to create a beautiful and efficient user interface.

The steps to integrate Bootstrap into a React project include: 1. Install the Bootstrap package, 2. Import the CSS file, 3. Use Bootstrap class name to style elements, 4. Use React-Bootstrap or reactstrap library to use Bootstrap's JavaScript components. This integration utilizes React's componentization and Bootstrap's style system to achieve efficient UI development.

Bootstrapisapowerfulframeworkthatsimplifiescreatingresponsive,mobile-firstwebsites.Itoffers:1)agridsystemforadaptablelayouts,2)pre-styledelementslikebuttonsandforms,and3)JavaScriptcomponentssuchascarouselsforenhancedinteractivity.

Bootstrap is a front-end framework developed by Twitter that integrates HTML, CSS and JavaScript to help developers quickly build responsive websites. Its core functions include: Grid system and layout: based on 12-column design, using flexbox layout, and supporting responsive pages of different device sizes. Components and styles: Provide a rich library of component, such as buttons, modal boxes, etc., and you can achieve beautiful effects by adding class names. How it works: Rely on CSS and JavaScript, CSS uses LESS or SASS preprocessors, and JavaScript relies on jQuery to achieve interactive and dynamic effects. Through these features, Bootstrap greatly improves development

BootstrapisafreeCSSframeworkthatsimplifieswebdevelopmentbyprovidingpre-styledcomponentsandJavaScriptplugins.It'sidealforcreatingresponsive,mobile-firstwebsites,offeringaflexiblegridsystemforlayoutsandasupportivecommunityforlearningandcustomization.

Bootstrapisafree,open-sourceCSSframeworkthathelpscreateresponsive,mobile-firstwebsites.1)Itoffersagridsystemforlayoutflexibility,2)includespre-styledcomponentsforquickdesign,and3)ishighlycustomizabletoavoidgenericlooks,butrequiresunderstandingCSStoop


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
