面试:div水平垂直居中方案--img自适应

<!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>

 

posted @ 2020-10-14 22:36  鱼樱前端  阅读(204)  评论(0编辑  收藏  举报