CSS to achieve centering

CSS to achieve centering

<div class="out">
    <div class="in"></div>
</div>

1. The level is centered

  1. Realize horizontal centering through margin: 0 auto;
.out{
    
    
    width: 600px;
    height: 600px;
    background: cornflowerblue;
}
.in{
    
    
    width: 100px;
    height: 100px;
    background: chartreuse;
    margin: 0 auto;
}

  1. Achieve horizontal centering through position: absolute + margin-left
.out {
    
    
    height: 400px;
    width: 400px;
    background: cadetblue;
    position: relative;
}
.in {
    
    
    width: 100px;
    height: 100px;
    background: greenyellow;
    position: absolute;
    left: 50%;
    margin-left: -50px;
} 
  1. Achieve horizontal centering through text-align: center
.out {
    
    
    height: 400px;
    width: 400px;
    background: cadetblue;
    text-align: center;
}
.in {
    
        
    width: 100px;
    height: 100px;
    background: greenyellow;
    display: inline-block;
} 
  1. Achieve horizontal centering by hidden node + float method
<div class="out">
    <div class="a"></div>
    <div class="in"></div>
</div>
.out{
    
    
    width: 600px;
    height: 600px;
    background: cornflowerblue;
}
.out .a{
    
    
    content: "";
    height: 50%;
    width: 25%;
    float: left;
}
.in{
    
    
    width: 50%;
    height: 50%;
    background: chartreuse;
    float: left;
}

2. Vertically centered

  1. CSS vertical centering is achieved through position: absolute.
.out {
    
    
    height: 400px;
    width: 400px;
    background: cadetblue; 
    position: relative;
}
.in {
    
    
    width: 100px;
    height: 100px;
    background: greenyellow;
    position: absolute;
    top: 50%;
    margin-top: -50px;
} 
  1. When the height of the vertically-centered block-level element is unknown,
    you can use the transform property in CSS3 to offset the Y-axis by 50% to achieve vertical centering. Some browsers may have compatibility issues.
.out {
    
    
    height: 400px;
    width: 400px;
    background: cadetblue;
    position: relative;
}
.in{
    
    
    width: 100px;
    height: 100px;
    background: greenyellow;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);   
} 
  1. CSS vertical centering by hiding nodes
<div class="out">
    <div class="a"></div>
    <div class="in"></div>
</div>
.out{
    
    
    width: 600px;
    height: 600px;
    background: cornflowerblue;
}
.out .a{
    
    
    content: "";
    height: 25%;
    width: 50%;
}
.in{
    
    
    width: 50%;
    height: 50%;
    background: chartreuse;
}

3. Vertical and horizontal centering

  1. Achieve vertical and horizontal centering through position: absolute + margin: auto
.out{
    
    
    width: 600px;
    height: 600px;
    background: cornflowerblue;
    position: relative;
}
.in{
    
    
    width: 100px;
    height: 100px;
    background: chartreuse;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
}
  1. Achieve vertical and horizontal centering through position: absolute + margin
.out {
    
    
    height: 400px;
    width: 400px;
    background: cadetblue;
    position: relative;
}
.in {
    
        
    width: 100px;
    height: 100px;
    background: greenyellow;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -50px;
    margin-left: -50px;
} 
  1. Use transform: translate to achieve vertical and horizontal centering
.out {
    
    
    height: 400px;
    width: 400px;
    background: cadetblue;
    position: relative;
}
.in {
    
        
    width: 100px;
    height: 100px;
    background: greenyellow;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
} 
  1. Line box: use text-align: center + line-height to achieve vertical and horizontal centering
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    .text{
     
     
        height: 400px;
        width: 400px;
        background: cadetblue;
        text-align: center;
        line-height: 400px;
    }
    </style>
</head>
<body>
    <div class="text">
        Lorem ipsum dolor sit.
    </div>
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_50945128/article/details/110728072