如何在 Bootstrap 4 中的图像叠加卡上垂直对齐?
How to vertically align on an image overlay card in Bootstrap 4?
我试图将 FontAwesome 图标置于图像覆盖卡的中心,但我无法将其垂直居中。
我尝试了其他多种建议,例如使用 d-flex
和 justify-content-center
,但 none 似乎有效。我错过了什么?
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css">
</head>
<body>
<div class="card bg-dark text-white">
<img class="card-img" src="http://placehold.jp/500x200.png" alt="Example">
<div class="card-img-overlay text-center">
<i class="fab fa-youtube fa-5x" style="color:#FF0000"></i>
</div>
</div>
</body>
我想让图标在图片中间居中,但是只能水平对齐,不能垂直对齐。感谢您的帮助。
您可以在 card-img-overlay 元素上使用 bootstrap flex utilities class :
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="card bg-dark text-white">
<img class="card-img" src="https://fakeimg.pl/250x100/000000/" alt="Example">
<div class="card-img-overlay text-center d-flex flex-column justify-content-center">
<a data-fancybox href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" class="">
<i class="fab fa-youtube fa-5x zoom" style="color:#FF0000"></i>
</a>
</div>
</div>
您可以将 d-flex
class 添加到 .card-img-overlay
元素,然后将 class="align-self-center mx-auto"
添加到内部 a
元素:
<div class="card bg-dark text-white">
<img class="card-img" src="images/example.jpg" alt="Example">
<div class="card-img-overlay d-flex">
<a class="align-self-center mx-auto" data-fancybox href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">
<i class="fa fa-youtube fa-5x zoom" style="color:#FF0000"></i>
</a>
</div>
</div>
官方 BS4 文档中关于 align-self-center
的更多信息:https://getbootstrap.com/docs/4.3/utilities/flex/#align-self
您的第一次尝试是正确的(添加 d-flex
和 justify-content-center
class)。你只是忘了还要添加 align-items-center
class.
我试图将 FontAwesome 图标置于图像覆盖卡的中心,但我无法将其垂直居中。
我尝试了其他多种建议,例如使用 d-flex
和 justify-content-center
,但 none 似乎有效。我错过了什么?
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css">
</head>
<body>
<div class="card bg-dark text-white">
<img class="card-img" src="http://placehold.jp/500x200.png" alt="Example">
<div class="card-img-overlay text-center">
<i class="fab fa-youtube fa-5x" style="color:#FF0000"></i>
</div>
</div>
</body>
我想让图标在图片中间居中,但是只能水平对齐,不能垂直对齐。感谢您的帮助。
您可以在 card-img-overlay 元素上使用 bootstrap flex utilities class :
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="card bg-dark text-white">
<img class="card-img" src="https://fakeimg.pl/250x100/000000/" alt="Example">
<div class="card-img-overlay text-center d-flex flex-column justify-content-center">
<a data-fancybox href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" class="">
<i class="fab fa-youtube fa-5x zoom" style="color:#FF0000"></i>
</a>
</div>
</div>
您可以将 d-flex
class 添加到 .card-img-overlay
元素,然后将 class="align-self-center mx-auto"
添加到内部 a
元素:
<div class="card bg-dark text-white">
<img class="card-img" src="images/example.jpg" alt="Example">
<div class="card-img-overlay d-flex">
<a class="align-self-center mx-auto" data-fancybox href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">
<i class="fa fa-youtube fa-5x zoom" style="color:#FF0000"></i>
</a>
</div>
</div>
官方 BS4 文档中关于 align-self-center
的更多信息:https://getbootstrap.com/docs/4.3/utilities/flex/#align-self
您的第一次尝试是正确的(添加 d-flex
和 justify-content-center
class)。你只是忘了还要添加 align-items-center
class.