Tooltips Bootstrap
In Bootstrap 4, tooltips are small pop-ups that provide additional information when users hover over or focus on an element. They can be very helpful for enhancing user experience by giving contextual hints or instructions. Here’s a detailed explanation with examples, properly indented:
Basic Usage
To use tooltips in Bootstrap 4, you need to follow these steps:
-
Include Bootstrap CSS and JavaScript: Make sure you have Bootstrap CSS and JavaScript files included in your project. You can use Bootstrap's CDN for this:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> -
Add the Tooltip HTML: Use the
data-toggleandtitleattributes to specify the tooltip's behavior and content. Thedata-toggle="tooltip"attribute initializes the tooltip, and thetitleattribute sets the tooltip's text.<button type="button" class="btn btn-secondary" data-toggle="tooltip" title="Tooltip text"> Hover me </button> -
Initialize Tooltips with JavaScript: Bootstrap tooltips require initialization with JavaScript. You can do this using jQuery:
<script> $(document).ready(function(){ $('[data-toggle="tooltip"]').tooltip(); }); </script>
Complete Example
Here’s a complete example of a Bootstrap 4 tooltip in action:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap 4 Tooltip Example</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<button type="button" class="btn btn-primary" data-toggle="tooltip" title="This is a tooltip">
Hover over me
</button>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
</script>
</body>
</html>
Customization
You can customize the appearance and behavior of tooltips using additional options such as placement, trigger, and delay. Here’s an example with some custom options:
<button type="button" class="btn btn-info" data-toggle="tooltip" data-placement="right" title="Tooltip on the right">
Hover me
</button>
data-placement: Defines the position of the tooltip (e.g.,top,bottom,left,right).data-trigger: Defines how the tooltip is triggered (e.g.,hover,focus,click).data-delay: Specifies the delay before showing or hiding the tooltip (e.g.,500for 500 milliseconds).
Tooltips are a powerful feature in Bootstrap 4 that can improve the usability of your site by providing helpful hints and context to users.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
