<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>垂直居中--图片自适应宽高</title>
        <style type="text/css">
            /* 方法一 定位 为了看效果就加上了背景 宽高*/ 
            .wrap{
                position: relative;
                width: 200px;
                height: 200px;
                background: #000;
            }
            .box{
                position: absolute;
                top: 50%;
                left: 50%;
                background: #999; 
                width: 100px;
                height: 100px;
                transform: translate(-50%,-50%);
            }
            /* 方法二 flex布局 */
            .wrap{
                display: flex;
                justify-content: center;
                align-items: center;
                width: 200px;
                height: 200px;
                background: #000;
            }
            .box{
                background: #999;
                width: 100px;
                height: 100px;
            }
            /* 图片自适应宽高 */
            img{
                max-width: 100%;
                max-height: 100%;
            }
        </style>
    </head>
    <body>
        <div class="wrap">
            <div class="box">
                <img src="https://f11.baidu.com/it/u1=2936607031&u2=3595029289&fm=76" >
            </div>
        </div>
    </body>
</html>

 

相关文章:

  • 2022-12-23
  • 2021-05-18
  • 2022-12-23
  • 2021-07-08
  • 2021-06-28
  • 2022-12-23
  • 2021-10-29
  • 2021-08-28
猜你喜欢
  • 2021-08-13
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
  • 2021-12-05
  • 2021-12-01
  • 2021-11-06
相关资源
相似解决方案