首页 > DIV/CSS > Div+Css网页居中

Div+Css网页居中

2011年12月13日 Xialu 发表评论 阅读评论

其实网页布局的方法有很多,css出现之前,人们一直是使用Table标签来进行网页布局!但是随着css的发展,Table布局方法几乎被淘汰!相对于Table,DIV+CSS具有更多的优势!

我们知道,在Table中,要居中一个网页及相关内容是十分简单的!直接添加一个属性就可以了!!但是在具有更多优势的div+css中怎么设置?下面来分部说明!

整个网页居中:

body{

margin-left:auto;

margin-right:auto;

}

单行DIV居中

#divIDName{

margin-left:auto;

margin-right:auto;

text-align:center;

}

多个DIV在一行内容的居中[下面以三个div为例说明]

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
body {text-align:center}
#outer {
    width:60%;
    background:#ffffcc;
    margin:auto;
    text-align:center;
}
.inner {
    width:100px;
    height:100px;
    margin:5px;
    border:1px solid #000;
}
* html .inner {display:inline}/* for ie*/
html>body #outer {display:table}/*for mozilla */
html>body .inner {display:table;float:left}/*for mozilla */
@media all and (min-width: 0px){/* opera 7 styles */
html>body .inner {display:inline-block;float:none;}
}

</style>
</head>
<body>
<!– force quirks mode by using the xml pro-logue –>
<div id="outer">
  <div class="inner">test</div>
  <div class="inner">2</div>
  <div class="inner">3</div>
  <br style="clear:both" />
</div>
</body>
</html>

上面的代码,并没有使div块居中.如果要是div块也居中的话,你可以使用div嵌套方法:即在几个div块外面加上一层div!

转载请以链接的形式注明原文地址:Div+Css网页居中来自67博客
分类: DIV/CSS 标签: , , ,
分享
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.