使图片垂直&水平居中的CSS实现方法

方法一:使用CSS的 background-image

html { 
   width:100%; 
   height:100%; 
   background:url(logo.png) center center no-repeat;
}

方法二:使用CSS的 margin 实现

img {
   position: absolute;
   top: 50%;
   left: 50%;
   width: 500px; /*图片宽度*/
   height: 500px; /*图片高度*/
   margin-top: -250px; /* 高度的一半 */
   margin-left: -250px; /* 宽度的一半 */
}

方法三:table 方式实现
CSS:

html, body, #wrapper {
   height:100%;
   width: 100%;
   margin: 0;
   padding: 0;
   border: 0;
}
#wrapper td {
   vertical-align: middle;
   text-align: center;
}

HTML:

<html>
<body>
   <table id="wrapper">
      <tr>
         <td><img src="logo.png" alt="" /></td>
      </tr>
   </table>
</body>
</html>

方法四:Flex 技术实现
CSS:

.container{
    width: 1000px;
    height: 800px;
    border: 1px solid #ccc;
    margin: 0 auto;
}
div.horizontal {
    height: 100%;
    display: flex;
    // justify-content: center;
}

div.vertical {
    display: flex;
    flex-direction: column;
    justify-content: center;
}

HTML:

<div class="container">
    <div class="horizontal div1">
        <div class="vertical">
            <img src="logo.png" />
        </div>
    </div>
</div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值