css面试题

62 阅读1分钟

手写垂直水平居中

常见的垂直水平居中方法

  • position+margin负值的方法(固定宽高)
  • position+margin:auto (固定宽高)
  • display:table-cell + vertical-align:middle (固定宽度)
  • position+transform(不需要固定宽高)
  • flex (不需要固定宽高)

代码

position+margin负值的方法

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>垂直水平居中</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .box {
        position: relative;
        width: 500px;
        height: 500px;
        border: 5px solid red;
    }

    .item {
        position: absolute;
        left: 50%;
        top: 50%;
        margin-top: -150px;
        margin-left: -100px;
        width: 200px;
        height: 300px;
        background-color: #ccc;
    }
</style>

<body>
    <div class="box">
        <div class="item">垂直水平</div>
    </div>
</body>

</html>

position+margin:auto

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>垂直水平居中</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    .box{
        position: relative;
        width: 500px;
        height: 500px;
        border: 5px solid red;
    }
    .item{
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        margin: auto; 
        width: 200px;
        height: 300px;
        background-color: #ccc;
    }
</style>
<body>
    <div class="box">
        <div class="item">垂直水平居中</div>
    </div>
</body>
</html>

display:table-cell + vertical-align:middle

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>垂直水平居中</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .box {
        display: table-cell;
        vertical-align: middle;
        width: 500px;
        height: 500px;
        border: 5px solid red;
    }

    .item {
        margin: auto;
        width: 200px;
        height: 300px;
        background-color: #ccc;
    }
</style>

<body>
    <div class="box">
        <div class="item">垂直水平居中</div>
    </div>
</body>

</html>

position+transform

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>垂直水平居中</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .box {
        position: relative;
        width: 500px;
        height: 500px;
        border: 5px solid red;
    }

    .item {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        background-color: #ccc;
    }
</style>

<body>
    <div class="box">
        <div class="item">垂直水平居中</div>
    </div>
</body>

</html>

flex

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>垂直水平居中</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .box {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 500px;
        height: 500px;
        border: 5px solid red;
    }

    .item {
     
        background-color: #ccc;
    }
</style>

<body>
    <div class="box">
        <div class="item">垂直水平居中</div>
    </div>
</body>

</html>