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-toggle
andtitle
attributes to specify the tooltip's behavior and content. Thedata-toggle="tooltip"
attribute initializes the tooltip, and thetitle
attribute 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.,500
for 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.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.