如何水平对齐表单?
如何水平对齐表单?例如:
<form>
<input type="email" placeholder="[email protected]">
<div>
<input class="enter" type="submit" value="Send"/>
</div>
</form>
将给出这样的框:
email
send
而我希望它们以这种方式出现:
email send
How can I horizontally align a form? For example:
<form>
<input type="email" placeholder="[email protected]">
<div>
<input class="enter" type="submit" value="Send"/>
</div>
</form>
Will give boxes as such:
email
send
Whereas I want them to appear in this fashion:
email send
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
删除 div 标签,如下所示:
remove div tag like this :
简单的方法:
更有风格的方法:
Simple way:
More style-ish way:
摆脱你的 DIV。你不需要它。
Get rid of your DIV. You don't need it.
您可以向包装表单元素的每个
div
添加float: left
样式。You can add a
float: left
style to eachdiv
that wraps the form elements.将值为
inline
的 CSS 属性display
添加到 DIV:Add the CSS property
display
with the valueinline
to the DIV:好吧,简单地说 - 如果您在
div
元素上使用float:left
,它应该水平对齐它们并让您继续前进。Well, simply put - if you use
float:left
on yourdiv
elements, it should align them horizontally and get you on your way.输入元素是内联块,这意味着它们总是在同一行,之所以有两行,是因为 div 元素是块元素,以便它能够与中的其他元素对齐同一条“线”,它必须是浮动的,或者不是相对定位的。
示例
Input elements are inline-block, meaning they're always on the same line, the reason why you got 2 lines, is because the div element is a block element, in order for it to be able to be aligned with other elements in the same "line", it must be floated, or positioned not relatively.
example
如果您希望表单内的所有字段对齐,您可以将 display:inline 作为 CSS 规则添加到包含的元素。我通常喜欢使用段落标签来分隔每个标签+输入标签,或无序列表。更新您的示例:
这适用于您添加为新列表项的每个字段。
If you want all fields inside a form aligned, you can add display:inline as a CSS rule to the containing elements. I generally like to use paragraph tags to separate each label+input tag, or an un ordered list. To update your example:
This will work for each field you add as a new list item.
奇怪的是,就我而言,简单地删除“div”并没有帮助。我必须明确地将“float:left”添加到每个输入中,以便将所有内容都放在一行中。希望它可以帮助遇到同样情况的人。
Strangely, in my case, simply removing 'div' did not help. I have to explicitly put "float:left" to each input in order to get everything in one line. Hopefully it helps someone who falls in the same situation.