为了账号安全,请及时绑定邮箱和手机立即绑定

垂直对齐具有响应高度的div内的图像

垂直对齐具有响应高度的div内的图像

摇曳的蔷薇 2019-06-18 16:02:54
垂直对齐具有响应高度的div内的图像我有下面的代码,它设置了一个容器,其高度随浏览器大小的变化而变化(以保持方宽比)。HTML<div class="responsive-container">     <div class="dummy"></div>     <div class="img-container">         <IMG HERE>     </div></div>CSS.responsive-container {     position: relative;     width: 100%;     border: 1px solid black;}.dummy {     padding-top: 100%; /* forces 1:1 aspect ratio */}.img-container {     position: absolute;     top: 0;     bottom: 0;     left: 0;     right: 0;}如何在容器内垂直对齐IMG?我所有的图像都有可变的高度,容器不能有固定的高度/线高,因为它是有响应的.救命啊!
查看完整描述

3 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

对于柔性盒,这很容易:

小提琴

只需将以下内容添加到图像容器中:

.img-container {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex; /* add */
    justify-content: center; /* add to align horizontal */
    align-items: center; /* add to align vertical */}


查看完整回答
反对 回复 2019-06-18
  • 3 回答
  • 0 关注
  • 355 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信