css - 在绝对定位的情况下怎么让div居中呢?
怪我咯
怪我咯 2017-04-17 13:43:56
0
5
915
<html>
<head>
    <title></title>
    <style type="text/css">
    #p1{width: 300px;height: 200px;background-color: blue;position: absolute;}
    #p2{width: 50px;height: 50px;background-color: red;margin: 0 auto;position: absolute;bottom:20px;}
    </style>
</head>
<body>
<p id="p1">
    <p id="p2"></p>
</p>
</body>
</html>
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(5)
黄舟
position:absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%)

This method is more suitable for situations where the width and height of itself and the width and height of the container are unclear

Additionally, I have summarized several centering methods:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    .father {
        width: 100px;
        height: 100px;
        border: 1px solid red;
        margin: 20px;
    }

    .child {
        width: 50px;
        height: 50px;
        border: 1px solid red;
        margin-left: auto;
        margin-right: auto;
    }
/*
    方法一:
    对子元素使用margin居中 
    使用场景:
    + 父元素高度固定;
    + 子元素高度固定
    简单粗暴,适用于子元素和父元素的宽高都固定的情况。
*/

    .method1 .child {
        margin-top: 25px;
    }
    /*
    方法二:
    适用于子元素高度和宽度固定,父元素高度不固定
 */

    .method2 {
        position: relative;
    }

    .method2 .child {
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -25px;
        margin-left: -25px;
    }
    /*
   方法三:
   子元素和父元素高度不定
   利用transform来调整位置
     */

    .method3 {
        position: relative;
    }

    .method3 .child {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    /*
    方法四:
    使用flex布局,利用flex-direction属性*/

    .method4 {
        display: flex;
        justify-content: center;
        flex-direction: column;
    }
    /*
    方法五:
    使用flex布局,利用align-items属性*/

    .method5 {
        display: flex;
        display: -webkit-flex;
        align-items: center;
    }
    /*
    方法六:
    使用table-cell布局
     */

    .method6 {
        display: table-cell;
        vertical-align: middle;
        text-align: center;
    }

    .method6 .child {
        vertical-align: middle;
    }
    </style>
</head>

<body>
    <p class="father method1">
        <p class="child"></p>
    </p>
    <p class="father method2">
        <p class="child"></p>
    </p>
    <p class="father method3">
        <p class="child"></p>
    </p>
    <p class="father method4">
        <p class="child"></p>
    </p>
    <p class="father method5">
        <p class="child"></p>
    </p>
      <p class="father method6">
        <p class="child"></p>
    </p>
</body>

</html>

Final effect

迷茫

Set the left value

洪涛
#p2 {
    width: 50px;
    height: 50px;
    background-color: red;
    margin: 0 auto;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -25px;
    margin-top: -25px;
}
阿神
#p2{
    width: 50px;
    height: 50px;
    background-color: red;
    margin:auto;
    position: absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    }
左手右手慢动作

Don’t know how to search? You can find the answer by searching in the community, so why repeat so many questions.

@_qunshan_

@_qunshan_’s answer

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!