码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • css垂直居中的6种方式


    在线demo演示地址:https://rondsjinhuajin.github.io/demo/index.html

    个人博客主页:KinHKin的博客_CSDN博客-vue,中秋活动,性能优化领域博主

    使用人群:前端面试,日常开发小技巧

    目录

    1、效果演示如下 

    ​编辑

    1、使用display:flex实现

    2、使用position + transform: translate(-50%,-50%)实现

    3、使用position+margin减去子元素宽高的一半实现

    4、使用position+margin:auto实现

    5、使用grid+align-self+justify-self实现

    6、使用css-table实现

    7、总结


    1、效果演示如下 

     注意:一个元素要想实现垂直水平居中,必须要有一个参照物,这个参照物要么是父级div元素,要么就是body元素。本文为父元素高度未知,子元素垂直水平居中方案。以下所有的css都经过博主亲自测验通过才发出,请放心使用❤️

    1. <!-- dom元素 -->
    2. <div class="parent">
    3. 父元素
    4. <div class="child">子元素</div>
    5. </div>

    以下是css部分: 

    1、使用display:flex实现

    1. .parent {
    2. width: 100%;
    3. height: 100%;
    4. background-color: brown;
    5. display: flex;
    6. align-items: center;
    7. justify-content: center;
    8. }
    9. .child {
    10. width: 400px;
    11. height: 400px;
    12. background-color: blue;
    13. }

    2、使用position + transform: translate(-50%,-50%)实现

    1. .parent {
    2. width: 100%;
    3. height: 100%;
    4. background-color: brown;
    5. position: relative;
    6. }
    7. .child {
    8. width: 400px;
    9. height: 400px;
    10. background-color: blue;
    11. position: absolute;
    12. left: 50%;
    13. top: 50%;
    14. transform: translate(-50%,-50%);
    15. }

    3、使用position+margin减去子元素宽高的一半实现

    1. .parent {
    2. width: 100%;
    3. height: 100%;
    4. background-color: brown;
    5. /* 第一种 */
    6. /* display: flex;
    7. align-items: center;
    8. justify-content: center; */
    9. position: relative;
    10. }
    11. .child {
    12. width: 400px;
    13. height: 400px;
    14. background-color: blue;
    15. /* 第二种 */
    16. /* position: absolute;
    17. left: 50%;
    18. top: 50%;
    19. transform: translate(-50%,-50%); */
    20. /* 第三种 */
    21. position: absolute;
    22. left: 50%;
    23. top: 50%;
    24. margin: -200px 0 0 -200px;
    25. }

    4、使用position+margin:auto实现

    1. .parent {
    2. width: 100%;
    3. height: 100%;
    4. background-color: brown;
    5. /* 第一种 */
    6. /* display: flex;
    7. align-items: center;
    8. justify-content: center; */
    9. position: relative;
    10. }
    11. .child {
    12. width: 400px;
    13. height: 400px;
    14. background-color: blue;
    15. /* 第二种 */
    16. /* position: absolute;
    17. left: 50%;
    18. top: 50%;
    19. transform: translate(-50%,-50%); */
    20. /* 第三种 */
    21. /* position: absolute;
    22. left: 50%;
    23. top: 50%;
    24. margin: -200px 0 0 -200px; */
    25. /* 第四种 */
    26. position: absolute;
    27. top: 0;
    28. left: 0;
    29. right: 0;
    30. bottom: 0;
    31. margin: auto;
    32. }

    5、使用grid+align-self+justify-self实现

    注意:这种方式父元素要没有其他的子元素或者是内容

     效果图如下:

     代码如下:

    1. .parent {
    2. width: 100%;
    3. height: 100%;
    4. background-color: brown;
    5. display: grid;
    6. }
    7. .child {
    8. width: 400px;
    9. height: 400px;
    10. background-color: blue;
    11. align-self: center;
    12. justify-self: center;
    13. }

    6、使用css-table实现

    注意:这种方式其实是父元素要固定宽高,我这里是用vh来定,类似于百分比 

    1. .parent {
    2. width: 100vw;
    3. height: 100vh;
    4. background-color: brown;
    5. /* 第六种 */
    6. display: table-cell;
    7. text-align: center;
    8. vertical-align: middle;
    9. }
    10. .child {
    11. width: 400px;
    12. height: 400px;
    13. background-color: blue;
    14. /* 第六种 */
    15. display: inline-block;
    16. }

    7、总结

    这里实现了比较常用的垂直水平居中的方案,十分有用,希望对于面试必考题来说,只要熟练掌握前面三种,基本上是没有问题了,源码我会放在本人主页的资源里面,供大家免费下载,创作不易,关注一下,后续将带来vue版本大屏可视化解决方案。敬请期待吧~~~❤️

  • 相关阅读:
    无胁科技-TVD每日漏洞情报-2022-9-8
    Live555库结构及其核心概念
    热更新的代码实现
    个人杂项总结
    那些测试员面试中的“潜规则”,千万不要踩坑
    技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
    知识点6--Docker的镜像命令
    基于springboot+vue的毕业生实习与就业管理系统
    微服务【Ribbon负载均衡&源码解析&饥饿加载】第2章
    【从零开始学习Redis | 第三篇】在Java中操作Redis
  • 原文地址:https://blog.csdn.net/weixin_42974827/article/details/126761400
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号