【问题标题】:HTML, CSS Center a Box [duplicate]HTML,CSS居中一个框[重复]
【发布时间】:2013-02-20 01:58:24
【问题描述】:

这里是注册框:

http://technicaldebt.co.uk/fyp/register.php

我试图让框居中在网页的中间。 CSS附在下面。任何帮助将不胜感激。

#fg_membersite fieldset {
left: 50%;
top: 50%;
width: 230px;
margin-top: -125px: /* height/2 */
margin-left: -115px; /* width/2 */
position: absolute;
}

#fg_membersite legend, #fg_membersite h2
{
font-family : Arial, sans-serif;
font-size: 1.3em;
font-weight:bold;
color:#333;
}

#fg_membersite label
{
font-family : Arial, sans-serif;
font-size:0.8em;
font-weight: bold;
}

#fg_membersite input[type="text"],#fg_membersite textarea,
#fg_membersite input[type="password"]
{
  font-family : Arial, Verdana, sans-serif;
  font-size: 0.8em;
line-height:140%;
color : #000; 
padding : 3px; 
border : 1px solid #999;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
border-radius: 5px;

}

#fg_membersite input[type="text"],
#fg_membersite input[type="password"]
{
height:18px;
width:220px;
}

#fg_membersite #scaptcha
{
width:60px;
height:18px;
}

#fg_membersite input[type="submit"]
{
width:100px;
height:30px;
padding-left:0px;
}

#fg_membersite textarea
{
height:120px;
width:310px;
}

#fg_membersite input[type="text"]:focus,
#fg_membersite textarea:focus
{
color : #009;
border : 1px solid #990000;
background-color : #ffff99;
font-weight:bold;
}

#fg_membersite .container
{
margin-top:8px;
margin-bottom: 10px;
}

#fg_membersite .error
{
 font-family: Verdana, Arial, sans-serif; 
 font-size: 0.7em;
 color: #900;
 background-color : #ffff00;
}

#fg_membersite #register_password_errorloc
{
  clear:both;
}

#fg_membersite  fieldset#antispam
{
 padding:2px;
 border-top:1px solid #EEE;
 border-left:0;
 border-right:0;
 border-bottom:0;
 width:350px;
}

#fg_membersite fieldset#antispam legend
{
    font-family : Arial, sans-serif;
   font-size: 0.8em;
   font-weight:bold;
 color:#333;    
}

#fg_membersite .short_explanation
{
   font-family : Arial, sans-serif;
   font-size: 0.6em;
 color:#333;   
}

/* spam_trap: This input is hidden. This is here to trick the spam bots*/
#fg_membersite .spmhidip
{
 display:none;
 width:10px;
 height:3px;
}
#fg_membersite #fg_crdiv
{
   font-family : Arial, sans-serif;
   font-size: 0.3em;
 opacity: .2;
 -moz-opacity: .2;
 filter: alpha(opacity=20);   
}
#fg_membersite  #fg_crdiv p
{
  display:none;
}

    #fg_membersite_content li
  {
   font-family : Arial, sans-serif;
  padding-top:10px;
  padding-bottom:10px;
}
    #fg_membersite_content 
{
         font-family : Arial, sans-serif;
         font-size: 0.9em;
  line-height: 150%
 }

#fg_membersite_content h2
  {
   font-family : Arial, sans-serif;
   font-size: 1.5em;
   font-weight:bold;
 color:#333;
}

下面的 HTML:

<!-- Form Code Start -->
<div id='fg_membersite'>
<form id='register' action='/fyp/register.php' method='post' accept-charset='UTF-8'>
<fieldset >
<legend>Register</legend>



<input type='hidden' name='submitted' id='submitted' value='1'/>

<div class='short_explanation'>* required fields</div>

<input type='text'  class='spmhidip' name='spe8306af0d23b01a2bf7c1734aacf25fe' />

<div><span class='error'></span></div>
<div class='container'>
<label for='name' >Your Full Name*: </label><br/>
<input type='text' name='name' id='name' value='' maxlength="50" /><br/>
<span id='register_name_errorloc' class='error'></span>
</div>

<div class='container'>
<label for='email' >Email Address*:</label><br/>
<input type='text' name='email' id='email' value='' maxlength="50" /><br/>
<span id='register_email_errorloc' class='error'></span>
</div>

<div class='container'>
<label for='username' >UserName*:</label><br/>
<input type='text' name='username' id='username' value='' maxlength="50" /><br/>
<span id='register_username_errorloc' class='error'></span>
</div>

<div class='container' style='height:80px;'>
<label for='password' >Password*:</label><br/>
<div class='pwdwidgetdiv' id='thepwddiv' ></div>
<noscript>
<input type='password' name='password' id='password' maxlength="50" />
</noscript>    
<div id='register_password_errorloc' class='error' style='clear:both'></div>
</div>

<div class='container'>
<input type='submit' name='Submit' value='Submit' />
</div>

</fieldset>
</form>

【问题讨论】:

  • 请也发布相关的HTML
  • margin-top: -125px: => margin-top: -125px; 带分号。

标签: html css


【解决方案1】:

如果不使用某种display: table 设置,在没有 JavaScript 或不知道元素本身高度的情况下,您无法将其垂直居中:

#fg_membersite fieldset {
    display: inline-block;
    margin: 0 auto;
    left: 50%;
    top: 50%;
    width: 230px;
    margin-top: -181px;
    margin-left: -115px;
    position: absolute;
}

“181px”源自字段集解析到的 362px 高度。如果高度发生变化,您必须更新它。

【讨论】:

  • 值得注意的是(在我的回答中),您几乎拥有它,您只是输入了错误的 CSS 定义(在您的 margin-top 定义之后,请参阅 : 而不是 ;)。
  • @BenM -125px 还不够
  • 是的,因此“几乎拥有它”... ;)
【解决方案2】:

您的边距定义被忽略。您的 CSS 定义中有一个冒号 : 而不是分号 (;):

#fg_membersite fieldset {
    left: 50%;
    top: 50%;
    width: 230px;
    margin-top: -125px; /* height/2 */
    margin-left: -115px; /* width/2 */
    position: absolute;
}

【讨论】:

    【解决方案3】:

    #fg_membersite fieldset 中删除定位,这会使您的框水平居中对齐,然后使用javascript 将其垂直对齐。像这样的:

    使用javascript:(body标签的末尾加载

    <script>
      var body = document.body;
      var bodyHeight = body.offsetHeight;
      var box = document.getElementById('main'); //replace 'main' with Id of the fieldset  
      var height = box.offsetHeight;
      box.style.marginTop = ((bodyHeight/2) - (height/2));
    </script>
    

    Working Fiddle using simple javascript

    Working Fiddle using jQuery

    【讨论】:

    • 这里真的不需要jQuery。
    • @BenM 我的解决方案提供了精确的垂直对齐,这也是另一种方式。
    • 是的,我知道。我的意思是,通过 CSS 可以轻松实现相同的结果。我的观点是 OP 没有在页面上使用 jQuery(据我所知),并且包含一个用于这种微不足道的东西的库是严重的矫枉过正。
    • @BenM 是的,你是对的。但我很高兴我的实验成功了,并想在这里分享:)。
    • @BenM 我使用 javascript 更新了我的帖子。
    【解决方案4】:

    删除

    “左:50%”

    右:50%

    css:

    fg_membersite 字段集

    并使用

    位置:相对;左边距:自动;边距右:自动

    【讨论】:

    • 我已经做到了,它仍然没有居中:(
    • 啊,我看到为包装元素添加了一个宽度
    • 在你的情况下“ html {width:100%}
    • 没有。这只会水平对齐,而不是垂直对齐。
    猜你喜欢
    • 2016-09-04
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2020-06-17
    • 2016-01-07
    相关资源
    最近更新 更多