How to make

后端 未结 12 1233
日久生厌
日久生厌 2020-12-12 15:37

I am creating a registration form for a website. I want each label and its corresponding input element to appear on the same line.

Here\'s my code:

相关标签:
12条回答
  • 2020-12-12 16:31

    Assuming you want to float the elements, you would also have to float the label elements too.

    Something like this would work:

    label {
        /* Other styling... */
        text-align: right;
        clear: both;
        float:left;
        margin-right:15px;
    }
    

    #form {
        background-color: #FFF;
        height: 600px;
        width: 600px;
        margin-right: auto;
        margin-left: auto;
        margin-top: 0px;
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
        padding: 0px;
        text-align:center;
    }
    label {
        font-family: Georgia, "Times New Roman", Times, serif;
        font-size: 18px;
        color: #333;
        height: 20px;
        width: 200px;
        margin-top: 10px;
        margin-left: 10px;
        text-align: right;
        clear: both;
        float:left;
        margin-right:15px;
    }
    input {
        height: 20px;
        width: 300px;
        border: 1px solid #000;
        margin-top: 10px;
        float: left;
    }
    input[type=button] {
        float:none;
    }
    <div id="form">
        <form action="" method="post" name="registration" class="register">
            <fieldset>
                <label for="Student">Name:</label>
                <input name="Student" id="Student" />
                <label for="Matric_no">Matric number:</label>
                <input name="Matric_no" id="Matric_no" />
                <label for="Email">Email:</label>
                <input name="Email" id="Email" />
                <label for="Username">Username:</label>
                <input name="Username" id="Username" />
                <label for="Password">Password:</label>
                <input name="Password" id="Password" type="password" />
                <input name="regbutton" type="button" class="button" value="Register" />
            </fieldset>
        </form>
    </div>

    Alternatively, a more common approach would be to wrap the input/label elements in groups:

    <div class="form-group">
        <label for="Student">Name:</label>
        <input name="Student" id="Student" />
    </div>
    

    #form {
        background-color: #FFF;
        height: 600px;
        width: 600px;
        margin-right: auto;
        margin-left: auto;
        margin-top: 0px;
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
        padding: 0px;
        text-align:center;
    }
    label {
        font-family: Georgia, "Times New Roman", Times, serif;
        font-size: 18px;
        color: #333;
        height: 20px;
        width: 200px;
        margin-top: 10px;
        margin-left: 10px;
        text-align: right;
        margin-right:15px;
        float:left;
    }
    input {
        height: 20px;
        width: 300px;
        border: 1px solid #000;
        margin-top: 10px;
    }
    <div id="form">
        <form action="" method="post" name="registration" class="register">
            <fieldset>
                <div class="form-group">
                    <label for="Student">Name:</label>
                    <input name="Student" id="Student" />
                </div>
                <div class="form-group">
                    <label for="Matric_no">Matric number:</label>
                    <input name="Matric_no" id="Matric_no" />
                </div>
                <div class="form-group">
                    <label for="Email">Email:</label>
                    <input name="Email" id="Email" />
                </div>
                <div class="form-group">
                    <label for="Username">Username:</label>
                    <input name="Username" id="Username" />
                </div>
                <div class="form-group">
                    <label for="Password">Password:</label>
                    <input name="Password" id="Password" type="password" />
                </div>
                <input name="regbutton" type="button" class="button" value="Register" />
            </fieldset>
        </form>
    </div>

    Note that the for attribute should correspond to the id of a labelable element, not its name. This will allow users to click the label to give focus to the corresponding form element.

    0 讨论(0)
  • 2020-12-12 16:32

    I've done this several different ways but the only way I've found that keeps the labels and corresponding text/input data on the same line and always wraps perfectly to the width of the parent is to use display:inline table.

    CSS

    .container {
      display: inline-table;
      padding-right: 14px;
      margin-top:5px;
      margin-bottom:5px;
    }
    .fieldName {
      display: table-cell;
      vertical-align: middle;
      padding-right: 4px;
    }
    .data {
      display: table-cell;
    }
    

    HTML

    <div class='container'>
        <div class='fieldName'>
            <label>Student</label>
        </div>
        <div class='data'>
            <input name="Student" />
        </div>
    </div>
    <div class='container'>
        <div class='fieldName'>
            <label>Email</label>
        </div>
        <div class='data'>
          <input name="Email" />
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-12 16:34

    I found "display:flex" style is a good way to make these elements in same line. No matter what kind of element in the div. Especially if the input class is form-control,other solutions like bootstrap, inline-block will not work well.

    Example:

    <div style="display:flex; flex-direction: row; justify-content: center; align-items: center">
        <label for="Student">Name:</label>
        <input name="Student" />
    </div>
    

    More detail about display:flex:

    flex-direction: row, column

    justify-content: flex-end, center, space-between, space-around

    align-items: stretch, flex-start, flex-end, center

    0 讨论(0)
  • 2020-12-12 16:35

    What you were missing was the float: left; here is an example just done in the HTML

    <div id="form">
    <form action="" method="post" name="registration" class="register">
        <fieldset>
            <label for="Student" style="float: left">Name:</label>
            <input name="Student" />
            <label for="Matric_no" style="float: left">Matric number:</label>
            <input name="Matric_no" />
            <label for="Email" style="float: left">Email:</label>
            <input name="Email" />
            <label for="Username" style="float: left">Username:</label>
            <input name="Username" />
            <label for="Password" style="float: left">Password:</label>
            <input name="Password" type="password" />
            <input name="regbutton" type="button" class="button" value="Register" />
        </fieldset>
    </form>
    

    The more efficient way to do this is to add a class to the labels and set the float: left; to the class in CSS

    0 讨论(0)
  • 2020-12-12 16:35

    For Bootstrap 4 it could be done with class="form-group" style="display: flex"

    <div class="form-group" style="display: flex">
        <label>Topjava comment:</label>
        <input class="form-control" type="checkbox"/>
    </div>
    
    0 讨论(0)
  • 2020-12-12 16:37

    Aside from using floats, as others have suggested, you can also rely on a framework such as Bootstrap where you can use the "horizontal-form" class to have the label and input on the same line.

    If you're unfamiliar with Bootstrap, you would need to include:

    • the link to the Bootstrap CSS (the top link where it says < -- Latest compiled and minified CSS -- >), in the head, as well as add some divs:
    • div class="form-group"
    • div class="col-..."
    • along with class="col-sm-2 control-label" in each label, as Bootstrap shows, which I included below.
    • I also reformatted your button so it's Bootstrap compliant.
    • and added a legend, to your form box, since you were using a fieldset.

    It's very straight forward and you wouldn't have to mess with floats or a ton of CSS for formatting, as you listed above.

      <div id="form">
        <div class="row">
          <form action="" method="post" name="registration" class="register form-horizontal">
            <fieldset>
            <legend>Address Form</legend>
    
          <div class="form-group">
            <label for="Student" class="col-sm-2 control-label">Name:</label>
              <div class="col-sm-6">
                <input name="Student" class="form-control">
              </div>
          </div>
    
          <div class="form-group">
            <label for="Matric_no" class="col-sm-2 control-label">Matric number: </label>
              <div class="col-sm-6">
                <input name="Matric_no" class="form-control">
              </div>
          </div>
    
          <div class="form-group">
            <label for="Email" class="col-sm-2 control-label">Email: </label>
              <div class="col-sm-6">
                <input name="Email" class="form-control">
              </div>
          </div>
    
          <div class="form-group">
            <label for="Username" class="col-sm-2 control-label">Username: </label>
              <div class="col-sm-6">
                <input name="Username" class="form-control">
              </div>
          </div>
    
          <div class="form-group">
            <label for="Password" class="col-sm-2 control-label">Password: </label>
              <div class="col-sm-6">
                <input name="Password" type="password" class="form-control">
              </div>
          </div>
    
          <div>
            <button class="btn btn-info" name="regbutton" value="Register">Submit</button>
          </div>
    
          </fieldset>
        </form>
        </div>
      </div>
    </div>
    

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