CSS未知宽高元素水平垂直居中

方法一 
思路:显示设置父元素为:table,子元素为:cell-table,这样就可以使用vertical-align: center,实现水平居中
优点:父元素(parent)可以动态的改变高度(table元素的特性)
缺点:IE8以下不支持

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未知宽高元素水平垂直居中</title>
</head>
<style>

.parent1{
    display: table;
    height:300px;
    width: 300px;
    background-color: #FD0C70;
}
.parent1 .child{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    color: #fff;
    font-size: 16px;
}

</style>
<body>
    <div class="parent1">
        <div class="child">hello world-1</div>
    </div>
</body>
</html>

方法二:

思路:使用一个空标签span设置他的vertical-align基准线为中间,并且让他为inline-block,宽度为0
缺点:多了一个没用的空标签,display:inline-blockIE 6 7是不支持的(添加上:_zoom1;*display:inline)。
当然也可以使用伪元素来代替span标签,不过IE支持也不好,但这是后话了

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未知宽高元素水平垂直居中</title>
</head>
<style>
.parent2{
    height:300px;
    width: 300px;
    text-align: center;
    background: #FD0C70;
}
.parent2 span{
    display: inline-block;;
    width: 0;
    height: 100%;
    vertical-align: middle;
    zoom: 1;/*BFC*/
    *display: inline;
}
.parent2 .child{
    display: inline-block;
    color: #fff;
    zoom: 1;/*BFC*/
    *display: inline;
}

</style>
<body>
    <div class="parent1">
        <div class="child">hello world-1</div>
    </div>

    <div class="parent2">
        <span></span>
        <div class="child">hello world-2</div>
    </div>
</body>
</html>

方法三

思路:子元素绝对定位,距离顶部 50%,左边50%,然后使用css3 transform:translate(-50%; -50%)
优点:高大上,可以在webkit内核的浏览器中使用
缺点:不支持IE9以下不支持transform属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未知宽高元素水平垂直居中</title>
</head>
<style>
.parent3{
    position: relative;
    height:300px;
    width: 300px;
    background: #FD0C70;
}
.parent3 .child{
    position: absolute;
    top: 50%;
    left: 50%;
    color: #fff;
    transform: translate(-50%, -50%);
}
</style>
<body>
<div class="parent3">
        <div class="child">hello world-3</div>
    </div>
</body>
</html>

方法四:
思路:使用css3 flex布局
优点:简单 快捷
缺点:兼容不好吧,详情见:http://caniuse.com/#search=flex

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未知宽高元素水平垂直居中</title>
</head>
<style>
.parent4{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 300px;
    height:300px;
    background: #FD0C70;
}
.parent4 .child{
    color:#fff;
}
</style>
<body>div> <div class="parent4">
        <div class="child">hello world-4</div>
    </div>
</body>
</html>

 

时间: 2024-08-05 02:31:54

CSS未知宽高元素水平垂直居中的相关文章

未知宽高元素水平垂直居中

一.transform <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head> <body> <!--方法1:一个未知宽高的弹出框,水平垂直居中--> <div style="position:relative;width:100%;height:600px;border:1px solid #888">

网页元素居中攻略记_(3)已知宽高元素水平垂直居中

已知宽高元素水平垂直居中 方案 使用了position的absolute属来实现,在上篇文章的垂直居中的基础上加上水平居中 代码 index.html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>已知宽高元素水平垂直居中</title> <style> #container { posit

css/css3实现未知宽高元素的垂直居中和水平居中

其实在平常的一些布局中也经常有要实现元素的垂直居中和水平居中的的需要,下面来写几种css/css3实现的未知宽高元素的水平和垂直居中的写法 ps:不考虑兼容问题(下次会写js实现元素的水平and垂直居中 ) 第一种 css3的transform .ele{ /*设置元素绝对定位*/ position:absolute; /*top 50%*/ top: 50%; /*left 50%*/ left: 50%; /*css3 transform 实现*/ transform: translate(

未知宽高div水平垂直居中3种方法

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head> <body> <!--方法1:一个未知宽高的弹出框,水平垂直居中--> <div style="position:relative;width:100%;height:600px;border:1px solid #888">方法1 <div

未知宽高图片水平垂直居中在div

<BODY> <div class="box"> <span class="car"></span> <img src="images/01.jpg" title="car" /> </div> </BODY> .box { width: 100%; overflow: hidden; text-align: center; /*font-si

div+css实现未知宽高元素垂直水平居中

div+css实现未知宽高元素垂直水平居中.很多同学在面试的时候都会遇到这样的问题:怎么用div+css的方法实现一个未知宽高的弹出框垂直水平居中??如果用JS的话就好办了,但是JS的使用会对页面性能造成影响,而且能用CSS实现的干嘛用JS呢,嘿嘿 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>div+css实现未

不定宽高的水平垂直居中

不定宽高的水平垂直居中的两种方法: 1/ .mybox{position:absolute;top:50%;left:50%;z-index:3;-webkit-translate(-50%,50%);background:#fff;} 2/.parent{justify-content:center;align-items:center;display:-webkit-flex;}

网页元素居中攻略记_(5)未知宽高元素绝对居中

题外话 以前,我们要自适应全局居中,需要借助JS或者JQ来实现,现在有了CSS3就可以省去好多功夫了,为什么这么说!! 请看比较: 传统的绝对居中 #container{ position:abosolute; top:50%; left:50%; margin-left:-包含块宽度的一半(如 -300px ); margin-top: -包含块高度的一半; } 这种是实现了包含块的绝对居中,但是有一个问题,就是宽高度无法自适应(需固定宽高)-比如动态增加数据的时候,用这个就不大合适了-.这时

css未知宽高的盒子div居中的多种方法

不知道盒子大小.宽高时,如何让盒子上下左右居中? 应用场景:比如上传图片时,并不知道图片的大小,但要求图片显示在某盒子的正中央. 方法1:让4周的拉力均匀-常用 <!-- Author: XiaoWen Create a file: 2017-01-13 13:46:47 Last modified: 2017-01-13 14:05:04 Start to work: Finish the work: Other information: --> <!DOCTYPE html>