简体   繁体   English

将标签与输入垂直对齐

[英]Vertically align label with input

I am currently converting an old HTML form to not use tables. 我目前正在将旧的HTML表单转换为不使用表格。 The label requires a fixed width, which I've achieved using an answer from this site. 标签需要固定的宽度,我已经使用本网站的答案实现了这一点。 The current code is as follows: 目前的代码如下:

HTML HTML

<div id="form">
    <label for="field1">Name&nbsp;</label>
    <input type="text" id="field1" value="default name" />
</div>

CSS CSS

label {
    float: left;
    width: 50px;
    white-space: nowrap;
}
input {
    display: block;
    overflow: hidden;
    width: *;
}
#field1 {
    width: 150px;
}

Currently, the label is vertically aligned at the top. 目前,标签在顶部垂直对齐。 How can I vertically align the label and field to the centre? 如何将标签和字段垂直对齐到中心?

Edit : I'm getting a lot of answers to basically pad my label into the correct position via a specified number of pixels. 编辑 :我得到了很多答案,基本上通过指定的像素数将标签填充到正确的位置。 I don't want to do that as it's basically hard-coding and not true vertical alignment to the middle. 我不想这样做,因为它基本上是硬编码而不是真正的垂直对齐到中间。

Following some advice I've received here, I've cleaned up my code some. 根据我在这里收到的一些建议,我已经清理了一些代码。 See the above edited code. 请参阅以上编辑的代码。

I've tried vertical-align: middle; 我试过vertical-align: middle; in label but it doesn't work. label但它不起作用。 Please note that the user's platform will be IE. 请注意,用户的平台将是IE。

If I get this right, You want the label and the input to vertically center align WRT each other and not the page. 如果我做对了,你希望labelinput垂直居中对齐WRT而不是页面。 For that, there are couple of ways. 为此,有几种方法。

The Flexbox Way Flexbox方式

If you want to use something new from the CSS world and be future ready, use flexbox. 如果您想使用CSS世界中的新东西并准备好未来,请使用flexbox。 Example - 示例 -

 .fieldHeading { width: 50px; } .fieldSpan { overflow: hidden; } #field1 { width: 150px; } /*flexbox approach. * height and background added for clarity. */ #form { height: 100px; background: #bada55; display: flex; align-items: center; } 
 <div id="form"> <label for="field1" class="fieldHeading">Name&nbsp;</label> <span class="fieldSpan"><input type="text" id="field1" value="default name" /></span> </div> 

The vertical-align Way vertical-align方式

This works well when you have both the label and the input on one line as inline-block elements. 当一行上的labelinput作为inline-block元素时,这很有效。 Example - 示例 -

 .fieldHeading { width: 50px; vertical-align:middle; } .fieldSpan { overflow: hidden; vertical-align:middle; } #field1 { width: 150px; } 
 <div id="form"> <label for="field1" class="fieldHeading">Name&nbsp;</label> <span class="fieldSpan"><input type="text" id="field1" value="default name" /></span> </div> 

The height & line-height Duo heightline-height Duo

This works well too but if your label is big and has the possibility of wrapping into multiple lines, it's gonna look terrible. 这也很有效,但如果你的标签很大并且有可能包裹成多行,那就太可怕了。 Example - 示例 -

 .fieldHeading { width: 50px; } .fieldSpan { overflow: hidden; } #field1 { width: 150px; } /*line-height and height be same for parent * background added for clarity. */ #form { line-height: 40px; height: 40px; background: #bada55; } 
 <div id="form"> <label for="field1" class="fieldHeading">Name&nbsp;</label> <span class="fieldSpan"><input type="text" id="field1" value="default name" /></span> </div> 

I hope these help you not only in this problem but for all other vertical alignment problems. 我希望这些不仅可以解决这个问题,还可以解决所有其他垂直对齐问题。

You can use line-height or margin-bottom until you get satisfactory results. 您可以使用line-heightmargin-bottom直到获得满意的结果。

Example

#form label {
    line-height:1.4;
    margin-bottom:2px;
}

This will apply the selector to all labels under the form. 这会将选择器应用于表单下的所有标签。 If you want to be specific, you can use the specific css class like .fieldHeading instead of #form label . 如果您想要具体,可以使用特定的css类,如.fieldHeading而不是#form label

Do you need like this? 你需要这样的吗?

<div id="form">
    <div class="valign">
        <label for="field1" class="fieldHeading">Name&nbsp;</label>
        <span class="fieldSpan"><input type="text" id="field1" value="default name" /></span>
    </div>
</div>

<style>
.fieldHeading {
    float: left;
    width: 50px;
}
.fieldSpan {
    display: block;
    overflow: hidden;
    width: *;
}
#field1 {
    width: 150px;   
}

#form {display: table;height:100%;}

.valign {
    display: table-cell;
    vertical-align: middle; 
}

</style>

I think, solution margin-bottom, use whatever px you want 我认为,解决方案边缘底部,使用你想要的任何px

.fieldHeading {
    float: left;
    width: 50px;
    margin-bottom: 3px;
}

Use: 采用:

.fieldHeading {
  display: inline-block;
  width: 50px;
  vertical-align: middle;
}
#field1 { width: 150px; }

Let span be an inline type. 设span为内联类型。

You don't really need the span for enclosing your input element. 你真的不需要用于封闭输入元素的span。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM