• CSS水平垂直居中


    1. margin:auto

    元素有宽度和高度时,利用margin:auto设置元素水平垂直居中:
    在这里插入图片描述
    HTML代码如下:

    • 1
    • 2
    • 3

    CSS代码如下:

    .div1 {
      background-color: #eee;
      width: 200px;
      height: 200px;
      position: relative;
    }
    .div1 .center {
      width: 50px;
      height: 50px;
      background-color: forestgreen;
      position: absolute;
      margin: auto;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.position:absolute

    HTML代码:

    • 1
    • 2
    • 3

    当已知元素宽度和高度时,可以设置position:absolutemargin为负的宽高的一半,CSS代码:

    .div2 {
      background-color: #eee;
      width: 200px;
      height: 200px;
      position: relative;
      margin-top: 20px;
    }
    .div2 .center {
      width: 50px;
      height: 50px;
      background-color: rgb(34, 71, 139);
      position: absolute;
      left: 50%;
      top: 50%;
      margin-left: -25px;
      margin-top: -25px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    当已知元素宽度和高度时,也可以利用calc计算属性设置topleft,CSS代码:

    .center {
      width: 50px;
      height: 50px;
      background-color: rgb(34, 71, 139);
      position: absolute;
      left: calc(50% - 25px);
      top: calc(50% - 25px);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    当元素宽度和高度未知时,可以设置position:absolutetransform:transate(-50%,-50%),css代码:

    .center {
      width: 50px;
      height: 50px;
      background-color: rgb(34, 71, 139);
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    实现效果:
    在这里插入图片描述

    3.弹性盒子

    通过为其父元素设置display:flex来实现居中
    HTML代码:

    • 1
    • 2
    • 3

    CSS代码:

    .div4 {
      background-color: #eee;
      width: 200px;
      height: 200px;
      position: relative;
      margin-top: 20px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .div4 .center {
      width: 50px;
      height: 50px;
      background-color: rgb(240, 248, 166);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.利用水平对齐和行高

    设置text-alignline-height实现单行文本水平垂直居中。
    HTML代码:

    我要居中

    • 1
    • 2
    • 3

    CSS代码:

    .div5 {
      background-color: #eee;
      width: 200px;
      height: 200px;
      margin-top: 20px;
    }
    .div5 .center {
      text-align: center;
      line-height: 200px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5.grid

    HTML代码:

    我要居中

    • 1
    • 2
    • 3

    在网格项目中设置justify-selfalign-self或者margin:auto,css代码:

    .div6 {
      background-color: #eee;
      width: 200px;
      height: 200px;
      margin-top: 20px;
      display: grid;
    }
    .div6 .center {
      align-self: center;
      justify-self: center;
      /* margin: auto; */
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在网格容器上设置justify-itemsalign-itemsjustify-contentalign-content,css代码:

    .div6 {
      background-color: #eee;
      width: 200px;
      height: 200px;
      margin-top: 20px;
      display: grid;
      align-items: center;
      justify-items: center;
      /* align-content: center;
      justify-content: center; */
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    数字IC验证面试常见的问题汇总,你掌握了几个?
    ant javac任务的fork和executable属性
    springboot设置并获取启动参数
    React-路由导航
    Java---File详解
    基于图模型及SSL的推荐系统历年经典论文整理分享
    在3dmax建好的模型导入到模方映射纹理,这个模型偏移设置该如何设置?默认的话,模型会偏移
    【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
    Python3语法总结-基本数据类型①
    【网页设计】期末大作业html+css (个人生活记录介绍网站)
  • 原文地址:https://blog.csdn.net/Gary_888/article/details/126734595