【问题标题】:How to align two form fields on the same row in html page using css?如何使用css在html页面的同一行中对齐两个表单字段?
【发布时间】:2019-11-21 00:14:12
【问题描述】:

我有这个 html 文本

<meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


<div class="glossary-form"></div>

<div class="form-group" style="display:inline-block; width:48%;padding-right: 5%;">
  <label for="glossary_entry_input_1">Lemma IT</label>
  <input type="text" class="form-control" id="glossary_entry_input_1" placeholder="">              
</div>

<div class="form-group" style="display:inline-block; width:48%; padding-left: 5%;">
		<label for="glossary_entry_input_2">Lemma CH</label>
		<input type="text" class="form-control" id="glossary_entry_input_2" placeholder="">              
</div>

<div class="form-group">
                    <label for="glossary_entry_input_3">Acronimo IT</label>
                    <small id="inputHelp" class="form-text text-muted">Inserire una sigla (se esiste) del Lemma. Nel caso in cui il lemma sia una grandezza fisica, inserire la lettera o il simbolo che la identifica.</small>
                    <input type="text" class="form-control" id="glossary_entry_input_3" placeholder="">              
            </div>  

</div>

生成以下表单域,在一行上对齐,如您所见

我想通过将第二个向右移动,如您所见,将它们与我的 html 页面的垂直轴对称地显示

使“引理 CH”框的左边距与“Acronimo IT”框之一对齐。 如何更改我的代码才能获得第二张图片中显示的结果?

【问题讨论】:

  • 您正在使用引导程序,因此请根据您的要求使用引导程序网格
  • 在第二张图片中,“引理 CH”框向右移动了一点,使其右边距与下面的“Acronimo IT”框相同。
  • 请创建一个有效的 sn-p 我们将能够为您提供帮助。
  • 感谢 Sumit Patel。我添加了工作的 sn-p。
  • @Tms91 我给个答案。检查。它可能会帮助你。

标签: css forms css-float styling border-box


【解决方案1】:

向右浮动不是正确的解决方案。试试这个。

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
  integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


<div class="glossary-form"></div>
<div class="row">
  <div class="col-md-6">
    <div class="form-group">
      <label for="glossary_entry_input_1">Lemma IT</label>
      <input type="text" class="form-control" id="glossary_entry_input_1" placeholder="">
    </div>
  </div>
  <div class="col-md-6">
    <div class="form-group">
      <label for="glossary_entry_input_2">Lemma CH</label>
      <input type="text" class="form-control" id="glossary_entry_input_2" placeholder="">
    </div>
  </div>

</div>
<div class="row">
  <div class="col-md-12">
    <div class="form-group">
      <label for="glossary_entry_input_3">Acronimo IT</label>
      <small id="inputHelp" class="form-text text-muted">Inserire una sigla (se esiste) del Lemma. Nel caso in cui il
        lemma sia una grandezza fisica, inserire la lettera o il simbolo che la identifica.</small>
      <input type="text" class="form-control" id="glossary_entry_input_3" placeholder="">
    </div>

  </div>
</div>
</div>

【讨论】:

  • 您好,请问您可以删除包含“favicon”的行吗?只是因为我不希望任何人对这个问题重新进行我的工作。谢谢。也感谢您的帮助,我赞成您的问题
  • 实际上这个答案并没有解决我的问题,因为我看到两个引理框显示在另一个下方,而我需要它们水平对齐。
  • 那是因为你是在小屏幕上查看,所以请在整页查看sn-p,你会看到。在较小的设备引导程序中将使其达到 100%。向右浮动不是一个好的解决方案。你的问题是你没有使用引导网格,它可以让你更好地控制 DOM,而且我们都知道引导最适合响应式。
  • 你是对的。抱歉,我之前已经打过绿卡了,其他人只是更快
  • 仅供参考,如果您觉得其他答案更好,您可以根据需要更改答案。
【解决方案2】:

您可以使用他们的文档..

https://getbootstrap.com/docs/4.0/components/forms/#form-row

我建议您使用 bootstrap 文档,因为无论如何您都在使用 bootstrap。创建您的表单将需要进一步的 CSS 来满足响应式设计。使用文档和引导程序会照顾你。

希望这会有所帮助。

【讨论】:

  • 谢谢,下次找响应式设计
  • 没问题,@Sumit Patel 的答案是最好的,因为他使用了引导网格......这意味着虽然只使用引导类(没有内联 CSS),但你已经完全实现了你的要求,也是开箱即用的响应式,无需额外的 CSS……您接受的答案将导致进一步的代码再次满足响应式设计。既然您已经在使用 bootstrap 来创建网页,那么没有理由不使用它并更加熟悉它。
【解决方案3】:

这是我的解决方案。我已将以下代码添加到“引理 CH”框中。希望它能满足您的要求。

float: right;

<meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


<div class="glossary-form"></div>

<div class="form-group" style="display:inline-block; width:48%;padding-right: 5%;">
  <label for="glossary_entry_input_1">Lemma IT</label>
  <input type="text" class="form-control" id="glossary_entry_input_1" placeholder="">              
</div>

<div class="form-group" style="display:inline-block; width:48%; padding-left: 5%; float: right;">
		<label for="glossary_entry_input_2">Lemma CH</label>
		<input type="text" class="form-control" id="glossary_entry_input_2" placeholder="">              
</div>

<div class="form-group">
                    <label for="glossary_entry_input_3">Acronimo IT</label>
                    <small id="inputHelp" class="form-text text-muted">Inserire una sigla (se esiste) del Lemma. Nel caso in cui il lemma sia una grandezza fisica, inserire la lettera o il simbolo che la identifica.</small>
                    <input type="text" class="form-control" id="glossary_entry_input_3" placeholder="">              
            </div>  

</div>

【讨论】:

  • 这正是我们想要的!谢谢!
  • 您好,请问您可以删除包含“favicon”的行吗?只是因为我不希望任何人对这个问题重新进行我的工作。谢谢。
  • 当然是@Tms91。我刚刚删除了它。其实我之前没有注意到。 :(
猜你喜欢
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
相关资源
最近更新 更多