<input type='text', id='name'></input>
,我有
<p>my name is xyz</p>
我想<p>
元素位於20像素的右側<input>
,如何使用CSS實現這一目標?
<input type='text', id='name'></input>
,我有
<p>my name is xyz</p>
我想<p>
元素位於20像素的右側<input>
,如何使用CSS實現這一目標?
<label><input type='text', id='name'><span style="padding-left:20px;">my name is xyz</span></label>
您可以浮動的元素來實現這一點:
input { float: left; }
p {
float: left;
margin-left: 20px;
}
我意識到,這是比簡單地增加一個CSS樣式聲明更多一點的工作,但我可以建議你使用slightly-更多的語義標記來設計你的表單?沿着線的東西:
<form action="path/to/script.php" method="post">
<fieldset> <!-- to associate related inputs -->
<legend>Name</legend> <!-- a broad description of this group of inputs -->
<input name="name" id="name" type="text" /> <!-- input elements are usually self-closing -->
<label for="name">My name is</label> <!-- the for attribute takes the id of the associated input, and allows a click on the label to focus the input -->
</fieldset>
</form>
然後,在CSS,如果你想label
要20像素的input
的權利,所有你需要做的是:
input {
margin-right: 20px;
}
/* or */
label {
margin-left: 20px;
}
JS Fiddle demo of margin-right
。 JS Fiddle demo of margin-left
。
推薦閱讀:
出於某種原因,我想用外部CSS文件接近的結果,因爲內容是動態創建的,而不是內聯樣式。但是謝謝你! – Mellon 2011-04-06 13:14:54
@Mellon:只是一個提示,絕不使用內聯樣式 – Christophe 2011-04-06 13:17:18
內聯樣式僅作爲示例。 – 2011-04-06 13:51:50