我想创建一个响应式主图,但我似乎无法将我的图片元素置于父 div 的中心。我已经检查了这些帖子:
Center image in div horizontally
Center align image within div horizontally
Center image element in parent div
正如这些其他帖子中所建议的,我尝试使用 margin: auto auto;
,使用 position
,将 min-width
设置为父 div,但图像仍然与最左侧对齐。
Here's an example我正在努力实现的目标
这是我的代码(我还创建了一个 jsfiddle:https://jsfiddle.net/wuy44t9p/1/):
<style type="text/css">
.profile-banner {
display: block;
height: 500px;
position: relative;
}
.profile-banner picture {
display: block;
margin: 0 auto;
position: absolute;
}
</style>
<div id="CPHBigBanner_pnlCoverPhoto">
<section class="profile-banner lazy">
<picture class="profile-herobanner lazy" data-was-processed="true">
<source type="image/webp" class="lazy" data-srcset="http://www.1001locaties.nl/images/tmp/41747_965_Owl.webp" srcset="/images/objphotos/covers/41747_965_Owl.webp" data-was-processed="true">
<img class="lazy loaded" data-src="http://www.1001locaties.nl/images/tmp/41747_965_Owl.png" alt="" src="/images/objphotos/covers/41747_965_Owl.png" data-was-processed="true">
</picture>
</section>
</div>
最佳答案
根据我的意见,您需要从图像中删除绝对定位,但还要向容器添加文本对齐中心:
.profile-banner {
display: block;
height: 500px;
text-align:center;
}
.profile-banner picture {
display: block;
margin: 0 auto;
}
<div id="CPHBigBanner_pnlCoverPhoto">
<section class="profile-banner lazy">
<picture class="profile-herobanner lazy" data-was-processed="true">
<source type="image/webp" class="lazy" data-srcset="http://www.1001locaties.nl/images/tmp/41747_965_Owl.webp" srcset="https://via.placeholder.com/350x150" data-was-processed="true">
<img class="lazy loaded" data-src="http://www.1001locaties.nl/images/tmp/41747_965_Owl.png" alt="" srcset="https://via.placeholder.com/350x150" data-was-processed="true">
</picture>
</section>
</div>
关于css - 在父 div 的中心对齐图片元素使图像左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50292506/