<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