2013-06-27 40 views
0

对不起,我在这方面是全新的。我试图让一个div,所以我可以做一个盒子在我的页面的中间,一切都会进入,就像在这个网站http://www.jellyneo.net/index.phpHTML5 div,我做错了什么?

我的HTML看起来像这样:

<!DOCTYPE HTML PUBLIC> 
<html> 
    <head> 
     <link rel="stylesheet" type="text/css" href="CSSdoc.css" /> 
    </head> 
    <body> 
     <center> 
      <div id="mainbox"> 
       <p>text</p> 
      </div> 
     </center> 
     <h1> text </h1> 
     <p>text</p> 
     <h2> text </h2> 
     <p>text</p> 
    </body> 
</html> 

和CSS我看起来像这样

#mainbox { 
    width: 900px 
    text-align: left 
} 

我在做什么错了?

回答

0
margin:auto; 

一旦它是一个固定的宽度,这将在中心对齐它。

<body style="margin:0;"> 
     <div style="width:900px; margin:auto; border:1px solid #000;" id="mainbox"> 
      <p>text</p> 
    <h1> text </h1> 

    <p>text</p> 
    <h2> text </h2> 

    <p>text</p> 
     </div> 

</body> 

为了清楚起见,我添加了一个边框。

使你的身体应该有

body{ 
margin:0; 
} 

而且你的框区域应该有

#mainbox{ 
width:900px; 
margin:auto; 
} 
0

你并不需要在你的HTML代码中的<center>标签; CSS将帮助你为中心事物。在您的CSSdoc.css文件中,只需调整以下内容:

#mainbox { 
    padding: 0; 
    margin: 0 auto; /*the 0 here represents the top and bottom margin spacing. it's good to have and control in many cases.*/ 
    width: 900px; 
    border: solid 2px #000; /*this is optional, more for debugging purposes to see your 900px scaled box*/ 
}