Bootstrap 5 拉伸 div 背景和内容中心
Bootstrap 5 stretch div background and content center
我正在尝试使用 align-items-stretch 拉伸 div 的背景并使用 align-items-center 使内容居中(垂直!)。当我使用很长的标题时,不知何故 align-items-center 不会被应用。
'Devote' 需要与另一张卡垂直居中对齐
这是应该发生的事情;垂直对齐相同高度的图像
我做错了什么?
更新:添加了 CSS 自己的风格 + bootstrap 特定于源
更新:当我应用内联 min-height 时,内容将垂直居中(这很好)。
<div class="col-6 grow d-flex align-items-stretch">
<div class="col-12 tile elevation-1">
<div class="row no-gutters align-items-center">
<div class="col-12 col-md-4">
<div class="col-12">
<img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
</div>
</div>
<div class="col-12 col-md-8 tile-product-content-horizontal" style="min-height:300px;">
<div class="col-12 ">
<span class="brand"> Brand name </span>
<h3> Short title </h3>
</div>
</div>
</div>
</div>
</div>
<div class="col-6 grow d-flex align-items-stretch">
<div class="col-12 tile elevation-1">
<div class="row no-gutters align-items-center">
<div class="col-12 col-md-4">
<div class="col-12">
<img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
</div>
</div>
<div class="col-12 col-md-8 tile-product-content-horizontal" style="min-height:300px;">
<div class="col-12 ">
<span class="brand"> Brand name </span>
<h3> This is a very long title which stretches the div </h3>
</div>
</div>
</div>
</div>
</div>
CSS:
.tile-product-content-horizontal {
padding: 20px 40px 20px 5px;
}
.tile {
border-radius: 16px;
background-color: #FFF;
margin-bottom: 20px;
background-image: url(assets/card-chevron.png);
background-repeat: no-repeat;
background-position: right 30px center;
}
/* BOOTSTRAP SPECIFIC */
.align-items-stretch {
align-items:stretch !important
}
.align-items-center {
align-items:center !important
}
.d-flex {
display:flex !important
}
您需要垂直拉伸列,然后垂直对齐它们的内容。
做这些:
- 移除内部的 .col-12 并改用 .tile-product-content-horizontal
- 在 .row 上使用 .h-100 并删除 .align-items-center
- 在最短的列上使用 .d-flex 和 .alig-items-cener。
<div class="col-6 grow d-flex align-items-stretch">
<div class="col-12 tile elevation-1">
<div class="row no-gutters h-100">
<div class="col-12 col-md-4">
<div class="col-12">
<img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
</div>
</div>
<div class="col-12 col-md-8 d-flex align-items-center" >
<div class="tile-product-content-horizontal">
<span class="brand"> Brand name </span>
<h3> This is a very long title which stretches the div </h3>
</div>
</div>
</div>
</div>
</div>
.tile-product-content-horizontal {
padding: 20px 40px 20px 5px;
}
.tile {
border-radius: 16px;
background-color: #FFF;
margin-bottom: 20px;
background-image: url(assets/card-chevron.png);
background-repeat: no-repeat;
background-position: right 30px center;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<div class="col-6 grow d-flex align-items-stretch">
<div class="col-12 tile elevation-1">
<div class="row no-gutters h-100">
<div class="col-12 col-md-4">
<div class="col-12">
<img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
</div>
</div>
<div class="col-12 col-md-8 d-flex align-items-center">
<div class="tile-product-content-horizontal">
<span class="brand"> Brand name </span>
<h3> Short title </h3>
</div>
</div>
</div>
</div>
</div>
<div class="col-6 grow d-flex align-items-stretch">
<div class="col-12 tile elevation-1">
<div class="row no-gutters h-100">
<div class="col-12 col-md-4">
<div class="col-12">
<img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
</div>
</div>
<div class="col-12 col-md-8 d-flex align-items-center">
<div class="tile-product-content-horizontal">
<span class="brand"> Brand name </span>
<h3> This is a very long title which stretches the div </h3>
</div>
</div>
</div>
</div>
</div>
我正在尝试使用 align-items-stretch 拉伸 div 的背景并使用 align-items-center 使内容居中(垂直!)。当我使用很长的标题时,不知何故 align-items-center 不会被应用。
我做错了什么?
更新:添加了 CSS 自己的风格 + bootstrap 特定于源 更新:当我应用内联 min-height 时,内容将垂直居中(这很好)。
<div class="col-6 grow d-flex align-items-stretch">
<div class="col-12 tile elevation-1">
<div class="row no-gutters align-items-center">
<div class="col-12 col-md-4">
<div class="col-12">
<img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
</div>
</div>
<div class="col-12 col-md-8 tile-product-content-horizontal" style="min-height:300px;">
<div class="col-12 ">
<span class="brand"> Brand name </span>
<h3> Short title </h3>
</div>
</div>
</div>
</div>
</div>
<div class="col-6 grow d-flex align-items-stretch">
<div class="col-12 tile elevation-1">
<div class="row no-gutters align-items-center">
<div class="col-12 col-md-4">
<div class="col-12">
<img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
</div>
</div>
<div class="col-12 col-md-8 tile-product-content-horizontal" style="min-height:300px;">
<div class="col-12 ">
<span class="brand"> Brand name </span>
<h3> This is a very long title which stretches the div </h3>
</div>
</div>
</div>
</div>
</div>
CSS:
.tile-product-content-horizontal {
padding: 20px 40px 20px 5px;
}
.tile {
border-radius: 16px;
background-color: #FFF;
margin-bottom: 20px;
background-image: url(assets/card-chevron.png);
background-repeat: no-repeat;
background-position: right 30px center;
}
/* BOOTSTRAP SPECIFIC */
.align-items-stretch {
align-items:stretch !important
}
.align-items-center {
align-items:center !important
}
.d-flex {
display:flex !important
}
您需要垂直拉伸列,然后垂直对齐它们的内容。
做这些:
- 移除内部的 .col-12 并改用 .tile-product-content-horizontal
- 在 .row 上使用 .h-100 并删除 .align-items-center
- 在最短的列上使用 .d-flex 和 .alig-items-cener。
<div class="col-6 grow d-flex align-items-stretch">
<div class="col-12 tile elevation-1">
<div class="row no-gutters h-100">
<div class="col-12 col-md-4">
<div class="col-12">
<img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
</div>
</div>
<div class="col-12 col-md-8 d-flex align-items-center" >
<div class="tile-product-content-horizontal">
<span class="brand"> Brand name </span>
<h3> This is a very long title which stretches the div </h3>
</div>
</div>
</div>
</div>
</div>
.tile-product-content-horizontal {
padding: 20px 40px 20px 5px;
}
.tile {
border-radius: 16px;
background-color: #FFF;
margin-bottom: 20px;
background-image: url(assets/card-chevron.png);
background-repeat: no-repeat;
background-position: right 30px center;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<div class="col-6 grow d-flex align-items-stretch">
<div class="col-12 tile elevation-1">
<div class="row no-gutters h-100">
<div class="col-12 col-md-4">
<div class="col-12">
<img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
</div>
</div>
<div class="col-12 col-md-8 d-flex align-items-center">
<div class="tile-product-content-horizontal">
<span class="brand"> Brand name </span>
<h3> Short title </h3>
</div>
</div>
</div>
</div>
</div>
<div class="col-6 grow d-flex align-items-stretch">
<div class="col-12 tile elevation-1">
<div class="row no-gutters h-100">
<div class="col-12 col-md-4">
<div class="col-12">
<img src="https://via.placeholder.com/512" alt="" class="img-fluid tile-image">
</div>
</div>
<div class="col-12 col-md-8 d-flex align-items-center">
<div class="tile-product-content-horizontal">
<span class="brand"> Brand name </span>
<h3> This is a very long title which stretches the div </h3>
</div>
</div>
</div>
</div>
</div>