简体   繁体   English

当某些标签跨越多行时,如何垂直对齐表单控件?

[英]How can I align form-controls vertically when some of the labels span multiple lines?

Using Bootstrap 3, I have a horizontal form with labels on top of the controls.使用 Bootstrap 3,我在控件顶部有一个带有标签的水平表单。 When one of the labels is too long, the text is wrapped to the next line (which is fine).当其中一个标签太长时,文本将换行到下一行(这很好)。 However, this shifts the input box down (see below).但是,这会使输入框向下移动(见下文)。 How can I align the other input vertically so that it doesn't look weird?如何垂直对齐其他输入,使其看起来不奇怪?

Example:例子:

<div class="form-group">
  <div class="row">
    <div class="col-sm-6">
      <label for="one">Label one</label>
      <input type="text" id="one" class="form-control">
    </div>
    <div class="col-sm-6">
      <label for="two">Label two is a very long label that will span at least two lines and shift the input box down a bit and make this form look really weird. How do I fix this?</label>
      <input type="text" id="two" class="form-control">
    </div>
  </div>
</div>

This looks like this:这看起来像这样: 在此处输入图像描述

But I would like it to look like this:但我希望它看起来像这样: 在此处输入图像描述

Here's a JSFiddle example: https://jsfiddle.net/13c420x6/2/这是一个 JSFiddle 示例: https://jsfiddle.net/13c420x6/2/

Would is be an idea to use CSS Grid Layout ?使用CSS 网格布局是一个想法吗? I striped of some of the elements to make the example more clear.我删除了一些元素以使示例更清晰。

 div.form-group { display: grid; grid-template-columns: auto; grid-template-rows: auto auto; }.g1 { grid-column: 1; grid-row: 1; }.g2 { grid-column: 1; grid-row: 2; }.g3 { grid-column: 2; grid-row: 1; }.g4 { grid-column: 2; grid-row: 2; } @media screen and (max-width: 500px) {.g3 { grid-column: 1; grid-row: 3; }.g4 { grid-column: 1; grid-row: 4; } }
 <div class="form-group"> <label class="g1" for="one">Label one</label> <input class="g2 form-control" type="text" id="one"> <label class="g3" for="two">Label two is a very long label that will span at least two lines and shift the input box down a bit and make this form look really weird. How do I fix this?</label> <input class="g4 form-control" type="text" id="two"> </div>

You can also use CSS Flexbox .您也可以使用 CSS Flexbox The label element can just wrap the input element. label 元素可以只包装输入元素。 Then you can remove the for attribute.然后您可以删除 for 属性。

 div.form-group { display: flex; } label { display: flex; flex-direction: column; justify-content: space-between; } @media screen and (max-width: 500px) { div.form-group { flex-direction: column; } }
 <div class="form-group"> <label>Label one<input class="form-control" type="text" id="one"></label> <label>Label two is a very long label that will span at least two lines and shift the input box down a bit and make this form look really weird. How do I fix this?<input class="form-control" type="text" id="two"></label> </div>

You can use justify-content: space-between but you need to assign display: flex and make a flex-direction: column .您可以使用justify-content: space-between但您需要分配display: flex并制作一个flex-direction: column Since there is no pre built in class in bootstrap 3. You should do this in your CSS file由于在引导程序 3 中没有预建 class。您应该在 CSS 文件中执行此操作

 .row{ display: flex; }.first-col{ display: flex; flex-direction: column; justify-content: space-between; }
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"> <div class="form-group"> <div class="row"> <div class="col-sm-6 first-col"> <label for="one">Label one</label> <input type="text" id="one" class="form-control"> </div> <div class="col-sm-6"> <label for="two">Label two is a very long label that will span at least two lines and shift the input box down a bit and make this form look really weird. How do I fix this?</label> <input type="text" id="two" class="form-control"> </div> </div> </div>

And I am suggest you to shift to bootstrap 4. Since it has more pre built classes and more.我建议你转向 bootstrap 4。因为它有更多的预建类等等。

Bootstrap 4 Answer (You can compare it with upper one). Bootstrap 4答案(您可以将其与上一个进行比较)。

 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous"> <div class="form-group"> <div class="row"> <div class="col-sm-6 d-flex flex-column justify-content-between"> <label for="one">Label one</label> <input type="text" id="one" class="form-control"> </div> <div class="col-sm-6"> <label for="two">Label two is a very long label that will span at least two lines and shift the input box down a bit and make this form look really weird. How do I fix this?</label> <input type="text" id="two" class="form-control"> </div> </div> </div>

You can use Flex to make it work.您可以使用 Flex 使其工作。

 .row { display: flex; flex-wrap: wrap; }.input-groups { display: flex; flex-direction: column; justify-content: space-between; } @media (max-width: 768px) {.col-sm-6 { width: 100%; } }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="form-group"> <div class="row"> <div class="col-sm-6 input-groups"> <label for="one">Label one</label> <input type="text" id="one" class="form-control" /> </div> <div class="col-sm-6 input-groups"> <label for="two">Label two is a very long label that will span at least two lines and shift the input box down a bit and make this form look really weird. How do I fix this?</label > <input type="text" id="two" class="form-control" /> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM