[英]How to make text boxes aligned vertically?
一個簡單的解決方案是CSS-Grid
,它將創建一個類似表格的布局:
body { display: grid; grid-template-columns: min-content auto; grid-gap: 10px; } /* counters the linebreak at white-space caused by min-content */ label { white-space: nowrap; }
<label>Sceenshot 1.png</label> <input type="text"> <label>Sceen Shot 2022-06-21 at 5.58.05 PM.png</label> <input type="text">
grid-template-columns: auto 1fr;
對於另一個附近的元素。 <div id="container" style=" display: grid; grid-template-columns: auto 1fr; gap: 0.5rem;"> <!-- 1 --> <span>small text</span> <input type="text"> <!-- 2 --> <span>longer text, longer text...</span> <input type="text"> </div>
display: grid
添加到父元素,如果您希望每個元素都在另一個元素之下 <div id="container" style="display: grid;"> <!-- 1 --> <span>small text</span> <input type="text"> <!-- 2 --> <span>longer text, longer text...</span> <input type="text"> </div>
我建議使用
<label>
而不是普通的<span>
! 以幫助可訪問性。 事實上,如果用戶點擊<span>
它將自動關注<input>
<div id="container" style="display: grid; grid-template-columns: auto 1fr; gap: 0.5rem;"> <!-- 1 --> <label for="first-input"> <span>small text</span> </label> <input type="text" id="first-input"> <!-- 2 --> <label for="second-input"> <span>longer text, longer text...</span> </label> <input type="text" id="second-input"> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.