Ярлык и текстовое поле в одной строке с помощью css
При создании нового приложения asp.net mvc3 вы получите регистрацию и регистрационную форму с надписью над текстовым полем.
Я хочу изменить его так, чтобы как метка, так и поле находились в одной строке и выровнены
Не работает
@using (Html.BeginForm()) {
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
CSS
.display-label,
.editor-label
{
display:inline-block;
width: 200px;
text-align: right;
margin-right: 10px;
}
.display-field,
.editor-field
{
display:inline-block;
margin: 0.5em 0 0 0;
}
Ответы
Ответ 1
Я обычно плаваю на моей этикетке слева, а входные строки - рядом с ней. Вот пример: http://jsfiddle.net/qXFLa/
Вот пример того, как я изменил бы код:
<div class="editor">
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
Затем для вашего css:
.editor label {
float: left;
width: 30px; // just putting an example here - but give them a width to align better
text-align: right; // this right-aligns them with the input
}
.editor input[type=text] {
width: 80px; // just putting an example value in here to make your inputs uniform in length
margin: 0 0 0 10px; // just some breathing room from the labels
}
Ответ 2
Я использую этот css
.editor-label
{ display: block;
float: left;
padding: 0;
width: 150px;
margin: 1em 1em 0 0;
}
.editor-field
{
width:auto;
margin: 0.5em 0 0 0;
overflow: auto;
min-height: 2em;
}
Ответ 3
Хотя я не пробовал принятый ответ kinakuta, вам требуется переустановить сгенерированное представление Visual Studio; Я бы подошел следующим образом: , если вы не хотите перенастраивать автогенерированный макет., т.е. Сохранить формат
<div class="editor-label" />
<div class="editor-field" />
<div class="editor-label" />
<div class="editor-field" />
etc...
Следующий CSS работает для меня. Не стесняйтесь предлагать улучшения. (он очень похож на ответ Pa0l0)
<style type="text/css">
.editor-label, .display-label
{
clear:both;
float:left;
width:150px;
margin:1em 0 0 0;
font-weight:bold;
}
.editor-field, .display-field
{
margin:1em 0 0 0;
min-height:1.5em;
}
</style>
Ответ 4
Несколько из этих ответов были не совсем тем, что я искал. Этот бит в моем CSS, кажется, работает для моих нужд.
input,
select,
textarea{
display:inline-block !important;
}