How to use links on card-img-overlay class in Bootstrap?
Last Updated :
25 Jul, 2024
When we put any link inside of a bootstrap card it performs well until we used card-img-overlay to put an image as a background of that card.
Approach:
All the links placed inside class card-img-overlay works but links placed outside this class do not work. To make these links work, set the position of these links 'relative'.
CSS Code:
Place this inside the <style> tag.
.card-link
{
position:relative;
}
Below example illustrates the approach:
Example 1: This example illustrates card card-img-overlay, in the 1st card we are not using the card-img-overlay but when we use the card-img-overlay the links are not working even text is not responding as a text. It totally behaves likes a picture.
html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Card</title>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href=
"https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js">
</script>
<script src=
"https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js">
</script>
<style>
.card {
width: 250px;
height: 300px;
border: 2px solid black;
padding: 5px;
}
h1 {
color: green;
text-align: center;
}
img {
height: 120px;
}
.left {
float: left;
}
.right {
float: right;
}
.container {
margin-top: 50px;
width: 600px;
height: auto;
}
</style>
</head>
<body>
<div class="container">
<h1>GeeksforGeeks</h1>
<div class="card left">
<img class="card-img-top" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20200109151258/672.png" />
<div class="card-inverse">
<h3 class="text-stroke">GeeksforGeeks</h3>
</div>
<div class="card-block">
<a href="#" class="card-link">Card link</a>
<p class="card-text">A Computer Science Portal</p>
</div>
<div class="card-footer">
<small class="text-muted">Card link is working</small>
</div>
</div>
<div class="card right">
<img class="card-img-top" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20200109151258/672.png" />
<div class="card-img-overlay card-inverse">
<h3 class="text-stroke">GeeksforGeeks</h3>
</div>
<div class="card-block">
<a href="#" class="card-link">Card link</a>
<p class="card-text">A Computer Science Portal</p>
</div>
<div class="card-footer">
<small class="text-muted">Card link is not working</small>
</div>
</div>
</div>
</body>
</html>
Output:
Example 2: This example illustrates card card-img-overlay, in the 1st card we are not using the card-img-overlay but when we use the card-img-overlay the links are working and text are also behaving as text.
html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Card</title>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href=
"https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js">
</script>
<script src=
"https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js">
</script>
<style>
.card-link{
position:relative;
}
.card {
width: 250px;
height: 300px;
border: 2px solid black;
padding: 5px;
}
h1 {
color: green;
text-align: center;
}
img {
height: 120px;
}
.left {
float: left;
}
.right {
float: right;
}
.container {
margin-top: 50px;
width: 600px;
height: auto;
}
</style>
</head>
<body>
<div class="container">
<h1>GeeksforGeeks</h1>
<div class="card left">
<img class="card-img-top" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20200109151258/672.png" />
<div class="card-inverse">
<h3 class="text-stroke">GeeksforGeeks</h3>
</div>
<div class="card-block">
<a href="#" class="card-link">Card link</a>
<p class="card-text">A Computer Science Portal</p>
</div>
<div class="card-footer">
<small class="text-muted">Card link is working</small>
</div>
</div>
<div class="card right">
<img class="card-img-top" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20200109151258/672.png" />
<div class="card-img-overlay card-inverse">
<h3 class="text-stroke">GeeksforGeeks</h3>
<div class="card-block">
<a href="#" class="card-link text-white" >Card link</a>
<p class="card-text">A Computer Science Portal</p>
</div>
</div>
<a href="#" class="card-link " >Card link</a>
<div class="card-footer">
<small>Card link is working</small>
</div>
</div>
</div>
</body>
</html>
Output:
Note: In the second example the muted text not behaving like text because it is out of the card-link div.
Similar Reads
How to Create Image Overlay Effects in Bootstrap ? Creating image overlay effects in Bootstrap means adding content like text, buttons, or other elements on top of an image. This is achieved using Bootstrap's utility classes and custom CSS to position the overlay content, often making use of relative and absolute positioning.ApproachUse Bootstrap's
2 min read
How to use image overlay correctly with Bootstrap ? In this article, we will be learning how to use image overlay correctly with Bootstrap. But before that, first, we need to know what is an Image Overlay. Image Overlay: Image overlay generally refers to the image being a background image and inserting texts, and links inside of that image. It can be
2 min read
How to Create a Responsive Image Gallery in Bootstrap ? An image gallery is like a digital album where we can put our multiple images to attract user attention. We have used Bootstrap a CSS framework for Creating a Responsive Image Gallery where the layout adjusts automatically based on the screen size, ensuring that it looks good on both large desktop s
3 min read
How to Create Cards in Bootstrap ? Bootstrap cards, use the .card class. Customize content with headings, paragraphs, images, and buttons. Utilize contextual background colors and additional classes for styling and alignment. Cards offer flexible content containers with options for headers, footers, and powerful display settings.In s
3 min read
How to Create an Image Overlay Icon using HTML and CSS ? Image overlay Icon can be an impressive addition to interactive detail or a set of features for your website. This article content will divide the task into two sections, the first section creating the structure and attach the link for the icon. In the second section, we will design the structure us
3 min read