面试题:水平垂直居中?

水平垂直居中?

  1. 绝对定位margin负值
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>1.水平垂直居中</title>
    <!--定位水平垂直居中,margin为负值-->
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .father{
            width: 500px;
            height: 500px;
            background: #000;
            margin: auto;
            position: relative;
        }
        .son{
            width: 200px;
            height: 200px;
            background: red;
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -100px 0 0 -100px;
        }
    </style>
</head>
<body>
<div class="father">
    <div class="son"></div>
</div>
</body>
</html>
复制代码
  1. 绝对定位,translate
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>2.水平垂直居中</title>
    <!--水平垂直居中:绝对定位,translate-->
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .father{
            width: 500px;
            height: 500px;
            background: red;
            margin: auto;
            position: relative;
        }
        .son{
            width: 200px;
            height: 200px;
            background: goldenrod;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-100px,-100px);
        }
    </style>
</head>
<body>
<div class="father">
    <div class="son"></div>
</div>
</body>
</html>
复制代码
  1. 父元素position:relative;子元素绝对定位,四个方位为0,margin:auto;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>3.水平垂直居中</title>
    <!--父元素position:relative;子元素绝对定位,四个方位为0,margin:auto-->
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .father{
            width: 500px;
            height: 500px;
            background: green;
            margin: auto;
            position: relative;
        }
        .son{
            width: 200px;
            height: 200px;
            background: pink;
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            margin: auto;
        }
    </style>
</head>
<body>
<div class="father">
    <div class="son"></div>
</div>
</body>
</html>
复制代码
  1. 水平垂直居中:父元素用overflow:hidden处理margin-top传递问题;子元素给元素设置margin
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>4.水平垂直居中</title>
    <!--水平垂直居中:父元素用overflow:hidden处理margin-top传递问题;子元素给元素设置margin-->
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .father{
            width: 500px;
            height: 500px;
            background: #000;
            margin: auto;
            overflow: hidden;
        }
        .son{
            width: 200px;
            height: 200px;
            background: brown;
            margin: 150px auto 0;/*margin-top传递问题margin-top:(父高-子高)/2*/
        }
    </style>
</head>
<body>
<div class="father">
    <div class="son"></div>
</div>
</body>
</html>
复制代码
  1. 水平垂直居中:父元素用overflow:hidden处理margin-top传递问题;子元素给元素设置margin
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>5.水平垂直居中</title>
    <!--水平垂直居中:父元素用overflow:hidden处理margin-top传递问题;子元素给元素设置margin-->
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .father{
            width: 500px;
            height: 500px;
            background: #000;
            margin: auto;
            overflow: hidden;
        }
        .son{
            width: 200px;
            height: 200px;
            background: brown;
            margin: 150px auto 0;/*margin-top传递问题margin-top:(父高-子高)/2*/
        }
    </style>
</head>
<body>
<div class="father">
    <div class="son"></div>
</div>
</body>
</html>
复制代码
  1. 水平垂直居中:给父元素加display:flex;justify-content:center;align-items:center
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>6.水平垂直居中</title>
    <style>
        /*水平垂直居中:给父元素加display:flex;justify-content:center;align-items:center*/
        *{
            margin: 0;
            padding: 0;
        }
        .father{
            width: 500px;
            height: 500px;
            background: bisque;
            margin: auto;
            display: flex;/*任何元素都可以设置display:flex*/
            justify-content: center;/*水平居中*/
            align-items: center;/*垂直居中*/
        }
        .son{
            width: 200px;
            height: 200px;
            background: goldenrod;
        }
    </style>
</head>
<body>
<div class="father">
    <div class="son"></div>
</div>
</body>
</html>
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值