老板的手机收到一个红包,为何红包没居中?

本文做者@千古壹号。css

前言

老板的手机收到一个红包,为何红包没居中?html

如何让一个子元素在父容器里水平垂直居中?这个问题必考,在实战开发中,也应用得很是多。前端

你也许能顺手写出好几种实现方法。但大部分人的写法不够规范,经不起千锤百炼。换句话说:这些人也就面试的时候夸夸其谈,但真的上战场的时候,他们不敢这么写,也不知道怎么写最靠谱。面试

这篇文章中,咱们来列出几种常见的写法,最终你会明白,哪一种写法是最优雅的。bash

固然,我还会拿出实际应用中的真实场景来举例,让你感觉一下标准垂直居中的魅力微信

如何让一个行内元素(文字、图片等)水平垂直居中

行内元素的居中问题比较简单。markdown

行内元素水平居中

给父容器设置:函数

text-align: center;

复制代码

行内元素垂直居中

文字的行高 等于 盒子的高度,可让单行文本垂直居中。好比:oop

.father {
        height: 20px;
        line-height: 20px;
    }
复制代码

如何让一个块级元素水平垂直居中

这一段是本文的核心。如何让一个块级的子元素在父容器里水平垂直居中?有好几种写法。咱们一块儿来看看。布局

margin: auto 的问题

在 CSS 中对元素进行水平居中是很是简单的:若是它是一个行内元素,就对它的父容器应用 text-align: center;若是它是一个块级元素,就对它自身应用 margin: auto或者 margin: 0 auto

咱们都知道,margin: auto至关于margin: auto auto auto automargin: 0 auto至关于margin: 0 auto 0 auto,四个值分别对应上右下左。其计算值取决于剩余空间

可是,若是要对一个元素垂直居中,margin: auto就行不通了。

好比下面这段代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style> .father{ height: 500px; background: pink; } .son { width: 300px; height: 200px; background: red; margin: auto; } </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <script></script>
</body>
</html>

复制代码

上面的代码中,父元素和子元素都是定宽高的,即使在这种状况下,我给子元素设置 margin: auto,子元素依然没有垂直居中。

那还有没有比较好的通用的作法呢?

方式一:绝对定位 + margin(须要指定子元素的宽高,不推荐)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style> * { margin: 0; padding: 0; } .father{ position: relative; min-height: 500px; background: pink; } .son { position: absolute; width: 200px; height: 100px; background: red; top: 50%; left: 50%; margin-top: -50px; margin-left: -100px; } </style>
</head>
<body>
    <div class="father">
        <div class="son">子元素的内容</div>
    </div>
    <script></script>
</body>
</html>

复制代码

代码解释:咱们先让子元素的左上角居中,而后向上移动宽度的一半(50px),就达到了垂直居中的效果;水平居中的原理相似。

不足之处:要求指定子元素的宽高,才能写出 margin-topmargin-left 的属性值。

可是,在一般状况下,对那些须要居中的元素来讲,其宽高每每是由其内容来决定的,不建议固定宽高。

方式二:绝对定位 + translate(无需指定子元素的宽高,推荐)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style> * { margin: 0; padding: 0; } .father{ position: relative; min-height: 500px; background: pink; } .son { position: absolute; background: red; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style>
</head>
<body>
    <div class="father">
        <div class="son">子元素的内容</div>
    </div>
    <script></script>
</body>
</html>
复制代码

这种写法,在没有指定子元素宽高的状况下,也能让其在父容器中垂直居中。由于 translate() 函数中使用百分比值时,是以这个元素自身的宽度和高度为基准进行换算和移动的(动态计算宽高)。

方式3:flex 布局(待改进)

将父容器设置为 Flex 布局,再给父容器加个属性justify-content: center,这样的话,子元素就能水平居中了;再给父容器加个属性 align-items: center,这样的话,子元素就能垂直居中了。

代码举例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style> * { margin: 0; padding: 0; } .father{ display: flex; justify-content: center; align-items: center; min-height: 100vh; background: pink; } .son { background: red; } </style>
</head>
<body>
    <div class="father">
        <div class="son">子元素的内容</div>
    </div>
    <script></script>
</body>
</html>

复制代码

上面这种写法的,不足之处在于:给父容器设置属性justify-content: centeralign-items: center以后,致使父容器里的全部子元素们都垂直居中了(若是父容器里有多个子元素的话)。可我明明只想让指定的某个子元素居中,要怎么改进呢?

方式4: flex 布局 + margin: auto(推荐)

咱们只需写两行声明便可:先给父容器设置 display: flex,再给指定的子元素设置咱们再熟悉不过的 margin: auto,便可让这个指定的子元素在剩余空间里,水平垂直居中。大功告成。

代码举例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style> * { margin: 0; padding: 0; } .father{ display: flex; min-height: 100vh; background: pink; } .son { margin: auto; background: red; } </style>
</head>
<body>
    <div class="father">
        <div class="son">子元素的内容,想水平垂直居中</div>
        <div class="son2">这个元素不想水平垂直居中</div>
    </div>
    <script></script>
</body>
</html>
复制代码

请注意,当咱们给父容器使用 Flex 布局 时,子元素的margin: auto不只能让其在水平方向上居中,垂直方向上也是居中的

参考文章:探秘 flex 上下文中神奇的自动 margin

垂直居中的典型应用场景:红包幕帘/弹窗

问题引入

就拿“弹窗”这一点来讲,如今你们的弹窗都是各类样式、各类布局满天飞。不过进公司后,你们在第一次写弹窗以前,都会问一个问题:“弹窗这么通用的东西,没有一个规范吗?”说完以后,又默默写本身的有个性的弹窗去了。

建议你们在写弹窗的时候,不管如何,必定要严格采用水平居中、垂直居中的写法。

千万不要用 margin-top 这种距离屏幕顶部的距离来计算弹窗的位置,太搓了。不要让领导以为:“大家写了这么久的前端代码,连个弹窗都搞不定?”

移动端,红包幕帘/弹窗 居中的规范写法(很是标准)

移动端场景,这里提供一个 红包幕帘/弹窗 的居中写法。注意,是严格居中,很是标准。为何是移动端?你有见过PC网页端给你送红包的么?

在实战开发中,下面的这段代码,能够直接拿去用。注释详细,贴心无比。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style> /* 整个弹窗组件 */ .component_popup { position: fixed; top: 0; bottom: 0; left: 0; right: 0; z-index: 100; } /* 遮罩背景 */ .popup_mask { position: fixed; top: 0; bottom: 0; left: 0; right: 0; background: rgba(0, 0, 0, 0.7); } /* 弹窗区域(内容 + close):严格居中 */ .popup_content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } /* 弹窗的内容部分 */ .content_box { width: 15.45rem; height: 19.32rem; background: url(http://img.smyhvae.com/20191010_1500_red-packet.png) no-repeat; background-size: 15.45rem 19.32rem; } /* 弹窗的close图标 */ .content_close { width: 1.25em; height: 1.25em; background: url(http://img.smyhvae.com/20191010_1500_close.png) no-repeat; background-size: 1.25rem 1.25rem; margin: 0 auto; margin-top: 0.5rem; } </style>
    </head>
    <body>
        <div class="content">默认文档流中的页面主体</div>

        <div class="component_popup">
            <div class="popup_mask"></div>
            <div class="popup_content">
                <div class="content_box"></div>
                <div class="content_close"></div>
            </div>
        </div>
    </body>
</html>

复制代码

实现效果:

补充

一、若是你的页面中,有不少弹窗,建议将弹窗封装成一个抽象组件。

二、任何弹窗,都须要解决“滚动穿透”的问题,本文篇幅有限,请自行查阅。

最后一段

有些实现方式虽然简单,但必需要经得起千锤百炼。咱们要作到敬畏每一行代码,不能浮于表面。团队开发,要的不是个性,而是标准和规范


若是你以为这篇内容对你有价值,请点赞,并关注咱们的官网和咱们的微信公众号(WecTeam),每周都有优质文章推送:

WecTeam
相关文章
相关标签/搜索