简体   繁体   English

在标签div中垂直对齐文本

[英]Aligning text vertically inside a label div

Example of problem http://img638.imageshack.us/img638/3733/97914817.jpg 问题示例http://img638.imageshack.us/img638/3733/97914817.jpg

I'm trying to recode one of my older forms. 我正在尝试重新编码我的一种旧表格。 It was filled with tables that I want to replace with CSS. 它充满了我要替换为CSS的表。 However I'm having trouble having text and a form element aligned vertically together. 但是,我无法使文本和表单元素垂直对齐。 As the picture shows the text defaults to starting at the top instead of in the middle. 如图所示,文本默认为从顶部开始,而不是从中间开始。 The blue highlights around the row is dreamweavers interpretation / selection of what is going on. 行周围的蓝色亮点是Dreamweavers解释/对进行中的选择。

I have label and input divs, both floated left, inside a div called #light, which is inside a general container. 我有一个标签和输入div,它们都向左浮动,在一个称为#light的div内,该div在常规容器内。 This is what my css code looks like: 这是我的CSS代码的样子:

#contentBox{
 width: 600px;
 float: left;
 background-color: #e2e2e2;
 overflow: auto;
 border-color: #c5c5c5;
 border-width: 1px;
 border-style: solid;
 font-size: 12px;
}
#light {
 float: left;
 width: 500px;
 padding: 15px;
 background-color: #e2e2e2;
 margin: 7px;
 border-color: #c5c5c5;
 border-width: 1px;
 border-style: solid;
 vertical-align: middle;
}
input {
 float: right;
 width: 20em;

}
label {
 float: left;
 text-align: right;
 vertical-align: baseline;
}

Any idea what the problem is? 知道是什么问题吗? I've tried swapping around the vertical-align in different divs, floating in different directions, getting rid of the label but I just end up with more problems rather than less. 我尝试在不同的div中围绕垂直对齐交换,以不同的方向浮动,摆脱了标签,但最终我遇到的问题更多而不是更少。

You cannot use vertical-align on elements unless they are table cells (or displayed as such) as this article explains. 您不能使用vertical-align的元素,除非它们是表格单元格(或显示等)的这篇文章解释说。 Set line-height to the element height if you've only got one row of text. 如果只有一行文本,请将line-height设置为元素高度。

Usually, to solve that problem, I use the line-height property: 通常,为了解决该问题,我使用line-height属性:

Ex: 例如:

div{width:600px;font:normal normal 12px/30px Arial, Helvetica, sans-serif;}

This will set the font to 12px, and the line-height to 30px, keeping the font vertically align within the 30px of its line. 这会将字体设置为12px,将行高设置为30px,使字体在其行的30px之内垂直对齐。

Vertical alignment of text can be incredibly annoying or incredibly easy. 文本的垂直对齐可能会令人讨厌或难以置信。

If the size of all the involved elements are known, your best bet is to set manual padding/margins on the text itself to make sure it's aligned. 如果知道所有涉及元素的大小,最好的办法是在文本本身上设置手动填充/边距,以确保其对齐。

If the content you want to center vertically is dynamic, this is your best bet . 如果您想垂直居中的内容是动态的, 这是您最好的选择

Not sure, but your input tag is set to "float:right", so its height won't be taken into account by the parent. 不确定,但是您的输入标签设置为“ float:right”,因此父级不会考虑其高度。 Hence, the height of the parent is actually probably the height of the label (I suspect dreamweaver is not interpreting correctly what browsers do.) Try to remove the float on the input tag and see if it makes a difference. 因此,父级的高度实际上可能是标签的高度(我怀疑Dreamweaver无法正确解释浏览器的功能。)尝试删除输入标签上的浮点数,看看是否有所不同。

Vertical alignment can be applied only to inline elements. 垂直对齐只能应用于内联元素。 The best solution is to modify your HTML and make it like in this examples 最好的解决方案是修改HTML并使其像本示例中所示

您可以寻求“便宜”的解决方案,并将padding-top应用于标签div。

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

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