-1

我一直在搞乱css,但我无法得到我想要的布局。我想让所有输入字段垂直对齐,但重要的是我仍然保留分隔符(因为我在它们上使用了javascript)我一直在玩不同的位置和浮动设置,我似乎无法让它工作......

如果有人有一个很好的解决方案,可以使其全部垂直对齐(标签直接位于输入字段的顶部),我将不胜感激

这是我凌乱的代码:

http://jsfiddle.net/hYa9a/4/

4

2 回答 2

2

Nightfirecat 建议的替代方案,如果计划是将标签放在文本框上方,那么这可能会奏效:

顺便说一句:我认为 jsfiddle.net/hYa9a/4/ 代码中有一个错字。在密码字段中,将其中一个 ID 输入为 usernameIn。这应该是密码输入吗?

<style>
#optionsBar{ /*same as login*/}
.placeholder {
    background-color: #fff;
    outline: none;
    font-family: "Times New Roman", Times, serif;
    font-size: 12px;
    text-indent: 0;
    /*box-shadow: 0 1px 1px rgba(0, 0, 0, 0.07) inset, 0 0 8px rgba(82, 168, 236, 0.6); */
    -moz-border-radius: 2px;
    border-radius: 2px;
}
/*same as login*/
.selectInput {
    border: 1px solid #8BAAFE;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.07) inset, 0 0 5px rgba(33, 93, 253, 0.6);
}    

#smLogin{
position:relative;
float:right;
width:100%;
height:30px;
}

#usernameIn{
    position: relative;
    float: left;
    width:150px;
    margin-left:-150px;
    margin-top:20px;
}
#passwordIn{
    position: relative;
    float: left;
    width:150px;
    margin-left:-150px;
    margin-top:20px;
}
#username{
    width:150px;
    position: relative;
    float: left;
}
#password{
    position: relative;
    float: left;
    width:150px;
}
#firstInput{
    position: relative;
    float: left;
}
#secondInput{
position: relative;
    float: left;
}
​&lt;/style>

<div id="optionsBar">
<div id="smLogin">
<form name="smLoginForm">

<div id="username" >
<label id="usernameLb"  for="username">Username</label>
</div>
<div id="usernameIn" class="inputs">
 <input id="firstInput" name="username"  class="placeholder controlInput"   type="text" size="20" value="">
</div>

<div id="password" >
 <label id="passwordLb" for="password">Password</label>
</div>
<div id="passwordIn" class="inputs">
 <input id="secondInput" name="password" class="placeholder controlInput" type="password" size="20" value="">
</div>

</form>
</div>
</div>​




注意:如果可以选择修改 HTML 代码而不仅仅是 CSS,我会建议将表单标签和文本框放在包装 div 中。就像是:

<div class="formobject">
<div id="username" >
<label id="usernameLb"  for="username">Username</label>
</div>
<div id="usernameIn" class="inputs">
 <input id="firstInput" name="username"  class="placeholder controlInput"   type="text" size="20" value="">
</div>
</div>

然后,您可以通过删除单个标签和文本框上的所有样式来清理 CSS,并将宽度和浮动设置为仅 formobject div。像:

<style>
.formobject{
    float: left;
    width:150px;
}
</style>
于 2012-05-16T08:22:55.723 回答
1

如果您的意思是将标签直接放在字段之上(如覆盖在字段之上),我建议您position: absolute;也将标签应用于z-index: -1;标签和background-color: transparent;文本字段,如本 JSFiddle 中所使用的那样

于 2012-05-16T08:00:50.367 回答