Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CSS新手:在我的例子中如何设置元素位置?_Css - Fatal编程技术网

CSS新手:在我的例子中如何设置元素位置?

CSS新手:在我的例子中如何设置元素位置?,css,Css,我有 我有 <input type='text', id='name'></input> 我的名字是xyz 我希望元素位于20px的右侧,如何使用CSS实现此目的?我的名字是xyz <p>my name is xyz</p> 您可以浮动元素以实现此目的: <label><input type='text', id='name'><span style="padding-left:20px;">my na

我有


我有

<input type='text', id='name'></input>
我的名字是xyz

我希望
元素位于20px右侧
,如何使用CSS实现此目的?

我的名字是xyz
<p>my name is xyz</p>

您可以浮动元素以实现此目的:

<label><input type='text', id='name'><span style="padding-left:20px;">my name is xyz</span></label>

我意识到这比简单地添加CSS样式声明要多一些工作,但是我可以建议您使用稍微多一些语义标记来设置表单的样式吗?大致如下:

input { float: left; }
p {
    float: left;
    margin-left: 20px;
}

建议阅读:


出于某种原因,我希望使用外部css文件来处理结果,因为元素是动态创建的,而不是内联样式。但是谢谢你@梅隆:只是一个提示,永远不要使用内联样式。内联样式只是一个例子。
<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>
input {
    margin-right: 20px;
}

/* or */

label {
    margin-left: 20px;
}