2012-04-11 188 views
-1

我有一个按钮,两个文本框和一些文本。如何将所有这些元素放入页面的中心?将html页面的所有元素放入页面的中心

<body> 
     Hello world! 
     <p><input TYPE="text" name="textbox1"><p> 
     <p><input TYPE="text" name="textbox2"></p> 
     <p><input TYPE="button" VALUE="Enter" ONCLICK="HomeButton()"></p> 
      <script> 
       function HomeButton() 
       { 
        location.href="registration.html"; 
       } 
      </script> 
</body> 
+2

垂直,水平还是两者? – PeeHaa 2012-04-11 20:50:30

回答

4

我会做这样的事情,也可以使用外部CSS表:)

<!--In your <head> add this--> 

<style type="text/css"> 
body{margin:0;padding:0} 
#body{width:800px; margin:auto} 
</style> 

<!--And here is your body--> 

<body> 
<div id="body"> 
CONTENT HERE! 
</div> 
</body> 

这将使800像素设定宽度,然后它会对齐在页面中心

+0

它不起作用 – 2012-04-11 21:01:08

+0

你使用什么浏览器? – 2012-04-11 21:02:12

+0

comodo龙。而在IE8中它也不起作用 – 2012-04-11 21:02:50

1

试试这个:

<body> 
<div style="margin:0 auto;text-align:center;"> 
     Hello world! 
     <p><input TYPE="text" name="textbox1"><p> 
     <p><input TYPE="text" name="textbox2"></p> 
     <p><input TYPE="button" VALUE="Enter" ONCLICK="HomeButton()"></p> 
      <script> 
       function HomeButton() 
       { 
        location.href="registration.html"; 
       } 
      </script> 
</div> 
</body> 
+1

如果不添加宽度,做得不多。也请不要使用内联的CSS。 – PeeHaa 2012-04-11 20:52:16

-1

这个简单的使用<body align="center"> 这里怎么看Fiddle

虽然它不是一个好的做法,但是这会工作,

1

你可以把这些控件到div或使用<center>标签或<body align="center">

1

使用表或div的

尝试下面的代码

<body > 
    <table width="100%" border="0"> 
     <tr> 
     <td height="200">&nbsp;</td> 
     <td>&nbsp;</td> 
     </tr> 
     <tr> 
     <td width="400px">&nbsp;</td> 
     <td> Hello world! 
      <p><input TYPE="text" name="textbox1"><p> 
      <p><input TYPE="text" name="textbox2"></p> 
      <p><input TYPE="button" VALUE="Enter" ONCLICK="HomeButton()"></p> </td> 

     </tr> 
    </table> 

    </body>