CSS基础应用:介绍几种常见的水平垂直居中实现方案

CSS基础应用:介绍几种常见的水平垂直居中实现方案

常见的水平垂直居中实现方案,可以介绍三种方案。 最简单的方案当然是flex布局: .father {     display: flex;     justify-content: center;     align-items: center; } .son {    ... } 绝对定位配合margin:auto,的实现方案: .father {     position: ...

CSS基础应用:介绍几种常见的水平垂直居中实现方案

常见的水平垂直居中实现方案,可以介绍三种方案。

最简单的方案当然是flex布局:

.father {
    display: flex;
    justify-content: center;
    align-items: center;
}
.son {
   ...
}

绝对定位配合margin:auto,的实现方案:

.father {
    position: relative;
}
.son {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
}

绝对定位配合transform实现:

.father {
    position: relative;
}
.son {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

原文链接:https://www.icz.com/technicalinformation/web/html/2024/03/10000.html,转载请注明出处~~~

0

评论0

请先

注意:请收藏好网址www.icz.com,防止失联!站内免费资源持续上传中…!赞助我们
没有账号? 注册  忘记密码?