当前位置: 首页 > article >正文

水平垂直居中的几种方法(总结)

  1. 1.使用 flexbox 的 justify-content 和 align-items

  2. .parent {
      display: flex;
      justify-content: center;  /* 水平居中 */
      align-items: center;      /* 垂直居中 */
      height: 100vh;            /* 需要指定高度 */
    }
    

  3. 2.使用 grid 的 place-items: center

  4. .parent {
      display: grid;
      place-items: center;  /* 水平和垂直同时居中 */
      height: 100vh;
    }
    

  5. 3.使用 position: absolute 和 transform

  6. .parent {
      position: relative;
      height: 100vh;
    }
    
    .child {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    

  7. 4.使用 table-cell 和 vertical-align: middle

  8. .parent {
      display: table;
      width: 100%;
      height: 100vh;
      text-align: center;          /* 水平居中 */
    }
    
    .child {
      display: table-cell;
      vertical-align: middle;      /* 垂直居中 */
    }
    

    PS:基本没用过,这个太老了

  9. 5.使用 margin: auto(子元素固定宽高)

  10. .parent {
      display: block;
      height: 100vh;
    }
    
    .child {
      width: 200px;
      height: 200px;
      margin: auto;
    }
    

  11. 6.使用 line-height 和 text-align: center(仅限单行文本)

  12. .parent {
      height: 100vh;
      line-height: 100vh;  /* 行高等于容器高度 */
      text-align: center;  /* 水平居中 */
    }
    
    .child {
      display: inline-block;
      vertical-align: middle;
    }
    

  13. 7.使用 inline-block 和 text-align: center

  14. .parent {
      text-align: center;   /* 水平居中 */
      height: 100vh;
    }
    
    .child {
      display: inline-block;
      vertical-align: middle;  /* 垂直居中 */
      margin: auto;
    }
    

  15. 8.使用 min-height 和 min-width + flexbox

  16. .parent {
      display: flex;
      justify-content: center; /* 水平居中 */
      align-items: center;     /* 垂直居中 */
      min-height: 100vh;
    }
    
    .child {
      min-width: 200px;
      min-height: 200px;
    }
    

  17. 9.使用 vh 单位

  18. .parent {
      height: 100vh;
      display: flex;
      justify-content: center;  /* 水平居中 */
      align-items: center;      /* 垂直居中 */
    }
    

  19. 10.使用 absolute + display: flex(嵌套居中)

  20. .parent {
      position: relative;
      height: 100vh;
    }
    
    .child {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      display: flex;
      justify-content: center;
      align-items: center;
    }
    

  21. 11.使用 display: block 和 margin: auto(适用于固定宽高的元素)

  22. .parent {
      display: block;
      height: 100vh;
    }
    
    .child {
      width: 200px;
      height: 200px;
      margin: auto;
    }
    

  23. 12.使用 text-indent 和 white-space: nowrap(适用于文字)

  24. .parent {
      text-align: justify;
      white-space: nowrap;
      text-indent: 50%;
      transform: translateX(-50%);
      height: 100vh;
    }
    

 


http://www.kler.cn/news/305348.html

相关文章:

  • 【浅谈】单片机基本概念
  • 在WPF中使用矢量图标
  • 【ArcGISProSDK】初识
  • Vue Router push方法的使用
  • vm虚拟机中ubuntu连不上网络,网络图标消失解决办法
  • 技术成神之路:设计模式(十三)访问者模式
  • 2.1 溪降技术:溪降运动的定义
  • 链路层和交换网_计算机网络
  • vue+el-table 可输入表格使用上下键进行input框切换
  • Qt常用控件——QSpinBox
  • STM32的寄存器深度解析
  • Mysql的高级查询:SQL关联查询(内连接/外连接/自连接)/子查询
  • uni-app和Node.js使用uni-push2.0实现通知栏消息推送功能
  • Centos挂载和删除nfs
  • python选择排序算法
  • 基于Java+Mysql实现(web)大型企业管理系统
  • API安全测试 | Postman + BurpSuite 配置证书代理
  • chapter14-集合——(List)——day17
  • 828华为云征文|部署在线文件管理器 Spacedrive
  • 大数据-132 - Flink SQL 基本介绍 与 HelloWorld案例
  • 【LLM多模态】CogVideoX文生视频模型结构和训练过程
  • 【openGauss】检查工具gs_check,gs_checkperf的应用
  • Redisson 异步释放锁
  • 什么是485无线通信
  • 磁盘写操作压力测试工具的设计与实现
  • ChatGLM3模型搭建教程
  • OpenCV结构分析与形状描述符(20)计算一个包围给定点集的最小外接圆函数minEnclosingCircle()的使用
  • 研1日记12
  • C++系列-函数对象/仿函数
  • [网络]TCP/IP协议 之 网络层IP协议(3)