


How to use tooltips in Bootstrap? A brief analysis of the usage of prompt components
How to use the prompt tool in
Bootstrap? The following article will introduce to you the usage of pop-up prompts and tooltip components in Bootstrap5. I hope it will be helpful to you!
Let’s talk about two controls: Popovers and Tooltips. These two components have a single function and are very simple to use. Lots of similarities. [Related recommendations: "bootstrap Tutorial"]
Popovers
1 Example
1.1 Notes
Things to note when using the popover plug-in:
- It must rely on bootstrap.bundle.min.js to work!
- For performance reasons, popovers are optional, so you must initialize them yourself.
1.2 Enable popovers everywhere
One way to initialize all popovers on the page is to select them via their data-bs-toggle attribute:
<!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-lg btn-danger" data-bs-toggle="popover" title="提示标题" data-bs-content="你想告诉别人些什么提示?你可以写在这里!"> 点此按钮弹出提示 </button> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> <script> var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]')) var popoverList = popoverTriggerList.map(function (popoverTriggerEl) { return new bootstrap.Popover(popoverTriggerEl) }) </script> </body> </html>
1.3 Using container options
When certain styles on the parent element interfere with the popover, you need to specify a custom container , so that the popover's HTML is displayed in that element. There is no difference in display between this one and the above one, except that an example-popover is added to the button class.
<!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-lg btn-danger example-popover" data-bs-toggle="popover" title="提示标题" data-bs-content="你想告诉别人些什么提示?你可以写在这里!"> 点此按钮弹出提示 </button> </div> <script ></script> <script> var popover = new bootstrap.Popover(document.querySelector('.example-popover'), { container: 'body' }) </script> </body> </html>
You can also use id, which seems more understandable
<!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" id="example-popover" class="btn btn-lg btn-danger" data-bs-toggle="popover" title="提示标题" data-bs-content="你想告诉别人些什么提示?你可以写在这里!"> 点此按钮弹出提示 </button> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> <script> var popover = new bootstrap.Popover(document.querySelector('#example-popover'), { container: 'body' }) </script> </body> </html>
2 Change the pop-up direction
us You can make the pop-up prompt information in four directions: top, right, bottom, left. It is also very simple to use. You only need to add data-bs-placement="position" to the button attribute, where the position can be top, bottom, left, or right.
It should be noted that the position to be displayed must have enough space, otherwise, it will automatically find a suitable position. If you set it to be displayed at the top, but it is already at the top of the browser, it will Shown below.
<!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-lg btn-danger" data-bs-toggle="popover" title="提示标题" data-bs-content="你想告诉别人些什么提示?你可以写在这里!"> 默认 </button> <button type="button" class="btn btn-lg btn-danger" data-bs-toggle="popover" data-bs-placement="top" title="提示标题" data-bs-content="你想告诉别人些什么提示?你可以写在这里!"> 上部 </button> <button type="button" class="btn btn-lg btn-danger" data-bs-toggle="popover" data-bs-placement="bottom" title="提示标题" data-bs-content="你想告诉别人些什么提示?你可以写在这里!"> 下部 </button> <button type="button" class="btn btn-lg btn-danger" data-bs-toggle="popover" data-bs-placement="left" title="提示标题" data-bs-content="你想告诉别人些什么提示?你可以写在这里!"> 左侧 </button> <button type="button" class="btn btn-lg btn-danger" data-bs-toggle="popover" data-bs-placement="right" title="提示标题" data-bs-content="你想告诉别人些什么提示?你可以写在这里!"> 右侧 </button> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> <script> var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]')) var popoverList = popoverTriggerList.map(function (popoverTriggerEl) { return new bootstrap.Popover(popoverTriggerEl) }) </script> </body> </html>
3 Close again anywhere
By default, click the button to display the prompt message , click this button again to hide the message, otherwise the message will always be displayed.
If we want to click anywhere again to close the previously displayed prompt information, we need to add a data-bs-trigger="focus"
attribute to the button, and add trigger: 'focus to the js file '
.
To achieve correct cross-browser and cross-platform behavior, the a tag must be used instead of the button tag, and the tabindex attribute must also be included.
<!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> <a tabindex="0" class="btn btn-lg btn-danger" role="button" data-bs-toggle="popover" data-bs-trigger="focus" title="提示标题" data-bs-content="你想告诉别人些什么提示?你可以写在这里!" > 点此按钮弹出提示,点击空白处提示消失 </a> </div> <script src="../bootstrap5/bootstrap.bundle.min.js" ></script> <script> var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]')) var popoverList = popoverTriggerList.map(function (popoverTriggerEl) { return new bootstrap.Popover(popoverTriggerEl) trigger: 'focus' }) </script> </body> </html>
Tooltips
Tooltips are very similar to Popovers and can also be If you choose to load it, you must initialize it yourself. Its display will also be Automatically adjust according to reserved space. Different from pop-up tips, tool tips will be displayed when the mouse hovers over the button, and will automatically hide when the mouse is moved away, without the need to click.
1 The tool tip validation code
is basically similar to the pop-up prompt. This code must be included in the page for the tool tip to take effect.
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')) var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) { return new bootstrap.Tooltip(tooltipTriggerEl) })
2 Tool tip example
Tool tips are generally used on buttons and links to explain their functions. Of course, they can also be used on on the picture. The value of title is the content displayed when the mouse is hovering, and you can use html elements.
The link has a default title attribute, and its prompt text is displayed in the browser status bar. This display is more intuitive.
<!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-secondary" data-bs-toggle="tooltip" title="这个是按钮提示"> 按钮提示 </button> <a href="#" data-bs-toggle="tooltip" title="这个是链接提示">链接提示</a> </div> <script ></script> <script> var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')) var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) { return new bootstrap.Tooltip(tooltipTriggerEl) }) </script> </body> </html>
3 Tooltip display position
Supports four tooltip directions just like pop-up tips , respectively top, bottom, left, right.
<!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-secondary" data-bs-toggle="tooltip" data-bs-placement="top" title="顶部提示"> 顶部提示 </button> <button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="right" title="右侧提示"> 右侧提示 </button> <button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="bottom" title="底部提示"> 底部提示 </button> <button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="left" title="左侧提示"> 左侧提示 </button> </div> <script ></script> <script> var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')) var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) { return new bootstrap.Tooltip(tooltipTriggerEl) }) </script> </body> </html>
4 Used for tips in articles
<!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> <div class="bd-example tooltip-demo"> <p> 最近<a href="#" data-bs-toggle="tooltip" title="哔哩哔哩">B站</a> 是迎来了自己12周年的纪念日, 之前吧,B站做过好些<a href="#" data-bs-toggle="tooltip" title="点此查看流行语盘点内容">流行语盘点</a>, 比如“awsl”一类的词,不少朋友都刷过,甚至有的<a href="#" data-bs-toggle="tooltip" title="相当于现代典故">梗</a>还出圈了, 像是后那个什么浪一类的,我留意到B站官方很多时候还会做一些相关的科普盘点啥的,时不时有推送。 </p> </div> </div> <script ></script> <script> var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')) var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) { return new bootstrap.Tooltip(tooltipTriggerEl) }) </script> </body> </html>
You can combine it with general classes to create more effects
The text part of both prompts can use the general classes of html and bootstrap to set the spacing, layout, fonts, colors, etc. You can try it yourself to create more Cool effect.
For more knowledge about bootstrap, please visit: bootstrap basic tutorial! !
The above is the detailed content of How to use tooltips in Bootstrap? A brief analysis of the usage of prompt components. For more information, please follow other related articles on the PHP Chinese website!

How to create responsive web applications using Bootstrap and React? By combining Bootstrap's CSS framework and React's componentized architecture, modern, flexible and easy to maintain can be created. The specific steps include: 1) Importing the CSS file of Bootstrap and using its class to style React components; 2) Using React's componentization to manage state and logic; 3) Loading Bootstrap styles as needed to optimize performance; 4) Creating a dynamic interface using React's Hooks and Bootstrap's JavaScript components.

Bootstrap is an open source front-end framework that helps developers quickly build responsive websites. 1) It provides predefined styles and components such as grid systems and navigation bars. 2) Implement style and dynamic interaction through CSS and JavaScript files. 3) The basic usage is to introduce files and build pages with class names. 4) Advanced usage includes custom styles through Sass. 5) Frequently asked questions include style conflicts and JavaScript component issues, which can be solved through developer tools and modular management. 6) Performance optimization is recommended to selectively introduce modules and rationally use grid systems.

React and Bootstrap are ideal combinations. 1) Use Bootstrap's CSS classes and JavaScript components, 2) integrate through React-Bootstrap or reactstrap, 3) load and optimize rendering performance on demand, and build an efficient and beautiful user interface.

Bootstrap is an open source front-end framework for creating modern, responsive, and user-friendly websites. 1) It provides grid systems and predefined styles to simplify layout and development. 2) Mobile-first design ensures compatibility and performance. 3) Through custom styles and components, the website can be personalized. 4) Performance optimization and best practices include selective loading and responsive images. 5) Common errors such as layout problems and style conflicts can be resolved through debugging techniques.

Bootstrap is an open source front-end framework developed by Twitter, suitable for building responsive websites quickly. 1) Its grid system is based on a 12-column structure, allowing for the creation of flexible layouts. 2) Responsive design function enables the website to adapt to different devices. 3) The basic usage includes building a navigation bar, and the advanced usage involves card components. 4) Common errors such as misuse of grid systems can be avoided by correctly setting the column width. 5) Performance optimization includes loading only necessary components, using CDN and file compression. 6) Best practices emphasize tidy code, custom styles and responsive design.

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.


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

Notepad++7.3.1
Easy-to-use and free code editor

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.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)
