使用CSS在同一行上放置标签和文本框

20
创建一个新的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;
}
4个回答

18

我通常将标签向左浮动,使其与输入框对齐。这里是一个例子: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
}

刚刚尝试了float left,但没有成功。你能展示一下如何修改问题中提到的CSS吗? - user9969
展示你的标记,如果需要我可以创建另一个JSFiddle。 - kinakuta
我会做的第一件事是将您的标签移动到与文本输入相同的父元素中。当您浮动标签时,它会在块元素内浮动,这意味着任何跟随的内容都会在其下方流动。请参考我的答案中的jsfiddle以了解我的意思。 - kinakuta
通过将标签移动到同一父级中,可以实现对齐。如何对齐文本框,使表单看起来不那么凌乱。 - user9969
给标签设置宽度 - 这将使输入框与最左边的距离相同,对齐它们。然后,根据您喜欢的标签对齐方式,您可以将它们保持原样,或者将其右对齐。 - kinakuta

15

我正在使用这个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;
}

类似的,我在Site.css中没有看到任何display-field/display-label元素。我已经使用VS2010创建了一个新项目。 - Rohit

1
一些回答并不完全符合我的要求。这段CSS代码似乎满足我的需求。
input,
select,
textarea{
    display:inline-block !important;
}

1
尽管我没有尝试过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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接