html 元素垂直水平居中

更新日期: 2020-09-19阅读: 1.6k标签: 居中

一、 不定宽高元素水平垂直居中

1、transform: translate()

<div class="wrapper"> 
    <p class="center">水平垂直居中</p>
</div>

.wrapper{   
    background-color: #eee;   
    height:200px; 
} 
.center{    
    position: relative;   
    width: 300px;   
    padding: 10px 20px;   
    background-color: #2c3e50;   
    color: #fff;   
    left: 50%;   
    right: 50%;   
    transform: translate(-50%, -50%); 
}

缺点:IE9以下不兼容


2、flex 布局,利用justify-content和align-items 实现居中

<div class="wrapper">   
<p class="center3">水平垂直居中</p> 
</div> 

.wrapper{ 
    height:200px;   
    background-color: #eee;   
    display: flex;   
    justify-content: center;   
    align-items: center; 
} 
.center2 {   
    position: relative;   
    width: 300px;   
    padding: 10px 20px;   
    background-color: #2c3e50;   
    color: #fff; 
}


3、使用table+table-cell实现垂直居中,display:inline-block;或margin: auto;实现水平居中

<div class="wrapper">  
    <div class="content">     
        <p class="center3">水平垂直居中</p>  
    </div>
</div> 

.wrapper{   
    background-color: #eee;   
    height: 200px;   
    width: 100%;   
    display: table; 
} 
.content {   
    display: table-cell;   
    text-align: center;   
    vertical-align: middle; 
} 
.center3 {   
    display: inline-block;   
    width: 300px;   
    padding: 10px 20px;   
    background-color: #2c3e50;   
    color: #fff; 
}


4、伪元素:after, :before 使用inline-block+ vertical-align:middle 对齐伪元素

<div class="wrapper">   
    <p class="center4">水平垂直居中</p> 
</div> 
.wrapper {   
    background-color: #eee;   
    height: 200px;   
    width: 100%;   
    position: relative; 
} 
.wrapper:after {   
    content: '';   
    display: inline-block;   
    vertical-align: middle;  
    height: 100%; 
} 
.center4 {   
    background-color:#2c3e50;   
    padding: 10px 20px;   
    color:#fff;   
    display: inline-block; 
}


5、writing-mode: 改变文字的显示方向

<div class="wrapper">   
    <div class="content">       
        <p class="center5">水平垂直居中</p>   
    </div> 
</div>

.wrapper {   
    background-color:#eee;   
    width: 100%;   
    height: 200px;   
    writing-mode: vertical-lr; 
} 
.content {   
    writing-mode: horizontal-tb;   
    display: inline-block;   
    width: 100%; 
} 
.center5 {   
    background-color:#2c3e50;   
    padding: 10px 20px;   
    color:#fff;   
    display: inline-block;   
    margin: auto;   
    text-align: left; 
}


二、 固定宽高元素水平垂直居中

6、absolute+ 负margin

<div class="wrapper">     
    <p class="center6">水平垂直居中</p>
</div> 
 .wrapper {   
     background-color: #eee;   
     height: 200px;   
     width: 100%;   
     position: relative; 
 } 
 .center6{   
     background-color: #2c3e50;   
     color: #fff;   
     width: 300px;   
     height: 50px;   
     line-height: 50px;   
     position: absolute;   
     top: 50%;   
     left: 50%;   
     margin-left: -150px;   
     margin-top: -25px; 
 }

设置绝对定位,left:50%; top: 50%;使当前元素的左上角处于父元素的中心位置, 再利用负margin使其中心位于父元素的中心。


7、absolute+ 上下左右等值

<div class="wrapper">     
    <p class="center7">水平垂直居中</p> 
</div> 
.wrapper {   
    background-color: #eee;   
    height: 200px;   
    width: 100%;   
    position: relative; 
}
.center7 {   
    background-color: #2c3e50;   
    color: #fff;  
    width: 300px;   
    height: 50px;   
    line-height: 50px;   
    position: absolute;   
    top: 0;   
    left: 0;   
    right: 0;   
    bottom: 0;   
    margin: auto; 
}

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

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

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

第一种:定位+margin:auto,注意:兼容性较好,缺点:不支持IE7以下的浏览器.第二种:定位+margin-left+margin-top注意:兼容性好;缺点:必须知道元素的宽高,第三种:定位+transfrom

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 实现垂直居中并不容易。有些方法在一些浏览器中无效。

点击更多...

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