简体   繁体   English

使用Bootstrap 3设置内联表单输入的样式

[英]Styling inlined form inputs with Bootstrap 3

I'm trying to create a simple form using Bootstrap 3 where people can sign up for a small event. 我正在尝试使用Bootstrap 3创建一个简单的表单,人们可以在其中注册一个小活动。 On this form, they should also be able to indicate if they want one or more T-shirts, and which size(s) they want to order. 在此表格上,他们还应该能够表明他们是否想要一件或多件T恤,以及他们想订购的尺码。 I'm not new to HTML, CSS and Javascript, but I am quite new to Bootstrap. 我对HTML,CSS和Java并不陌生,但是对Bootstrap还是很陌生。 Also while I'm a programmer by day, I'm mainly writing REST services so my design skills are fairly limited. 另外,尽管我每天都是程序员,但我主要是在编写REST服务,因此我的设计技能受到很大限制。 Using w3schools and other tutorial sites I have arrived at this: 使用w3schools和其他教程网站,我得出了以下结论:

    <div class="form-group form-group-sm">
        <p class="help-block">Please select the number of T-shirts per size you would like to order.</p>
        <label class="col-xs-1" for="shirt-s">S</label>
        <div class="col-xs-1">
            <input class="form-control" type="text" id="shirt-s">
        </div>
        <label class="col-xs-1" for="shirt-m">M</label>
        <div class="col-xs-1">
            <input class="form-control" type="text" id="shirt-m">
        </div>
        <label class="col-xs-1" for="shirt-l">L</label>
        <div class="col-xs-1">
            <input class="form-control" type="text" id="shirt-l">
        </div>
        <label class="col-xs-1" for="shirt-xl">XL</label>
        <div class="col-xs-1">
            <input class="form-control" type="text" id="shirt-xl">
        </div>
        <label class="col-xs-1" for="shirt-xxl">XXL</label>
        <div class="col-xs-1">
            <input class="form-control" type="text" id="shirt-xxl">
        </div>
        <div class="col-xs-2">&nbsp;</div>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-default">Submit</button>
    </div>

See jsfiddle: 参见jsfiddle:

https://jsfiddle.net/tonvanbart/83nbny8h/5/ https://jsfiddle.net/tonvanbart/83nbny8h/5/

Not the prettiest in the world, but I can live with it. 不是世界上最漂亮的,但我可以忍受。 My main question is: why is the 'submit' button not in it's own row but inlined after the input fields? 我的主要问题是:为什么“提交”按钮不在其自己的行中,而是在输入字段之后内联? At first I thought I had to get to a total of 12 columns and I only had 10. But adding a 2-column div with a non-breaking space didn't help, and adding a <br> tag had no effect either. 起初,我以为我只能达到12列,而我只有10列。但是添加一个2列的div且不间断的空格也无济于事,而添加<br>标记也没有效果。

Also, while I can live with this (it's for an informal, small group of people who know each other well) if anybody could give me a pointer on how to get the T-shirt size indicators closer to their respective input fields, that would be great. 另外,尽管我可以接受(这是一个互相了解的非正式,一小组人),但是如果有人可以给我一个指示,如何使T恤尺寸指示器更接近各自的输入字段,那将很棒。 Thank you in advance. 先感谢您。

Feel free to let me know if you need more information. 如果您需要更多信息,请随时告诉我。

Here's the fixed code: 这是固定代码:

https://jsfiddle.net/ccx9x7om/1/ https://jsfiddle.net/ccx9x7om/1/

I added the following CSS 我添加了以下CSS

.row .form-group label, .row .form-group input {
  display: inline-block;
}

.row .form-group input {
  width: 50%;
}

button {
  margin: 25px;
}

In order to use bootstrap's columns properly, you need to wrap them in a row, and wrap all rows inside either a .container or a .container-fluid . 为了正确使用引导程序的列,您需要将它们包装成一行,并将所有行包装在.container.container-fluid I moved the labels inside their respective .col-xs-2 then wrapped everything in the needed aforementioned divs. 我将标签移到它们各自的.col-xs-2然后将所有内容包装在所需的上述div中。

Another way you could do this is by nesting your columns as to split the form in half which will greatly reduce the screen space it's using and bring your inputs closer while remaining responsive across viewports. 您可以执行此操作的另一种方法是,将列嵌套以将窗体分成两半,这将大大减少它正在使用的屏幕空间,并在保持跨视口响应的同时使输入更近。

This is simply following the container/row/column layout that Bootstrap generally follows. 这只是遵循Bootstrap通常遵循的容器/行/列布局。 See Nesting Columns . 请参阅嵌套列

*Note: The form-group class isn't necessary, it simply adds padding around your inputs. *注意: form-group类不是必需的,它只是在输入周围添加了填充。

See working Snippet at FullPage. 请参阅FullPage上的有效代码段。

 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <form> <h2>Order a Shirt</h2> <div class="row"> <div class="col-sm-6"> <div class="form-group"> <label for="name">Name</label> <input type="password" class="form-control" id="name" placeholder="Your Name"> </div> <div class="form-group"> <label for="email">Email address</label> <input type="email" class="form-control" id="email" placeholder="Email address (never shown)"> </div> </div> <div class="col-sm-6"> <div class="row"> <div class="col-xs-4 col-sm-4"> <div class="form-group"> <label for="shirt-s">S</label> <input class="form-control" type="text" id="shirt-s"> </div> </div> <div class="col-xs-4 col-sm-4"> <div class="form-group"> <label for="shirt-m">M</label> <input class="form-control" type="text" id="shirt-m"> </div> </div> <div class="col-xs-4 col-sm-4"> <div class="form-group"> <label for="shirt-l">L</label> <input class="form-control" type="text" id="shirt-l"> </div> </div> </div> <div class="row"> <div class="col-xs-4 col-sm-4"> <div class="form-group"> <label for="shirt-xl">XL</label> <input class="form-control" type="text" id="shirt-xl"> </div> </div> <div class="col-xs-4 col-sm-4"> <div class="form-group"> <label for="shirt-xxl">XXL</label> <input class="form-control" type="text" id="shirt-xxl"> </div> </div> <div class="col-xs-4 col-sm-4"> <div class="form-group"> <label>Order Now</label> <button type="submit" class="btn btn-success btn-block btn-submit">Submit</button> </div> </div> <div class="col-xs-12"> <p class="help-block">Please select the number of T-shirts per size you would like to order.</p> </div> </div> </div> </div> </form> </div> 

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

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