Skip to content
静心静心
HOME
DoubtfulCases
github icon
  • CSS

    • 标签
      • a标签
        • Label 标签
          • background
            • background属性
              • border和outline
                • borderAndoutline
                  • css - padding
                    • css 单位
                      • css 所有的居中(仅限参考)
                        • 1、水平居中
                          • 2、垂直居中
                            • 3 水平加垂直居中
                            • CSS 之 重构 回流
                              • Css选择器 权重
                                • css选择器
                                  • CSS之阴影
                                    • css之float
                                      • css重点
                                        • grid布局
                                          • HTML基础标签+表单
                                            • table布局

                                            css 所有的居中方式(仅限参考)

                                            author iconYYtimer icon大约 2 分钟

                                            此页内容
                                            • 1、水平居中
                                            • 2、垂直居中
                                            • 3 水平加垂直居中

                                            # css 所有的居中方式(仅限参考)

                                            <div>
                                            	<div><div>
                                            </div>
                                            
                                            1
                                            2
                                            3

                                            # 1、水平居中

                                            # 方式一(推荐)
                                              div:nth-of-type(1){
                                                    width: 100px;
                                                    height: 100px;
                                                    border: 1px solid black;
                                                }
                                                div:nth-of-type(1)>div{
                                                    margin:  0 auto;
                                                    width:50px;
                                                    height: 40px;
                                                    border: 1px solid black;
                                                }
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6
                                            7
                                            8
                                            9
                                            10
                                            11
                                            # 方式二:子元素设置为display:inline-block(不推荐)
                                                div:nth-of-type(1){
                                                    width: 100px;
                                                    height: 100px;
                                                    border: 1px solid black;
                                                    text-align: center;
                                                }
                                                div:nth-of-type(1)>div{
                                                    display: inline-block;
                                                    width:50px;
                                                    height: 40px;
                                                    border: 1px solid black;
                                                }
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6
                                            7
                                            8
                                            9
                                            10
                                            11
                                            12
                                            # 方式三 利用绝对定位,父亲设置相对定位,左右为设置为0
                                            div:nth-of-type(1){
                                                    position: relative;
                                                    width: 100px;
                                                    height: 100px;
                                                    border: 1px solid black;
                                                    text-align: center;
                                                }
                                                div:nth-of-type(1)>div{
                                                    position: absolute;
                                                    left:0;
                                                    right: 0;
                                                    margin: auto;
                                                    width:50px;
                                                    height: 40px;
                                                    border: 1px solid black;
                                                }
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6
                                            7
                                            8
                                            9
                                            10
                                            11
                                            12
                                            13
                                            14
                                            15
                                            16
                                            # 方式四 利用flex布局来实现
                                                div:nth-of-type(1){
                                                    display: flex;
                                                    flex-direction: row;
                                                    justify-content: center;
                                                    width: 100px;
                                                    height: 100px;
                                                    border: 1px solid black;
                                                    text-align: center;
                                                }
                                                div:nth-of-type(1)>div{
                                                    width:50px;
                                                    height: 40px;
                                                    border: 1px solid black;
                                                }
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6
                                            7
                                            8
                                            9
                                            10
                                            11
                                            12
                                            13
                                            14

                                            # 2、垂直居中

                                            # 方式一 利用绝对定位
                                                div:nth-of-type(1){
                                                   position: relative;
                                                    width: 100px;
                                                    height: 100px;
                                                    border: 1px solid black;
                                                    text-align: center;
                                                }
                                                div:nth-of-type(1)>div{
                                                    position: absolute;
                                                    top:0;
                                                    bottom:0;
                                                    margin:auto;
                                                    width:50px;
                                                    height: 40px;
                                                    border: 1px solid black;
                                                }
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6
                                            7
                                            8
                                            9
                                            10
                                            11
                                            12
                                            13
                                            14
                                            15
                                            16
                                            # 方式二 利用flex布局来实现
                                              div:nth-of-type(1){
                                                    display: flex;
                                                    align-items: center;
                                                    width: 100px;
                                                    height: 100px;
                                                    border: 1px solid black;
                                                    text-align: center;
                                                }
                                                div:nth-of-type(1)>div{
                                                  
                                                    width:50px;
                                                    height: 40px;
                                                    border: 1px solid black;
                                                }
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6
                                            7
                                            8
                                            9
                                            10
                                            11
                                            12
                                            13
                                            14

                                            # 3 水平加垂直居中

                                            # 1、对内容进行居中

                                            ​

                                            text-align:center
                                            
                                            1

                                            ​ 利用CSS进行元素的水平居中,比较简单,行级元素设置其父元素的text-align center,

                                            line-height:200px #这里的200px为行的高度。自然内容就可以居中
                                            
                                            1

                                            ​

                                            # 2、使用display:flex 布局

                                             div:nth-of-type(1){
                                                    display: flex;
                                                    flex-direction: row;
                                                    justify-content: center;
                                                  	align-items: center;
                                                    }
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6

                                            # 3、使用定位position 来居中

                                            # 方式一(不推荐)
                                            div:nth-of-type(1){
                                                    position: relative;
                                                    width: 100px;
                                                    height: 100px;
                                                    border: 1px solid black;
                                                }
                                                div:nth-of-type(1)>div{
                                                    position: absolute;
                                                    top:50%;
                                                    left:50%;
                                                    margin-top:-20px;
                                                    margin-left: -25px;
                                                    width:50px;
                                                    height: 40px;
                                                    border: 1px solid black;
                                                }
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6
                                            7
                                            8
                                            9
                                            10
                                            11
                                            12
                                            13
                                            14
                                            15
                                            16
                                            # 方式二(推荐)
                                                div:nth-of-type(1){
                                            
                                            
                                                    position: relative;
                                                    width: 100px;
                                                    height: 100px;
                                                    border: 1px solid black;
                                                }
                                            
                                                div:nth-of-type(1)>div{
                                                    position: absolute;
                                                    top:0;
                                                    left: 0;
                                                    right: 0;
                                                    bottom:0;
                                                    margin:auto;
                                                    width:50px;
                                                    height: 40px;
                                                    border: 1px solid black;
                                                }
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6
                                            7
                                            8
                                            9
                                            10
                                            11
                                            12
                                            13
                                            14
                                            15
                                            16
                                            17
                                            18
                                            19
                                            20
                                            # 方式三:display:table

                                            利用dispaly对对div中的子元素进行居中

                                              .box1{
                                                    display:table;
                                                    width:400px;
                                                    height: 500px;
                                                    border:1px solid black;
                                                }
                                                .box1 p{
                                                  	 display: table-cell; 
                                                    vertical-align: middle;
                                                    
                                                }
                                            
                                            1
                                            2
                                            3
                                            4
                                            5
                                            6
                                            7
                                            8
                                            9
                                            10
                                            11
                                            edit icon编辑此页open in new window
                                            上一页
                                            css 单位
                                            下一页
                                            CSS 之 重构 回流
                                            傻瓜都能写出计算机可以理解的代码。唯有能写出人类容易理解的代码的,才是优秀的程序员。
                                            Copyright © 2022 YY

                                            该应用可以安装在您的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和您的操作系统安全地进行交互。

                                            详情