我该如何将网页居中显示在屏幕上?就像stackoverflow网站上的页面一样?
我正在编写我的网站主页面,想使用HTML和CSS将主页面居中显示在屏幕中央,然后再使用CSS构建内部定位块。但是我无法让我的页面在中间可视化!
谢谢!
<!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></head>
<body>
<div id="page"> ... content ... </div>
</body>
</html>
CSS可以非常简单:
#page {
margin-left:auto;
margin-right:auto;
width:960px;
}
您还需要确保您有一个有效的文档类型声明(doctype),这样才能使它起作用。
<body>
中创建一个 <div>
,如下所示:<body>
<div id="main">
<!-- ... -->
</div>
</body>
并添加以下CSS:
body {
text-align: center; /* IE */
}
#main {
margin-left: auto;
margin-right: auto;
text-align: left; /* IE */
width: XXX; /* Fill this out. */
}
让HTML代码如下:
<body>
<div class="box">.....</div>
</body>
.box{
width: 200px;
margin: 10px auto;
}
auto
,这意味着它将自动调整其位置以居中。 auto
保证左右边距将设置为相同的大小。<div style="text-align: center; display: inline-block; width: specifyYourWidthHere">
<div>
//Your TextBox here
</div>
</div>
<div style="width: 800px; margin: 0px auto;">
your content here
</div>
如果您想要实现水平和垂直对齐,请查看此页面:http://www.wpdfd.com/editorial/thebox/deadcentre2.html
你可以尝试:
<center>
这是更多的文本,以满足30个字符的最小要求。