傳統用 table 排版表單的做法早已過時,用 CSS 才是標準做法。本文示範如何讓標籤與輸入框整齊對齊,做出清爽的表單。
label{
float: left;
width: 80px;
}
form{ margin:0px }
input{
width: 180px;
border:1px solid #808080
}
textarea{
width: 250px;
height: 150px;
}
#sbutton{
margin-left: 80px;
margin-top: 5px;
width:80px;
}
br{ clear: left; }
<form action="" method="post">
<label for="user">姓名:</label>
<input type="text" id="user" name="user" value="" /><br />
<label for="email">郵件:</label>
<input type="text" id="email" name="email" value="" /><br />
<label for="comment">備註:</label>
<textarea id="comment" name="comment"></textarea><br />
<input type="submit" id="sbutton" value="確定" /><br />
</form>
關鍵在於讓 label 向左浮動並固定寬度,使所有標籤的文字對齊成一直行;再用 br{clear:left} 清除浮動,讓每一組「標籤+輸入框」乖乖換行。這樣一來,表單看起來整齊劃一,維護起來也輕鬆。