居中

84 阅读1分钟

自己对知识的了解,留作面试用,如有错误请指正

水平居中:

行内元素:给父元素设置 text-align: center

<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>Document</title>
    <style>
        .p{
            text-align: center;
        }
        .box{
            width: 50px;
            height: 50xp;
            background-color: brown;
            
        }
    </style>
</head>
<body>
    <div class="p">
        <span class="box">一个盒子</span>
    </div>
    
</body>
</html>

块级元素:已知width, margin:0 auto

<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>Document</title>
    <style>
        
        .box{
            
            width: 50px;
            margin: 0 auto;
            background-color: brown;
            
        }
    </style>
</head>
<body>
    
        <div class="box">一个盒子</div>
   
    
</body>
</html>

绝对定位:left+tranxlateX

<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>Document</title>
    <style>
        
        .box{
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            background-color: brown;
            
        }
    </style>
</head>
<body>
    
        <div class="box">一个盒子</div>
   
    
</body>
</html>

flex布局:父元素justify-content: center

<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>Document</title>
    <style>
        .p{
            display: flex;
            justify-content: center;
        }
        .box{
            
            background-color: brown;
            
        }
    </style>
</head>
<body>
    <div class="p">
        <div class="box">一个盒子</div>
    </div>
        
   
    
</body>
</html>

# 垂直居中

单行文字:padding/line-height=height

<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>Document</title>
    <style>
        
        .box{
            height: 200px;
            line-height: 200px;

            /* padding: 100px; */
            background-color: brown;
            
        }
    </style>
</head>
<body>
    
        <div class="box">文字垂直居中</div>
   
</body>
</html>

绝对定位:

子绝父相:

1、子元素上下为0,margin:auto 已知高度

2、子元素 top:50%,(margin-top:height一半/translateY(-50%))未知高度

<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>Document</title>
    <style>
        .p{
            position: relative;
            background-color: aqua;
            height: 200px;
        }
        .box{
            position: absolute;
            top:50%;
            /* margin-top: -100px; */
            transform: translateY(-50%);
            /* top: 0;
            bottom: 0;
            margin: auto; */
            height: 200px;
            background-color: brown;
            
        }
    </style>
</head>
<body>
    <div class="p">
        <div class="box">一个盒子</div>
    </div>
   
</body>
</html>

flex布局:

未知高度,父元素align-items: center;

<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>Document</title>
    <style>
        .p{
            display: flex;
            align-items: center;
            background-color: aqua;
            height: 500px;
        }
        .box{
            /* height: 22px; */
            background-color: brown;
            
        }
    </style>
</head>
<body>
    <div class="p">
        <div class="box">一个盒子</div>
    </div>
   
</body>
</html>

上下左右居中

flex:父元素align-items: center;justify-content: center;

<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>Document</title>
    <style>
        .p{
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: aqua;
            height: 500px;
        }
        .box{
            /* height: 22px; */
            background-color: brown;
            
        }
    </style>
</head>
<body>
    <div class="p">
        <div class="box">一个盒子</div>
    </div>
   
</body>
</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>Document</title>
    <style>
        .p{
            position: relative;
            background-color: aqua;
            height: 500px;
        }
        .box{
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            /* height: 22px; */
            background-color: brown;
            
        }
    </style>
</head>
<body>
    <div class="p">
        <div class="box">一个盒子</div>
    </div>
   
</body>
</html>