[英]How to center an element vertically in a div?
I'm practising making a form and I would like to position the form elements on the left in the centre of their row sort of how it would automatically be done with a table. 我正在练习制作表单,我想将表单元素放在其行中心的左侧,以了解如何自动使用表格。 Here is some jsFiddle for what i've been attempting so far.
这是到目前为止我一直在尝试的一些jsFiddle 。
How do I vertically centre the label element for a form? 如何垂直对齐表单的标签元素?
Give label
same height as input filed
have & give line-height
to your label
. 给
label
相同的高度, input filed
必须与给line-height
到你的label
。 Like this: 像这样:
.element label {
float: left;
font-weight: 700;
height: 30px;
line-height: 30px;
width: 156px;
}
You could use jQuery and setup the position after the elements are loaded. 您可以使用jQuery并在元素加载后设置位置。 Minor adjustments to HTML (assign IDs):
对HTML的细微调整(分配ID):
<div class="element">
<label id="feedback">Feedback: </label>
<select multiple="multiple" name="feedback" size="4" id="selectbox">
<option value="">Option1</option>
<option value="">Option1</option>
<option value="">Option2</option>
<option value="">Option3</option>
<option value="">Option4</option>
<option value="">Option5</option>
</select>
</div>
Javascript: Javascript:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function(){
var ht = $("#selectbox").height();
var ht2 = $("#feedback").height();
ht = $("#selectbox").offset().top + (ht-ht2) / 2;
$("#feedback").offset({ top: ht });
});
</script>
Strictly CSS (ID assignment on the label) 严格的CSS(标签上的ID分配)
<div class="element">
<label id="feedback">Feedback: </label>
<select multiple="multiple" name="feedback" size="4">
<option value="">Option1</option>
<option value="">Option1</option>
<option value="">Option2</option>
<option value="">Option3</option>
<option value="">Option4</option>
<option value="">Option5</option>
</select>
</div>
And the CSS: 和CSS:
.element select {
height: 200px;
}
.element label#feedback {
margin-top: 85px;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.