html5 - h5页面不规则尺寸图片垂直居中
ringa_lee
ringa_lee 2017-04-17 11:46:01
[HTML讨论组]

问题描述:
想要在chrome 模拟手机上让不规则尺寸图片垂直水平居中。
边框适应屏幕设置成正方形。
现在只能做到水平居中。
达达们有什么好的建议和意见?








  
  
  
  
  

  



ringa_lee
ringa_lee

ringa_lee

全部回复(12)
黄舟

既然你说到了h5,那肯定首推Flex布局呀。

.pic-box {
    display: flex;
    justify-content: center;
    align-items: center;
}

justify-content: center;用于水平居中,align-items: center;设置垂直居中。

大家讲道理
img{width: 100%;height: 100%;object-fit:contain;}
伊谢尔伦
.pic-box{
  position: absolute;
  width: 100%;
  height: 100%;
}
li img{
  max-width: 100%;
  max-height: 100%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%,-50%,0);
  -ms-transform: translate3d(-50%,-50%,0);
  -moz-transform: translate3d(-50%,-50%,0);
  -webkit-transform: translate3d(-50%,-50%,0);
  -o-transform: translate3d(-50%,-50%,0);
}
大家讲道理

用fle布局

ringa_lee

既然是h5,当然弹性布局是首先应该尝试的。而且flex用起来也很方便。

巴扎黑

没人想到table-cell么。。。?渲染比flex效率要高

迷茫

left:50%; transform:translate(-50%, 0)

阿神

h5有个width height的新单位,vw vh,你查查就懂了

阿神

图片用js设置height和width一样变成正方形,图片的上一层用display:table使图片垂直居中

迷茫

做成背景图片更简单

.wrapper{
width:50%;
height:50%;/* 为了正方形,高宽只要取相同值就行*/
background: url(../img/path) no-repeat;
background-position: center center;
background-size:cover;
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号