欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

120. 4 cool ways to center any items using CSS

程序员文章站 2022-05-02 09:37:01
...

1. Text Align

  文字居中,不过也只是针对文字。

 1) test文字会横向居中

2) 对于Div ,会居中其中文字

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
          margin: 0;
          padding: 0;
          background: #eaeaea;
        }

        .container{
            width: 1000px;
            height: 800px;
            background: white;
            margin: 0 auto;
            text-align: center;
        }

        .box{
            width:300px;
            height:160px;
            background: yellowgreen;
        }
    </style>
</head>
<body>
    <div class="container">
        test
        <div class="box">test</div>
    </div>
</body>
</html>

2. Margin Auto: 

  margin: 0 auto;

 ç­‰ä»·äºŽmargin-left:auto; margin-right:auto;

上述方法是左右对齐,对于上下对齐,需要计算上下margin,并且设置父元素为overflow:hidden

overflow:详解  overflow:解决外边距坍塌

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
            body {
                margin: 0;
                padding: 0;
                background: #eaeaea;
            }
            
            .container {
                width: 1000px;
                height: 600px;
                background: white;
                overflow: hidden;
                
            }
            
            .box {
                width: 200px;
                height: 200px;
                background:deepskyblue;
                margin:200px auto;
            }
    </style>
</head>
<body>
    <div class="container">
        <div class="box" ></div>
    </div>
   
</body>
</html>

3. Display Flex:justify-content 主轴对齐方式 align-items:侧轴对齐方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            margin:0;
            padding:0;
            background: #eaeaea;
        }
        .box1{
            height:200px;
            width:200px;
            background: deepskyblue;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .box2{
            height:100px;
            width:100px;
            background: yellowgreen;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

4. Position Absolute

position:relative用于父元素, position:absolute用于子元素, 子元素会依照父层的边界进行定位的, 不然position:absolute 会逐层向上寻找设置了position:relative的元素边界, 直到body元素.

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box1 {
            height: 200px;
            width: 200px;
            background: deepskyblue;
            position: relative;
        }

        .box2 {
            height: 100px;
            width: 100px;
            background: yellowgreen;
            position: absolute;
            top:50%;
            left:50%;
            transform: translate(-50%, -50%);
        }
    </style>
</head>

<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>

</html>