![](/img/trans.png)
[英]How can I align form-controls vertically when some of the labels span multiple lines?
[英]How do I align a <label> with a <span> when the <span> sometimes goes onto multiple lines?
我的代碼是:
<label>Something:</label> <span>Some Value</span>
<br/>
<label>Something:</label> <span>Some Value</span>
<br/>
<label>
具有固定寬度。
輸出:
Something: Some Value
Something: Some Value
但是,如果某些值很長,則包裝不正確
Something: Some Value
Something: Some Value Some
Value Some
有沒有一個很好的解決方案,所以它按照你的預期排隊? 我正在制作一個相當大的數據表,並且無法預測哪些值會提前跨越多行。
如果你可以修復兩個元素的寬度,則應用display:block
和float:left
。 這將創建一個類似於外觀的列,並使跨度包含在列中。
CSS :
label, span{
float: left;
width: 150px; /* Adjust widths to fit your needs */
}
br{ clear: both;}
HTML
<label>Something:</label> <span>Some Value</span>
<br/>
<label>Something:</label> <span>Some Value that is alot longer and wraps</span>
JS小提琴: http : //jsfiddle.net/gvPdQ/
如果您被允許調整標記我有以下解決方案。 要求標簽是固定寬度; 跨度填補剩余空間。
<div class="row">
<label>Something:</label> <span>Some Value</span>
</div>
<div class="row">
<label>Something:</label> <span>Some Value Some Value Some Value Some Value Some Value Some Value Some Value Some Value Some Value Some Value</span>
</div>
.row {
overflow: hidden;
}
.row label {
display: block;
float: left;
width: 200px;
}
.row span {
display: block;
margin-left: 210px; /* equal to label width plus some gap */
}
如果要制作“大型數據表”,請使用table
元素,例如
<table>
<tr><th>Something <td>Some value
<tr><th>Something <td>Some value
...
</table>
您仍然可以在Something上設置固定寬度,但您不需要。
將label
元素用於標記可標記元素(通常是控件,即表單字段)以外的任何其他內容是不正確的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.