码迷,mamicode.com
首页 > 其他好文 > 详细

垂直居中

时间:2017-03-25 15:41:37      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:title   margin   大神   play   content   head   utf-8   targe   lin   

关于垂直居中的方法已经老生常谈了,这里只记录下自己最近发现的垂直居中方法。

1、通过vertival-align:middle:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .wrap{
            width: 1000px;
            height: 500px;
            margin: 0 auto;
            text-align: center;
            border: 1px solid #ccc;
        }
        .text{
            vertical-align: middle;
        }
        .text:before{
            content: ‘‘;
            display: inline-block;
            width: 1px;
            height: 100%;
            margin-left: -1px;
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <span class="text">it is a word</span>
    </div>
</body>
</html>

这种方法是通过元素与伪元素设置vertical-align:middle来实现的,很多朋友对这个属性都是一知半懂,可以看看这位大神的文章:http://www.zhangxinxu.com/wordpress/2010/05/我对css-vertical-align的一些理解与认识(一)/

2、通过绝对定位与margin:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        .wrap{
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            width: 500px;
            height: 300px;
            border: 1px solid #ccc;
            margin: auto
        }
    </style>
</head>
<body>
    <div class="wrap">is is a word</div>
</body>
</html>

这个就不必多说了,太基础。

还有很多方法可以实现,比如通过表格之类的。

 

垂直居中

标签:title   margin   大神   play   content   head   utf-8   targe   lin   

原文地址:http://www.cnblogs.com/11lang/p/6617058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!