CSS子元素在父元素中水平垂直居中的几种方法

1. 水平居中(margin: auto;)子父元素宽度固定,子元素上设置 margin: auto; 子元素不能设置浮动,否则居中失效。

        #div1{
            width: 300px;
            height: 300px;
            border: 1px solid red;
        }
        #div1 p {
            width: 100px;
            height: 100px;
            background-color: green;

            /*float: left;    !*如果设置了浮动,则自动居中就会失效。*!*/
            margin: 0 auto;
        }

        <div id="div1">
            <p></p>
        </div>

 

2. 水平居中,子父元素宽度固定,父元素设置 text-align: center; 
子元素设置 display: inline-block; 子元素不能设置浮动,否则居中失效。 
如果将元素设置为 inline , 则元素的宽高设置会失效,这就需要内容来撑起盒子

       #div2 {
            width: 300px;
            height: 300px;
            border: 1px solid red;

            /*position: relative;*/
            text-align: center;
        }
        #div2 p{
            width: 100px;
            height: 100px;
            background-color: green;
            margin: 0;

            /*float: left;     !*如果设置了浮动,则自动居中就会失效。*!*/
            display: inline-block;
            /*display: inline;*/
            /*display: inline-flex;*/
        }    

        <div id="div2">
            <h4>其他内容</h4>
            <p></p>
            <h3>其他内容</h3>
        </div>    

 

1. 水平垂直居中,子元素相对于父元素绝对定位, 
子元素top,left设置为50%,子元素margin的top,left减去自身高,宽的一半。

        #div3 {
            width: 300px;
            height: 300px;
            border: 1px solid red;

            position: relative;
        }
        #div3 p {
            width: 100px;
            height: 100px;
            background-color: green;

            /*margin-top: 10%;      !*百分比相对于父元素*!*/
            /*padding-top: 10%;        !*百分比相对于父元素*!*/
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -50px;
            margin-left: -50px;
        }

        <div id="div3">
            <p></p>
            <h3>其他内容</h3>
        </div>

 

2. 水平垂直居中,子元素相对于父元素绝对定位, 
将子元素的top,right,bottom,left均设为0,然后设置子元素 margin: auto;

       #div4{
            width: 300px;
            height: 300px;
            border: 1px solid red;

            position: relative;
        }
        #div4 p{
            width: 100px;
            height: 100px;
            background-color: green;

            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }

        <div id="div4">
            <p></p>
            <h3>其他内容</h3>
        </div>

 

3. 水平垂直居中,父元素设置 display: table-cell; vertical-align: middle; 
子元素设置 margin: auto; 
这种方式是让所有的子元素作为一个整体垂直居中,并不能单独指定某一个子元素居中

        #div5{
            width: 300px;
            height: 300px;
            border: 1px solid red;

            display: table-cell;
            vertical-align: middle;
        }
        #div5 p{
            width: 100px;
            height: 100px;
            background-color: green;

            margin: auto;
        }

        <div id="div5">
            <p></p>
        </div>

 

4. 水平垂直居中,子元素相对定位,top,let设为50%,transform: translate(-50%, -50%); 
这种方式并不会释放子元素在文档中所占的位置。

        #div6{
            width: 300px;
            height: 300px;
            border: 1px solid red;
        }
        #div6 p {
            width: 100px;
            height: 100px;
            background-color: green;
            margin: 0;

            position: relative;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        <div id="div6">
            <p></p>
            <h3>其他内容</h3>
        </div>

 

5. 水平垂直居中,子元素相对于父元素绝对定位, 
子元素top,let设为50%,transform: translate(-50%, -50%); 
这种方式会释放子元素在文档中所占的位置

        #div7{
            width: 300px;
            height: 300px;
            border: 1px solid red;

            position: relative;
        }
        #div7 p {
            width: 100px;
            height: 100px;
            background-color: green;
            margin: 0;

            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        <div id="div7">
            <p></p>
            <h3>其他内容</h3>
        </div>

 

6. 水平垂直居中,父元素设置 display: flex; justify-content: center; align-items: center; 
justify-content: center; 是让所有的子元素作为一个整体居中。

       #div8{
            width: 300px;
            height: 300px;
            border: 1px solid red;

            display: flex;
            justify-content: center;
            align-items: center;
        }
        #div8 p{
            width: 100px;
            height: 100px;
            background-color: green;
            margin: 0;
        }

        <div id="div8">
            <p></p>
        </div>

转载于:https://www.cnblogs.com/yingtoumao/p/11541256.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 实现垂直居一直是Web前端开发比较棘手的问题。下面介绍几种常用的CSS垂直居方法。 一、利用flex布局实现垂直居 元素设置display:flex; align-items:center; justify-content:center; 实现子元素的垂直居。 二、利用绝对定位实现垂直居子元素设置为绝对定位,通过设置top:50%; transform:translateY(-50%);实现子元素的垂直居。 三、利用table-cell属性实现垂直居元素的display属性设置为table-cell,vertical-align属性设置为middle,实现子元素的垂直居。 四、利用line-height属性实现垂直居元素子元素的line-height属性设置为相同的值,并将子元素设置为display:inline-block,实现子元素的垂直居。 综上所述,以上列举了常用的四种方法来实现CSS垂直居,根据需要选择合适的方法来解决垂直居问题。 ### 回答2: 在网页设计,垂直居是一个比较常见的需求。但是,实现垂直居并不是一件容易的事情。在CSS,有几种方法可以实现垂直居,下面介绍一些常用的方法: 1.使用flexbox Flexbox是一种强大的CSS布局模式,可以很容易地实现垂直居。通过将容器的display属性设置为flex,然后将align-items属性设置为center,就可以将内容垂直居。 ``` .container { display: flex; align-items: center; justify-content: center; } ``` 2.使用position 在容器使用绝对定位可以实现垂直居。将子元素的position属性设置为absolute,并将top和bottom都设置为0,然后将margin属性设置为auto,这样就可以将内容垂直居。 ``` .container { position: relative; } .child { position: absolute; top: 0; bottom: 0; margin: auto; } ``` 3.使用table-cell 将容器的display属性设置为table,子元素的display属性设置为table-cell,并将vertical-align属性设置为middle,就可以将内容垂直居。 ``` .container { display: table; } .child { display: table-cell; vertical-align: middle; } ``` 4.使用transform 使用transform属性可以让内容在垂直方向上居。将子元素的position属性设置为absolute,然后将top和left都设置为50%,再使用transform: translate(-50%, -50%),就可以将内容垂直居。 ``` .container { position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } ``` 以上是一些常用的CSS垂直居方法,可以根据具体需求选择合适的方法。 ### 回答3: 在网页设计,垂直居元素是一个非常常见的需求,而CSS也提供了多种方法实现垂直居。下面我们来介绍一些常用的垂直居方法。 1. 使用Flexbox布局 Flexbox是CSS3引入的一种灵活的基于容器和项目的布局方式。我们可以通过设置容器的display属性为flex,再设置justify-content和align-items属性来实现元素的水平和垂直居。 例如,将含有一个子元素的容器水平和垂直居,可以将CSS代码写成这样: ``` .container { display: flex; justify-content: center; align-items: center; } .container > div { width: 200px; height: 100px; background-color: #f00; } ``` 2. 使用绝对定位和负边距 我们可以将要居元素使用绝对定位,然后设置top、bottom、left、right属性为0,同时使用margin:auto来实现水平居。 例如: ``` .parent { position: relative; } .child { position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; width: 200px; height: 100px; background-color: #f00; } ``` 但这种方法需要在元素声明定位属性为relative或absolute。 3. 使用transform 我们可以使用CSS3的transform属性,将要垂直居元素的translateY属性设置为-50%。 例如: ``` .parent { position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translateY(-50%); width: 200px; height: 100px; background-color: #f00; } ``` 这种方法同样需要在元素声明定位属性为relative或absolute。 4. 使用flex布局和auto margin Flexbox布局使用auto margin来实现水平和垂直居。 例如: ``` .parent { display: flex; } .child { margin: auto; width: 200px; height: 100px; background-color: #f00; } ``` 这种方法非常简单,但需要注意元素需要设置为flex布局。 总的来说,以上这些方法都可以实现垂直居,可以根据具体情况选择适合自己的方式。同时,我们也可以根据需求结合使用不同的方法实现垂直居
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值