指引网

当前位置: 主页 > 网页制作 > CSS >

css元素垂直水平居中实例

来源:网络 作者:佚名 点击: 时间:2017-06-21 21:32
[摘要]  本文章来给大家介绍在css元素垂直水平居中的一些实现方法与相关实例了,[原理] 设定宽度和高度,父节点为 position:relative; CSS是这样写的.

/* 
    名称: 垂直 水平 居中
    用法:
        1. [原理] 设定宽度和高度,父节点为 position:relative; CSS是这样写的:
   
          position:absolute;left:50%;top:50%;
          margin-left:-元素自身宽度的一半;
          margin-top:-元素自身高度的一半;
   
        2. [原理] table, 用起来就更简单了, 只要添加:
   
          text-algin:center;
          vertical-align:middle;
   
        3. [方法] 提供模板化的CSS class方法, 规则如下:

 代码如下 复制代码

   
          <div class="sl-hvalign" style="width:500px"> <!-- 记得加宽度, 不要写行内样式, 这里只是一个提示 -->
                 <div class="sl-hvalign-cnt">
                         <div class="sl-hvalign-cnt-inner">
                               <!-- your code -->
                         </div>
                 </div>
          </div> <!-- .sl-hvalign -->
 */


.sl-hvalign{
    display:table;
    overflow:hidden;
    margin:0 auto;
    height: 100%;
    *position:relative;
}

.sl-hvalign-cnt{
    display:table-cell;
    vertical-align: middle;
    *position:absolute;
    top:50%;
}

.sl-hvalign-cnt-inner{
    *position:relative;
    *top:-50%;
}

单纯的图片垂直居中,写三层标签确实有些费,但是这不得不说是一种折中的方案,当有图片、有文字时,是一个比较好的选择。。

 代码如下 复制代码

<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<style type="text/css">
.out{width:400px;height:300px;margin:20px auto;display:table-cell;text-align:center;vertical-align:middle;background:#ccc;}
.out img{width:100px;height:100px;background:#fcc;}
</style>
</head>

<body>
<div class='out'>
<img src="" alt="" />
</div>
</body>
</html>


其他的CSS实现垂直居中的方法
A
在 content 元素外插入一个 div。设置此 div height:50%; margin-bottom:-contentheight;。
content 清除浮动,并显示在中间。
优点:适用于所有浏览器;没有足够空间时(例如:窗口缩小) content 不会被截断,滚动条出现
缺点:唯一我能想到的就是需要额外的空元素了

 代码如下 复制代码

#floater{float:left; height:50%; margin-bottom:-120px;}
#content{clear:both; height:240px; position:relative;}

<div id="floater"></div>
<div id="content">
Content here
</div>

B
使用了一个 position:absolute,有固定宽度和高度的 div。这个 div 被设置为 top:0; bottom:0;。但是因为它有固定高度,其实并不能和上下都间距为 0,因此 margin:auto; 会使它居中。使用 margin:auto;使块级元素垂直居中是很简单的。优点:简单;缺点:IE(IE8 beta)中无效;无足够空间时,content 被截断,但是不会有滚动条出现

 代码如下 复制代码

<!doctype html>
<html>
<head>
<title></title>
<meta charset='utf-8' />
<style type="text/css">
.outer{height:240px;width:500px;margin:20px auto 0;position:relative;background:#ccf;}
.inner{width:300px;height:100px;background:#9f9;position:absolute;top:0;bottom:0;left:0;right:0;margin:auto;text-align: center;}
.inner img{height:100px;}
</style>
</head>

<body>
<div class="outer">
<div class="inner">
<img src="bokan.png" alt="" />
</div>
</div>
</body>
</html>

C
使用绝对定位的 div,把它的 top 设置为 50%,top margin 设置为负的 content 高度。这意味着对象必须在 CSS 中指定固定的高度。
因为有固定高度,或许你想给 content 指定 overflow:auto,这样如果 content 太多的话,就会出现滚动条,以免content 溢出。
优点:适用于所有浏览器;不需要嵌套标签。缺点:没有足够空间时,content 会消失(类似div 在 body 内,当用户缩小浏览器窗口,滚动条不出现的情况)

 代码如下 复制代码

.content {
position:absolute;
top:50%;
height:240px;
margin-top:-120px; /* negative half of the height */
}

<div class="content">
Content goes here

</div>

------分隔线----------------------------