码农知识堂 - 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版本大屏可视化解决方案。敬请期待吧~~~❤️

  • 相关阅读:
    在 macOS 上安装 Rust 开发环境并运行第一个程序的详细步骤
    I/O设备管理
    使用 C++ 部署深度学习模型快速上手方案
    给运行中的docker容器挂载目录——筑梦之路
    Kubernetes弃用Docker,我们该怎么办?
    Vue2.0中的$watch、$set、$delete源码解析
    前端基础知识点
    应用程序主题生成很简单!界面控件DevExtreme有现成的主题生成器
    netstat 竟然还能这么玩儿?
    JavaSE从基础到入门:抽象类和接口
  • 原文地址:https://blog.csdn.net/weixin_42974827/article/details/126761400
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号