如何水平对齐表单?

发布于 2024-12-02 02:09:39 字数 450 浏览 0 评论 0原文

如何水平对齐表单?例如:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

暖阳 2024-12-09 02:09:39

删除 div 标签,如下所示:

<form>
<input type="email" placeholder="[email protected]">
<input class="enter" type="submit" value="Send"/> 
</form>

remove div tag like this :

<form>
<input type="email" placeholder="[email protected]">
<input class="enter" type="submit" value="Send"/> 
</form>
南城旧梦 2024-12-09 02:09:39

简单的方法:

<form> 
<input type="email" placeholder="[email protected]"> 
<input class="enter" type="submit" value="Send"/>
</form>

更有风格的方法:

<form> 
<div style="float:left"><input type="email" placeholder="[email protected]"></div> 
<div style="float:left"><input class="enter" type="submit" value="Send"/></div> 
</form>

Simple way:

<form> 
<input type="email" placeholder="[email protected]"> 
<input class="enter" type="submit" value="Send"/>
</form>

More style-ish way:

<form> 
<div style="float:left"><input type="email" placeholder="[email protected]"></div> 
<div style="float:left"><input class="enter" type="submit" value="Send"/></div> 
</form>
葬﹪忆之殇 2024-12-09 02:09:39

摆脱你的 DIV。你不需要它。

<form>
<input type="email" placeholder="[email protected]">
<input class="enter" type="submit" value="Send"/> 
</form>

Get rid of your DIV. You don't need it.

<form>
<input type="email" placeholder="[email protected]">
<input class="enter" type="submit" value="Send"/> 
</form>
紫竹語嫣☆ 2024-12-09 02:09:39

您可以向包装表单元素的每个 div 添加 float: left 样式。

You can add a float: left style to each div that wraps the form elements.

相思碎 2024-12-09 02:09:39

将值为 inline 的 CSS 属性 display 添加到 DIV:

#yourdiv
{
  display: inline;
}

Add the CSS property display with the value inline to the DIV:

#yourdiv
{
  display: inline;
}
So要识趣 2024-12-09 02:09:39

好吧,简单地说 - 如果您在 div 元素上使用 float:left ,它应该水平对齐它们并让您继续前进。

<form>
    <input type="email" placeholder="[email protected]" style="float:left;">
    <div> 
        <input class="enter" type="submit" value="Send" style="float:left;"/> 
    </div>
</form>

Well, simply put - if you use float:left on your div elements, it should align them horizontally and get you on your way.

<form>
    <input type="email" placeholder="[email protected]" style="float:left;">
    <div> 
        <input class="enter" type="submit" value="Send" style="float:left;"/> 
    </div>
</form>
终止放荡 2024-12-09 02:09:39
<input type="text"/>
<input type="submit"/>

输入元素是内联块,这意味着它们总是在同一行,之所以有两行,是因为 div 元素是块元素,以便它能够与中的其他元素对齐同一条“线”,它必须是浮动的,或者不是相对定位的。

示例

<input type="text"/>
<input type="submit"/>

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

残龙傲雪 2024-12-09 02:09:39

如果您希望表单内的所有字段对齐,您可以将 display:inline 作为 CSS 规则添加到包含的元素。我通常喜欢使用段落标签来分隔每个标签+输入标签,或无序列表。更新您的示例:

<form>
  <ul>
    <li><input type="text" type="email" placeholder="[email protected]" /></li>
    <li><input type="text" type="submit" value="send" /></li>
  </ul>
</form>

<style type="text/css" media="screen">
  form ul {
    list-style:none;
  }
  form li {
    display:inline;
  }
</style>

这适用于您添加为新列表项的每个字段。

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:

<form>
  <ul>
    <li><input type="text" type="email" placeholder="[email protected]" /></li>
    <li><input type="text" type="submit" value="send" /></li>
  </ul>
</form>

<style type="text/css" media="screen">
  form ul {
    list-style:none;
  }
  form li {
    display:inline;
  }
</style>

This will work for each field you add as a new list item.

春夜浅 2024-12-09 02:09:39

奇怪的是,就我而言,简单地删除“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文