JavaScript Link Preview on Hover

JavaScript Link Preview on Hover
Code Snippet:Link Preview on Hover
Author: Elliott
Published: January 14, 2024
Last Updated: January 22, 2024
Downloads: 745
License: MIT
Edit Code online: View on CodePen
Read More

The JavaScript code snippet helps you to preview the link on hover. When you hover over a link, it reveals an image, title, and description in a card. This feature helps users quickly preview linked content without clicking.

You can use this code in your web project to provide a convenient link preview feature. Moreover, you can customize the preview card with additional CSS according to your website’s design.

How to Create Link Preview On Hover Using JavaScript

1. To get started, add the HTML structure to your webpage. Inside a <section>, create links that you want to have link previews for. Each link should have the class “link-with-preview” and custom data attributes for image, title, and text.

	<h1>Link Preview</h1>
		Once upon a time there were two
		<a href="" target="_blank" onmouseover="showLinkPreview()" onmouseleave="hideLinkPreview()" class="link-with-preview" data-image="" data-title="Mountain Friends 🏔" data-text="Introduced by their tectonic plate neighbors when they were very young.">
			mountain friends
		with a
		<a href="" target="_blank" onmouseover="showLinkPreview()" onmouseleave="hideLinkPreview()" class="link-with-preview" data-image="" data-title="Raging River 🌊" data-text="With the exception that this one is not very exciting.">raging river
		between. That's the end of
		<a href="" target="_blank" onmouseover="showLinkPreview()" onmouseleave="hideLinkPreview()" class="link-with-preview" data-image="" data-title="Our Story" data-text="Looking at all these pictures makes me really not want to be inside right now.">our story</a>.

	<div class="card">
		<img src="" class="card-img-top" />
		<div class="card-body">
			<h5 class="card-title"></h5>
			<p class="card-text"></p>

2. Apply some CSS to style the link previews and the card that will display the preview information. Customize the appearance to match your website’s design.

:root {
	font-size: 16px;

body {
	font-size: 24px;
	font-family: sans-serif;

h1 {
	font-weight: 600;
	line-height: 1.5;

section a {
	color: rgb(255, 144, 85);

section a:hover {
	text-decoration: none;
	color: rgb(255, 180, 89);

.card {
       background: #fff;
	width: 15rem;
	display: none;
	font-size: 1rem;
	color: black;
	position: absolute;
	z-index: 100;
	bottom: 30px;
	left: 50%;
	transform: translateX(-50%);
       box-shadow: 1px 2px 8px rgba(0, 0, 0, 0.35);

.link-with-preview {
	position: relative;

.card img {
	width: 15rem;

.card-title {
	font-size: 1.25rem;
    padding: 12px;
@media (max-width: 600px) {
	:root {
		font-size: 10px;
	h1 {
		font-size: 4rem;

3. Finally, copy and paste the following JavaScript code into your website’s JavaScript file. This code defines functions for showing and hiding link previews and adds event listeners to the links.

const card = document.querySelector(".card");

const hideLinkPreview = () => = "none";

const showLinkPreview = e => {
  const image = e.currentTarget.getAttribute("data-image");
  card.querySelector("img").setAttribute("src", image);

  const title = e.currentTarget.getAttribute("data-title");
  card.querySelector("h5").textContent = title;

  const text = e.currentTarget.getAttribute("data-text");
  card.querySelector("p").textContent = text;


  return = "inline-block";

document.querySelectorAll(".link-with-preview").forEach(el => {
  el.addEventListener("mouseover", showLinkPreview);
  el.addEventListener("mouseleave", hideLinkPreview);

That’s all! hopefully, you have successfully created a link preview feature on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link