如何让子元素在父元素中水平垂直居中七种方法?

更新日期: 2019-04-07阅读: 3.6k标签: 居中

html:

<div class="container">
        <div class="box">green</div>
</div>


第一种:定位+margin:auto

.container {
        position: relative;
        width: 300px;
        height: 300px;
        background: yellow;
    }
    .box {
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        margin: auto;
        width: 100px;
        height: 100px;
        background: red;
    }

注意:兼容性较好,缺点:不支持IE7以下的浏览器


第二种:定位+margin-left+margin-top

.container {
        position: relative;
        width: 300px;
        height: 300px;
        background: yellow;
    }
    .box {
        position: absolute;
        left: 50%;
        top: 50%;
        margin-left: -50px;
        margin-top: -50px;
        width: 100px;
        height: 100px;
        background: red;
    }

注意:兼容性好;缺点:必须知道元素的宽高


第三种:定位+transfrom

.container {
        position: relative;
        width: 300px;
        height: 300px;
        background: yellow;
    }
    .box {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        width: 100px;
        height: 100px;
        background: red;
    }

注意:这是css3里的样式;缺点:兼容性不好,只支持IE9+的浏览器


第四种:弹性盒子

.container {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 300px;
        height: 300px;
        background: yellow;
    }
    .box {
        width: 100px;
        height: 100px;
        background: red;
    }

移动端首选


第五种:flex+margin: auto

.container {
        display: flex;
        width: 300px;
        height: 300px;
        background: yellow;
    }
    .box {
        margin: auto;
        width: 100px;
        height: 100px;
        background: red;
    }

移动端首选


第六种:形成table-cell,子元素设置display:inline-block

.container {
        display: table-cell;
        vertical-align: middle;
        text-align: center;
        width: 300px;
        height: 300px;
        background: yellow;
    }
    .box {
        display: inline-block;
        width: 100px;
        height: 100px;
        background: red;
    }

注意:兼容性:由于display:table-cell的原因,IE67不兼容


第七种:line-height+display:inline

.container {
        width: 300px;
        height: 300px;
        line-height: 300px;
        text-align: center;
        background: yellow;
    }
    .box {
        display: inline;
        background: red;
    }

链接: https://www.fly63.com/article/detial/2722

css样式——完美解决div水平居中及div水平垂直居中的方法总结

在我们网页开布局中,经常遇到div元素的垂直居中或者水平居中。这篇文章,我总结一下

css图片居中,通过纯css实现图片居中的多种实现方法

在网页布局中,图文排版是我们常用的,那么经常会遇到如何让图片居中显示呢,这篇文章将总结常用css实现图片居中的方法总结。

安卓移动端line-height垂直居中出现偏移的原因,及解决方法

目前在移动端安卓手机上使用line-height属性,让它的值等于height,结果发现是不居中的。出现了一定位置的偏移情况,如果略微只有两三个像素差距是看不出来的。

css中line-height的理解_介绍line-height实际应用

css中line-height行高是指文本行基线之间的距离,不同字体,基线位置不同。line-height只影响行内元素和其他行内内容,而不会直接影响块级元素。line-height实际应用:图片水平垂直居中、多行文本水平垂直居中、用line-height代替height

css垂直居中的几种实现方式

相比较水平居中,垂直居中比较复杂点。尤其是在实际页面中,有很多特殊的场景,导致水平居中实现起来比较麻烦。这篇文章旨在纪录一些我知道的居中方式

div元素宽度不定的情况下如何居中显示

方法一:兼容IE67,但是当元素宽度大于50%时,会出现滚动条。外层使用text-align为center是为了让里面的内联元素居中,很显然在外层设置text-align:center后

CSS 不定宽高的垂直水平居中方式总汇

垂直居中,在 CSS 中是一个老生常谈的问题,面试的时候也会时常被提及。所以,今天我们就来聊聊 9 种不同的居中方法。有常见的 flex、transform、absolute 等等。也有 CSS3 的网格布局。还有伪元素的方法,是的,你没有看错

CSS中实现图片垂直居中

在曾经的 淘宝UED 招聘 中有这样一道题目:使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中。当然出题并不是随意,而是有其现实的原因

CSS实现垂直居中的5种方法

利用 CSS 来实现对象的垂直居中有许多不同的方法,比较难的是选择那个正确的方法。我下面说明一下我看到的好的方法和怎么来创建一个好的居中网站。使用 CSS 实现垂直居中并不容易。有些方法在一些浏览器中无效。

设置 letter-spacing 后文字不能居中的解决方法

今天才发现,给文字设置 letter-spacing 再设置 text-align: center; ,文字会整体往左偏移,不能居中。 给文字嵌套了一个 span 标签,再选择文字可以看出,letter-spacing 给每个字的右边都加了一个间距。

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!