'How to avoid overflow of a bootstrap card image in card-body?
I am using bootstrap cards and I could manage the card image from overflowing outside the card area but the bottom of the image overflows in the card-body region. Not sure if we have any CSS attribute that can avoid overflowing content into the current div.
#services img {
object-fit: cover;
height : 15rem;
transition: transform 0.2s;
}
#services .card {
overflow: hidden;
padding : 0px 0px;
}
#services img:hover {
transform: scale(1.1);
}
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div class="container py-5" id="services">
<div class="row pt-5">
<div class="col-md-6">
<div class="card shadow-sm">
<img src="https://media.istockphoto.com/photos/hot-air-balloons-flying-over-the-botan-canyon-in-turkey-picture-id1297349747?b=1&k=20&m=1297349747&s=170667a&w=0&h=oH31fJty_4xWl_JQ4OIQWZKP8C6ji9Mz7L4XmEnbqRU=" alt="...">
<div class="card-body">
<h5 class="card-title">Card Title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk
of the card's content.</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Solution 1:[1]
Try out this snippet.
This is fairly simple as you have to wrap the image inside a div and add overflow-hidden utility class to the wrapper.
overflow utilities: Overflow utilities
#services img {
object-fit: cover;
height : 15rem;
transition: transform 0.2s;
}
#services img:hover {
transform: scale(1.1);
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container py-5" id="services">
<div class="row pt-5">
<div class="col-md-6">
<div class="card shadow-sm">
<div class="overflow-hidden">
<img class="card-img-top" src="https://media.istockphoto.com/photos/hot-air-balloons-flying-over-the-botan-canyon-in-turkey-picture-id1297349747?b=1&k=20&m=1297349747&s=170667a&w=0&h=oH31fJty_4xWl_JQ4OIQWZKP8C6ji9Mz7L4XmEnbqRU=" alt="...">
</div>
<div class="card-body">
<h5 class="card-title">Card Title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk
of the card's content.</p>
</div>
</div>
</div>
</div>
</div>
Solution 2:[2]
#services img {
object-fit: cover;
transition: transform 0.2s;
width: 100%;
}
#services .card {
overflow: hidden;
padding : 0px 0px;
}
#services img:hover {
transform: scale(1.1);
}
#services .card-img {
height : 15rem;
overflow: hidden;
width: 100%;
}
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div class="container py-5" id="services">
<div class="row pt-5">
<div class="col-md-6">
<div class="card shadow-sm">
<div class="card-img">
<img src="https://media.istockphoto.com/photos/hot-air-balloons-flying-over-the-botan-canyon-in-turkey-picture-id1297349747?b=1&k=20&m=1297349747&s=170667a&w=0&h=oH31fJty_4xWl_JQ4OIQWZKP8C6ji9Mz7L4XmEnbqRU=" class="img-fluid" alt="...">
</div>
<div class="card-body">
<h5 class="card-title">Card Title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk
of the card's content.</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
This should work:
- Wrap the image into a div
- Give width 100% to this div and height
(ex: 15rem), and makeoverflow:hiddenof this div. So, anything that goes outside of div will be hidden.
Solution 3:[3]
you should put your image in div tag like blow:
<div class="card-image">
<img src="https://picsum.photos/id/237/200/300" alt="...">
</div>
then style like below:
#services .card-image img {
object-fit: cover;
height : 15rem;
width: 100%;
transition: transform 0.2s;
}
#services .card {
overflow: hidden;
padding : 0px 0px;
}
#services .cardimage img:hover {
transform: scale(1.1);
}
.card-image{
width: 100%;
height : 15rem;
overflow: hidden;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Saroj Shrestha |
| Solution 3 | Mohammad Esmaeili |
