如何避免卡体中bootstrap个卡图溢出?
How to avoid overflow of a bootstrap card image in card-body?
我正在使用 bootstrap 卡片,我可以防止卡片图像溢出到卡片区域之外,但图像底部溢出到卡片主体区域。不确定我们是否有任何 CSS 属性可以避免内容溢出到当前 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/bootstrap@5.1.3/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>
试试这个片段。
这很简单,因为您必须将图像包装在 div 中,然后将 overflow-hidden 实用程序 class 添加到包装器中。
溢出实用程序: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/bootstrap@5.1.3/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>
#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/bootstrap@5.1.3/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>
这应该有效:
- 将图像包装成 div
- 将此 div 的宽度和高度
(ex: 15rem)
设为 100%,并将此 div 设为 overflow:hidden
。因此,div 之外的任何内容都将被隐藏。
你应该把你的图片放在 div 标签中,就像吹:
<div class="card-image">
<img src="https://picsum.photos/id/237/200/300" alt="...">
</div>
然后样式如下:
#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;
}
我正在使用 bootstrap 卡片,我可以防止卡片图像溢出到卡片区域之外,但图像底部溢出到卡片主体区域。不确定我们是否有任何 CSS 属性可以避免内容溢出到当前 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/bootstrap@5.1.3/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>
试试这个片段。
这很简单,因为您必须将图像包装在 div 中,然后将 overflow-hidden 实用程序 class 添加到包装器中。
溢出实用程序: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/bootstrap@5.1.3/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>
#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/bootstrap@5.1.3/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>
这应该有效:
- 将图像包装成 div
- 将此 div 的宽度和高度
(ex: 15rem)
设为 100%,并将此 div 设为overflow:hidden
。因此,div 之外的任何内容都将被隐藏。
你应该把你的图片放在 div 标签中,就像吹:
<div class="card-image">
<img src="https://picsum.photos/id/237/200/300" alt="...">
</div>
然后样式如下:
#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;
}