Label on the left side instead above an input field

前端 未结 15 825
太阳男子
太阳男子 2020-12-02 09:44

I would like to have the labels not above the input field, but on the left side.

相关标签:
15条回答
  • 2020-12-02 10:06

    You can create such form where label and form control are side using two method -

    1. Inline form layout

    <form class="form-inline" role="form">
        <div class="form-group">
            <label for="inputEmail">Email</label>
            <input type="email" class="form-control" id="inputEmail" placeholder="Email">
        </div>
        <div class="form-group">
            <label for="inputPassword">Password</label>
            <input type="password" class="form-control" id="inputPassword" placeholder="Password">
        </div>
        <button type="reset" class="btn btn-default">Reset</button>
        <button type="submit" class="btn btn-primary">Login</button>
    </form>
    

    2. Horizontal Form Layout

    <form class="form-horizontal">
        <div class="row">
            <div class="col-sm-4">
                <div class="form-group">
                    <label for="inputEmail" class="control-label col-xs-3">Email</label>
                    <div class="col-xs-9">
                        <input type="email" class="form-control" id="inputEmail" placeholder="Email">
                    </div>
                </div>
            </div>
            <div class="col-sm-5">
                <div class="form-group">
                    <label for="inputPassword" class="control-label col-xs-3">Password</label>
                    <div class="col-xs-9">
                        <input type="password" class="form-control" id="inputPassword" placeholder="Password">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <button type="reset" class="btn btn-default">Reset</button>
                <button type="submit" class="btn btn-primary">Login</button>
            </div>
        </div>
    </form>
    

    You can check out this page for more information and live demo - http://www.tutorialrepublic.com/twitter-bootstrap-tutorial/bootstrap-forms.php

    0 讨论(0)
  • 2020-12-02 10:10

    You can see from the existing answers that Bootstrap's terminology is confusing. If you look at the bootstrap documentation, you see that the class form-horizontal is actually for a form with fields below each other, i.e. what most people would think of as a vertical form. The correct class for a form going across the page is form-inline. They probably introduced the term inline because they had already misused the term horizontal.

    You see from some of the answers here that some people are using both of these classes in one form! Others think that they need form-horizontal when they actually want form-inline.

    I suggest to do it exactly as described in the Bootstrap documentation:

    <form class="form-inline">
      <div class="form-group">
        <label for="nameId">Name</label>
        <input type="text" class="form-control" id="nameId" placeholder="Jane Doe">
      </div>
    </form>
    

    Which produces:

    enter image description here

    0 讨论(0)
  • 2020-12-02 10:13

    You can use form-inline class for each form-group :)

    <form>                      
        <div class="form-group form-inline">                            
            <label for="exampleInputEmail1">Email address</label>
                <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
        </div>
    </form>
    
    0 讨论(0)
  • 2020-12-02 10:13

    I had the same problem, here is my solution:

    <form method="post" class="form-inline form-horizontal" role="form">
            <label class="control-label col-sm-5" for="jbe"><i class="icon-envelope"></i> Email me things like this: </label>
            <div class="input-group col-sm-7">
                <input class="form-control" type="email" name="email" placeholder="your.email@example.com"/>
                <span class="input-group-btn">
                    <button class="btn btn-primary" type="submit">Submit</button>
                </span>
            </div>
        </form>
    

    here is the Demo

    0 讨论(0)
  • 2020-12-02 10:14

    Put the <label> outside the form-group:

    <form class="form-inline">
      <label for="rg-from">Ab: </label>
      <div class="form-group">
        <input type="text" id="rg-from" name="rg-from" value="" class="form-control">
      </div>
      <!-- rest of form -->
    </form>
    
    0 讨论(0)
  • 2020-12-02 10:17

    The Bootstrap 3 documentation talks about this in the CSS documentation tab in the section labelled "Requires custom widths", which states:

    Inputs, selects, and textareas are 100% wide by default in Bootstrap. To use the inline form, you'll have to set a width on the form controls used within.

    If you use your browser and Firebug or Chrome tools to suppress or reduce the "width" style, you should see things line up they way you want. Clearly you can then create the appropriate CSS to fix the issue.

    However, I find it odd that I need to do this at all. I couldn't help but feel this manipulation was both annoying and in the long term, error prone. Ultimately, I used a dummy class and some JS to globally shim all my inline inputs. It was small number of cases, so not much of a concern.

    Nonetheless, I too would love to hear from someone who has the "right" solution, and could eliminate my shim/hack.

    Hope this helps, and props to you for not blowing a gasket at all the people that ignored your request as a Bootstrap 3 concern.

    0 讨论(0)
提交回复
热议问题