原创设计 · PhotoShop · AI/CD · Fireworks · AutoCAD · 3DMAX · Flash · 网页教学 · 高精图库 · 笔刷滤镜 · 矢量素材 · 图片素材 · 模板素材 · 会员相册
加入VIP,下载精美素材 · 原创设计欣赏第七期· QQ空间代码 · QQ空间代码 · QQ空间站· 非主流图片 · QQ头像 · 繁体字非主流图片 · QQ个性签名 · QQ空间播放器

2009年最新设计图书素材低价热销
设计优秀作品第九期/新手学习贴
加入VIP,即送3000缘分币,每月500
 下载素材,加入VIP,享受更多权限
发新话题
打印

[Css+Div] 一行、多行文本垂直居中的CSS实例说明

一行、多行文本垂直居中的CSS实例说明

在表格布局时代,不需要过多的考虑垂直居中的问题,在单元格中,默认就是垂直居中的,一行文字是垂直居中,三行文字同样也会垂直居中。进行CSS网页布局,这样的形式改变了。文字默认是居于容器顶部。

如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>chinaz.com</title>
<style type="text/css">
#MrJin {
width:500px;
height:200px;
border:1px solid #03c;
text-align:center;
}
</style>
</head>
<body>
<div id="MrJin"><a href="http://www.chinaz.com/">CSS Web Design</a></div>
</body>
</html>


在这样的情况下,如何实现文字垂直居中呢。分为三种情况:

一、如果是单行文本,可以用行距来解决问题。

我们为它增加行距的定义,得到单行文本垂直居中的效果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>chinaz.com</title>
<style type="text/css">
#MrJin {
width:500px;
height:200px;
border:1px solid #03c;
text-align:center;
line-height:200px;
}
</style>
</head>
<body>
<div id="MrJin"><a href="http://www.chinaz.com/">CSS Web Design </a></div>
</body>
</html>



二、如果是多行文本,父容器不固定高度。

我们可以用padding来解决问题。

设置容器的padding上下为相同的固定值,容器的高度随着内容的增加而增加。

以此来实现多行文本的垂直居中。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>chinaz.com</title>
<style type="text/css">
#MrJin {
width:500px;
padding:50px 0;
border:1px solid #03c;
text-align:center;
}
</style>
</head>
<body>
<div id="MrJin"><p><a href="http://www.chinaz.com/">CSS Web Design </p><p>我们致力于为中文网站提供动力!</a></p></div>
</body>
</html>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>chinaz.com</title>
<style type="text/css">
#MrJin {
width:500px;
padding:50px 0;
border:1px solid #03c;
text-align:center;
}
</style>
</head>
<body>
<div id="MrJin"><a href="http://www.chinaz.com/">
<p>CSS Web Design</p>
<p>中国站长站</p>
<p>我们致力于为中文网站提供动力!</p>
</a></div>
</body>
</html>

三、如果是多行文本,父容器固定高度。

这就需要用到定位,而且需要给HTML增加标签。我们不提倡这样做。

但目前这个方法可以更好的解决问题。

在容器的内部需要增设两个容器,来实现这样的效果。

MrJin、MrJin_a和MrJin_b的设置分别如下:

#MrJin {   
    position:static;
    *position:relative;   
    height:300px;
    width:500px;
    border:1px solid #03c;
    *display:block!important;
    display:table!important;
}
#MrJin_a {
    position:static;
    *position:absolute;
    display:table-cell;
    vertical-align:middle;
    *display:block;
    top:50%;
    width:100%;
}
#MrJin_b {
    position:relative;
    top:-50%;
    text-align:center;
    width:100%;
}


这样设置以后,不管容器内的文本是一行,还是多行,都将会实现垂直居中对齐。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>chinaz.com</title>
<style type="text/css">
#MrJin {
position:static;
*position:relative;
height:300px;
width:500px;
border:1px solid #03c;
*display:block!important;
display:table!important;
}
#MrJin_a {
position:static;
*position:absolute;
display:table-cell;
vertical-align:middle;
*display:block;
top:50%;
width:100%;
}
#MrJin_b {
position:relative;
top:-50%;
text-align:center;
width:100%;
}
</style>
</head>
<body>
<div id="MrJin">
<div id="MrJin_a">
<div id="MrJin_b">
<a href="http://www.chinaz.com/">
CSS Web Design
</a>
</div>
</div>
</div>
</body>
</html>


或者:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>chinaz.com</title>
<style type="text/css">
#MrJin {
position:static;
*position:relative;
height:300px;
width:500px;
border:1px solid #03c;
*display:block!important;
display:table!important;
}
#MrJin_a {
position:static;
*position:absolute;
display:table-cell;
vertical-align:middle;
*display:block;
top:50%;
width:100%;
}
#MrJin_b {
position:relative;
top:-50%;
text-align:center;
width:100%;
}
</style>
</head>
<body>
<div id="MrJin">
<div id="MrJin_a">
<div id="MrJin_b">
<a href="http://www.chinaz.com/">
<p>CSS Web Design 我爱CSS</p>
<p>中国站长站</p>
<p>我们努力保持每天更新,为您提供最新最全的CSS网页布局教程。</p>
</a>
</div>
</div>
</div>
</body>
</html>




加入论坛VIP,下尽您想要的素材,点击进入!

如果您在做图或者看教程(PHOTOSHOP方面),遇到任何问题请到问题交流区提问,地址:http://www.missyuan.com/forum-41-1.html;我们会在第一时间帮助您解决问题,如果在教程后面跟帖,一律不给予解决!~


TOP

好东西  多谢楼主了




TOP

发新话题



关于本站 广告服务 联系我们 版权隐私 合作站点 网站地图 免责申明 管理团队

Powered by Discuz!6.0.0 Copyright © 2008 www.missyuan.com All rights reserved.