Here’s a tutorial on how to create Bootstrap 5 Popover With Html Content. This method provides a way to display rich, interactive content on hover, enhancing user engagement and providing contextual information. Follow the steps below to implement this feature on your website.
Adding Header Assets
First, you need to include necessary CSS stylesheets in the <head> section of your HTML document. This ensures the visual styles are properly rendered.
<link href="https://fonts.googleapis.com/css?family=Cookie|Karla:400,700" rel="stylesheet"> <link rel="stylesheet" href="https://public.codepenassets.com/css/normalize-5.0.0.min.css">
Creating the HTML Structure
Next, create the basic HTML structure for your content. This includes the main content area and the anchor elements that will trigger the popovers. Use data-hover-content attribute to define the HTML content for the popover.
<div class="content">
<h1>Vincent van Gogh</h1>
<p>Vincent van Gogh, in full Vincent Willem van Gogh, (born March 30, 1853, <a href="#" data-hover-content="<div class='hover-content'>
<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/344846/500px-P1040705_copyGemeentehuis_Zundert.jpg' />
<p>Zundert is a municipality and town in the south of the Netherlands, in the province of North Brabant.</p>
</div>">Zundert, Netherlands</a> — died July 29, 1890, Auvers-sur-Oise, near <a href="#" data-hover-content="<div class='hover-content'>
<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/344846/eyfel-kulesi-paris.jpg' />
<p>Paris is the capital and most populous city of France.</p>
</div>">Paris, France</a>), Dutch painter, generally considered the greatest after <a href="#" data-hover-content="<div class='hover-content'>
<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/344846/Rembrandt.jpg' />
<p>Rembrandt Harmenszoon van Rijn (July 15, 1606 – October 4, 1669) was a Dutch draughtsman, painter and printmaker.</p>
</div>">Rembrandt van Rijn</a>, and one of the greatest of the <a href="#" data-hover-content="<div class='hover-content'>
<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/344846/Vincent-van-Gogh-The-Starry-Night-1889-2.jpg' />
<p>Post-Impressionism is an art movement that developed in the 1890s.</p>
</div>">Post-Impressionists.</a> The striking colour, emphatic brushwork, and contoured forms of his work powerfully influenced the current of <a href="#" data-hover-content="<div class='hover-content'>
<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/344846/Screen%20Shot%202019-04-20%20at%2012.04.29.png' />
<p>Expressionism is a modernist movement, initially in poetry and painting, originating in Germany at the beginning of the 20th century.</p>
</div>">Expressionism</a> in modern art. Van Gogh’s art became astoundingly popular after his death, especially in the late 20th century,
when his work sold for record-breaking sums at auctions around the world and was featured in blockbuster touring exhibitions. In part because of his extensive published letters, van Gogh has also been mythologized in the popular imagination as the
quintessential tortured artist.</p>
</div>
<span class="info">Please hover a link</span>
Styling with CSS
Now, define the CSS styles to control the appearance of your content and popovers. This will ensure they look visually appealing and consistent with your website’s design.
* {
box-sizing: border-box;
}
body {
background-color: #f5f5f5;
font-family: "Karla", sans-serif;
}
.content {
max-width: 550px;
padding: 40px;
margin: 40px auto;
}
.content > h1 {
margin: 0 0 20px 0;
font-family: "Cookie", monospace;
font-size: 60px;
}
.content > p {
line-height: 1.7;
margin: 0;
}
.content > p > a {
color: black;
font-weight: 700;
}
.content > p > a:hover {
background-color: #ffe0b2;
}
.hover-content {
padding: 10px;
box-shadow: 0 2px 20px rgba(0, 0, 0, 0.2);
box-sizing: border-box;
background-color: #fff;
width: 240px;
}
[data-hover-wrapper] {
transition: opacity 0.3s, transform 0.3s;
}
.hover-content > img {
max-width: 100%;
margin-bottom: 10px;
}
.hover-content > p {
font-size: 14px;
margin: 0;
line-height: 1.4;
}
.info {
position: fixed;
bottom: 0;
right: 0;
background-color: #000;
padding: 10px 20px;
color: #fff;
font-size: 13px;
}
Implementing JavaScript Functionality
Implement the JavaScript code to handle the popover behavior. This includes creating, positioning, and removing the popovers on mouseover, mousemove, and mouseleave events.
const links = document.getElementsByTagName("a");
[...links].forEach(link => {
link.addEventListener("mouseover", handleMouseOver);
link.addEventListener("mousemove", handleMouseMove);
link.addEventListener("mouseleave", handleMouseLeave);
});
function handlePosition(e) {
const ID = e.target.getAttribute("data-hover-id");
const wrapper = document.getElementById(ID);
let top = "";
if (
!(e.target.getBoundingClientRect().top + wrapper.offsetHeight > innerHeight))
{
top = `${e.clientY + e.target.offsetHeight}px`;
} else {
top = `${e.clientY - (wrapper.offsetHeight + e.target.offsetHeight)}px`;
}
return `position: fixed; left: ${e.clientX -
wrapper.offsetWidth / 2}px; top:${top}`;
}
function handleMouseOver(e) {
const hoverContent = e.target.getAttribute("data-hover-content");
const ID = Math.random().
toString(36).
substr(2, 9);
const wrapper = document.createElement("DIV");
e.target.setAttribute("data-hover-id", ID);
wrapper.setAttribute("data-hover-wrapper", "");
wrapper.setAttribute("id", ID);
wrapper.setAttribute("style", "opacity: 0; transform: scale(.8)");
wrapper.innerHTML = hoverContent;
document.body.append(wrapper);
wrapper.setAttribute("style", handlePosition(e));
// You can remove this line when you are using. I had added for the demo.
if (document.querySelector('.info')) document.querySelector('.info').remove();
}
function handleMouseLeave(e) {
const ID = e.target.getAttribute("data-hover-id");
document.getElementById(ID).style.opacity = 0;
document.getElementById(ID).style.transform = "scale(.8)";
setTimeout(() => {
document.getElementById(ID).remove();
}, 150);
}
function handleMouseMove(e) {
const ID = e.target.getAttribute("data-hover-id");
const wrapper = document.getElementById(ID);
wrapper.setAttribute("style", handlePosition(e));
}
window.addEventListener('scroll', () => {
const wrapper = document.querySelector('[data-hover-wrapper]');
if (wrapper) wrapper.remove();
});
That concludes this guide! You should now have a working Bootstrap 5 Popover with HTML content.







