【问题标题】:Form input elements not centering表单输入元素不居中
【发布时间】:2011-09-30 15:23:56
【问题描述】:

我在这里查看了许多不同的答案,它们似乎都归结为 text-align: center 在父 div 的样式中。我已经尝试过了,它适用于标签,但不适用于实际的输入元素。

JSFiddle

这是基本代码:

html

<div id="content"> 
  <div id="login_form"> 
    <h2>Log in to DOT Voting</h2> 
    <form>
        <label for="username">Username</label>
        <input type="text" name="username" value=""  />
        <label for="password">Password</label>
        <input type="password" name="password" value=""  />
        <input type="submit" name="submit" value="Log In"  />
  </div>     
</div> 

css

#login_form {
    width:90%;
    background-color: #bdd2ff;
    border: 1px solid white;
    margin: 50px auto 0;
    padding: 1em;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    text-align: center;
}

input[type=text], input[type=password] {
    text-align:center;
    display: block;
    margin: 0 0 1em 0;
    width: 90%; 
    border: 1px solid #818181;
    padding: 5px;
}

input[type=submit] , form a {
    border: none;
    margin-right: 1em;
    padding: 6px;
    text-decoration: none;
    font-size: 12px;
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    background: #cfdeff;
    color: black;
    box-shadow: 0 1px 0 white;
    -moz-box-shadow: 0 1px 0 white;
    -webkit-box-shadow: 0 1px 0 white;
}

input[type=submit]:hover, form a:hover {
    background: #007cc2;
    cursor: pointer;
}

login_form div 以页面为中心,表单标签以 div 为中心,提交按钮以 div 为中心。但是文本输入框没有居中。我能做些什么来迫使他们居中? (我不在乎输入框的内容是否居中;我只希望框本身在 div 中居中。)

【问题讨论】:

    标签: css centering


    【解决方案1】:

    添加

    margin: auto;
    

    到您的input[type=text], input[type=password] 班级。

    还要确保删除 text-align: center; 属性,因为这会导致输入中的文本居中。

    JSFiddle

    【讨论】:

    • 谢谢,成功了。所有的答案都差不多。接受这个,因为它是第一个。
    【解决方案2】:

    修改input 元素的margin 声明,以将auto 用于margin-leftmargin-right

    #login_form {
        width:90%;
        background-color: #bdd2ff;
        border: 1px solid white;
        margin: 50px auto 0;
        padding: 1em;
        -moz-border-radius: 10px;
        -webkit-border-radius: 10px;
        text-align: center;
    }
    
    input[type=text], input[type=password] {
        text-align:center;
        display: block;
        margin: 0 auto 1em auto;
        width: 90%;  /*280px;*/
        border: 1px solid #818181;
      /*  -moz-border-radius: 1px;
        -webkit-border-radius: 1px; */
        padding: 5px;
    }
    
    input[type=submit] , form a {
        border: none;
        margin-right: auto;
        margin-left: auto;
        padding: 6px;
        text-decoration: none;
        font-size: 12px;
        -moz-border-radius: 4px;
        -webkit-border-radius: 4px;
        background: #cfdeff;
        color: black;
        box-shadow: 0 1px 0 white;
        -moz-box-shadow: 0 1px 0 white;
        -webkit-box-shadow: 0 1px 0 white;
    }
    
    input[type=submit]:hover, form a:hover {
        background: #007cc2;
        cursor: pointer;
    }
    

    Updated JS Fiddle.

    【讨论】:

      【解决方案3】:

      text-align 不适用于块元素。删除input[type=text], input[type=password] 上的display:block 或将其更改为display:inline-block。请注意,内联块不适用于

      或者由于您在输入中声明了宽度,您可以删除 text-align:center 并使用 margin: 0 auto 1em auto;

      【讨论】:

        【解决方案4】:

        它基于 90% 的 input[type=text], input[type=password] css 值居中。您可以使用固定宽度定义它,或者对于液体布局,将其设置为 100% 宽度并调整其边距。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-05-24
          • 1970-01-01
          • 1970-01-01
          • 2018-03-07
          • 1970-01-01
          • 2017-03-12
          • 1970-01-01
          相关资源
          最近更新 更多