css3实现水平垂直居中------(特别注意,里边的固定还是不固定)

a,----定位方式(父元素宽高固定,子元素宽高固定)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div class="Father">
        <div class="children"></div>
    </div>
  <style lang="scss" scoped>
   .Father{
     position: relative;
   }
   .children{
     width: 50px;
     height: 50px;
     position: absolute;
     top: 0px;
     bottom: 0px;
     left: 0px;
     right: 0px;
     margin: auto;
   }
</style>

b, ----flex布局方式(父元素宽高不固定,子元素宽高不固定)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<div class="Father">
        <div class="children"></div>
    </div>
  <style lang="scss" scoped>
  .Father {
    border: 1px solid red;
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .children {
    width: 50px;
    height: 50px;
    border: 1px solid blue;
  }
</style>

c, ----transform方式(父元素宽高不固定,子元素宽高不固定)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<div class="Father">
        <div class="children"></div>
    </div>
  <style lang="scss" scoped>
  .Father {
    border: 1px solid red;
    height: 100px;
    position: relative;
  }
  .children {
    width: 50px;
    height: 50px;
    border: 1px solid blue;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);  /* 使用css3的transform来实现 */
  }
</style>

.

posted @   每天都要进步一点点  阅读(158)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· 从零开始开发一个 MCP Server!
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
历史上的今天:
2018-12-06 ant design pro (九)引入外部模块
2017-12-06 weex 项目开发(三) weexpack + weex-ui
点击右上角即可分享
微信分享提示