我在内联显示label
,input
和div
元素,期望它们垂直对齐,最初有效。
但是,当我尝试允许div
的内容溢出省略号时,它们不再垂直对齐
注意:这是在Chrome 46.02490.86
中观察到的
p {color:red;}
input,
label,
div {
width: 50px;
display: inline-block;
margin-left: 5px;
}
label {
text-align: right;
}
.longdesc {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
<label>Name:</label>
<input type='text' value='James'>
<label>Desc:</label>
<div>Short</div>
<hr />
<label>Name:</label>
<input type='text' value='James'>
<label>Desc:</label>
<div class='longdesc'>Longer Description</div>
<p>
In the second example "Long" is higher up
</p>
如何在不弄乱垂直对齐的情况下实现溢出效果?
答案 0 :(得分:9)
将vertical-align: top
添加到您的inline-block
元素:
p {color:red;}
input,
label,
div {
width: 50px;
display: inline-block;
margin-left: 5px;
vertical-align: top;
}
label {
text-align: right;
}
.longdesc {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
&#13;
<label>Name:</label>
<input type='text' value='James'>
<label>Desc:</label>
<div>Short</div>
<hr />
<label>Name:</label>
<input type='text' value='James'>
<label>Desc:</label>
<div class='longdesc'>Longer Description</div>
<p>
In the second example "Long" is higher up
</p>
&#13;
答案 1 :(得分:2)
我不知道你在css中的垂直对齐位置是什么?
默认情况下,vertical-align
具有属性baseline
,如果您想集中使用vertical-align: middle
p {color:red;}
input,
label,
div {
width: 50px;
display: inline-block;
margin-left: 5px;
vertical-align: middle;
}
label {
text-align: right;
}
.longdesc {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
&#13;
<label>Name:</label>
<input type='text' value='James'>
<label>Desc:</label>
<div>Short</div>
<hr />
<label>Name:</label>
<input type='text' value='James'>
<label>Desc:</label>
<div class='longdesc'>Longer Description</div>
<p>
In the second example "Long" is not higher up :)
</p>
&#13;