元素居中的几种方式

简介: 元素居中的几种方式

1:设置元素绝对定位,通过margin属性,移动自身宽高的一半

2:设置元素绝对定位,设置margin:auto

3:设置弹性布局,通过align-items: center;justify-content: center;实现

4:设置元素绝对定位,通过transform属性,移动自身宽高的一半

代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,body{width:100%;height:100%}
        /* 元素水平及垂直实现方式1 */
        #d1{
            position: relative;
            width:100%;
            height:100%;
            background-color:#f00;
        }
        .children{
            position:absolute;
            width:100px;
            height:100px;
            left:50%;
            top:50%;
            margin-left:-50px;
            margin-top:-50px;
            background-color:#fff;
        }
        /* 元素水平及垂直实现方式2 */
        #d1{
            position: relative;
            width:100%;
            height:100%;
            background-color:#f00;
        }
        .children{
            position:absolute;
            width:100px;
            height:100px;
            left:0;
            top:0;
            right:0;
            bottom:0;
            margin:auto;
            background-color:#fff;
        }
        /* 元素水平及垂直实现方式3 */
        #d1{
            width:100%;
            height:100%;
            background-color:#f00;
            display:flex;
            align-items: center;    /*垂直居中*/
            justify-content: center;    /*水平居中*/
        }
        .children{
            display: inline-block;
            width:100px;
            height:100px;
            background-color:#fff;
        }
        /* 元素水平及垂直实现方式4 */
        #d1{
            position: relative;
            width:100%;
            height:100%;
            background-color:#f00;
        }
        .children{
            position:absolute;
            width:100px;
            height:100px;
            background-color:#fff;
            left:50%;
            top:50%;
            transform: translate(-50% -50%);
        }
    </style>
</head>
<body>
    <div id="d1">
        <div class="children"></div>
    </div>
</body>
</html>


相关文章
|
2月前
【奇技淫巧】实现flex布局中,单独某个元素挪到右侧、底部,与其他元素排列不同,就像是个另类。(主要是用到margin-left和align-self)
【奇技淫巧】实现flex布局中,单独某个元素挪到右侧、底部,与其他元素排列不同,就像是个另类。(主要是用到margin-left和align-self)
|
18天前
|
前端开发
css元素实现垂直竖直万能居中
css元素实现垂直竖直万能居中
12 0
|
13天前
|
前端开发
css 图标和文字对齐 —— 垂直居中对齐,任意位置对齐
css 图标和文字对齐 —— 垂直居中对齐,任意位置对齐
16 2
|
13天前
|
前端开发 容器
css 动态文本对齐自适应 — 文本宽度小于容器宽度时居中对齐,文本宽度大于容器宽度时居左对齐
css 动态文本对齐自适应 — 文本宽度小于容器宽度时居中对齐,文本宽度大于容器宽度时居左对齐
11 2
|
23天前
|
移动开发 前端开发 HTML5
有关CSS中排版常见问题(清除默认样式问题 + 元素居中问题 + 元素之间的空白问题 + 行内块的幽灵空白问题)
有关CSS中排版常见问题(清除默认样式问题 + 元素居中问题 + 元素之间的空白问题 + 行内块的幽灵空白问题)
|
10天前
|
前端开发
css实战——清除列表中最后一个元素的下边距
css实战——清除列表中最后一个元素的下边距
14 0
|
2月前
|
前端开发 容器
css样式元素的相对定位,绝对定位,固定定位等元素定位运用技巧详解
css样式元素的相对定位,绝对定位,固定定位等元素定位运用技巧详解
|
2月前
|
前端开发 容器
css中元素水平居中的方式
css中元素水平居中的方式
56 0
|
11月前
|
容器
【元素水平垂直居中的方法有哪些?元素不定宽高呢?】
【元素水平垂直居中的方法有哪些?元素不定宽高呢?】
【元素水平垂直居中的方法有哪些?元素不定宽高呢?】
|
11月前
元素水平垂直居中的六种方法
元素水平垂直居中的六种方法
68 1