【问题标题】:How can I align two input fields of two forms in same line?如何在同一行中对齐两个表单的两个输入字段?
【发布时间】:2014-11-29 14:15:31
【问题描述】:

以下是来自 html 页面的两种形式

<form method="get" action="search/s" id="number">
        <div style="text-align: center;">
                <input  type="text" id="regNo" name="regNo" size="30" maxLength="50" > or     
        </div>      
    </form>           

    <form method="get" action="search/l" id="name"> 

                <input  type="text" id="studentName" name="studentName" size="30" maxLength="50" >

    </form>     

我希望两个字段一个接一个地在一行中并居中对齐。我该怎么做?

【问题讨论】:

  • 你试过#number{ float:left; } ?
  • 你能给我一个例子吗?

标签: javascript html css user-interface web


【解决方案1】:

将它们放在一个额外的容器元素中,例如 div 和 text-align:center。然后只需将display:inline-block 赋予form 元素以覆盖默认的block 渲染,这会强制它们进入新行。

Sample fiddle.

【讨论】:

  • 我想知道使用容器和 css 是否会成为 UI 端的性能问题。关于浏览器加载和 DOM 的性能。只是好奇。
  • 不在这个级别。使用数十万,它最终可能会在统计上显着。更可能需要数百万才能使其可衡量。而且很可能您仍然需要它们才能使用这么多元素正确渲染。
  • 如果您对性能感到好奇 - 在 Facebook 中间的某个地方点击“检查元素”,然后问问自己加载速度有多快。
【解决方案2】:

您想省略第一个输入周围的 div - 它是 display: block by nature 和 blocks 自动在新行上对齐。 .container div 应具有固定宽度(或 px 或 %),以便居中工作。

<div class="container">
<form method="get" action="search/s" id="number">

            <input  type="text" id="regNo" name="regNo" size="30" maxLength="50" > or     

</form>           

<form method="get" action="search/l" id="name"> 

            <input  type="text" id="studentName" name="studentName" size="30" maxLength="50" >

</form>
</div>
<style>

    .container {
    text-align: center;
}

【讨论】:

  • 你在这里做了一些非常邪恶的事情。首先,display:inline 对于像form 这样的自然块元素是非常危险的。在这种情况下它会正常运行,但通常你应该更喜欢inline-block。其次,您假设内容的宽度是固定的,这是严格限制且不需要的。
  • 我编辑了答案,所以表单不是内联的。对我来说似乎有点邪恶的是 text-align: center of input fields - 我宁愿将它们都固定在(即 50%)上,并且容器也可以固定在 ie 上。 75% 的母元素 - 这应该与响应式设计相得益彰......
  • text-align 用于控制容器内inline 元素的呈现。 input 元素默认为 inline,这样使用它们并没有什么坏处。关于如何改进布局的建议很好,但与手头的问题无关,其他方式更容易灵活地解决。
  • 是的,你是对的。让我将其更改为最简单的解决方案。我指定了输入显示:inline,以防它之前被操作过...
【解决方案3】:

您只需要在第一个表单中向左浮动。 JSFIDDLE你问的例子

#number{
 float:left;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2019-11-21
    • 1970-01-01
    • 2023-03-06
    • 2018-06-24
    • 1970-01-01
    相关资源
    最近更新 更多