HTML form with side by side input fields

后端 未结 6 1675
离开以前
离开以前 2021-02-05 10:52

I have a html form that is basically vertical but i really have no idea how to make two text fields on the same line. For example the following form below i want the First and L

相关标签:
6条回答
  • 2021-02-05 11:19

    Put style="float:left" on each of your divs:

    <div style="float:left;">...........
    

    Example:

    <div style="float:left;">
      <label for="username">First Name</label>
      <input id="user_first_name" name="user[first_name]" size="30" type="text" />
    </div>
    
    <div style="float:left;">
      <label for="name">Last Name</label>
      <input id="user_last_name" name="user[last_name]" size="30" type="text" />
    </div>
    

    To put an element on new line, put this div below it:

    <div style="clear:both;">&nbsp;</div>
    

    Of course, you can also create classes in the CSS file:

    .left{
      float:left;
    }
    
    .clear{
      clear:both;
    }
    

    And then your html should look like this:

    <div class="left">
      <label for="username">First Name</label>
      <input id="user_first_name" name="user[first_name]" size="30" type="text" />
    </div>
    
    <div class="left">
      <label for="name">Last Name</label>
      <input id="user_last_name" name="user[last_name]" size="30" type="text" />
    </div>
    

    To put an element on new line, put this div below it:

    <div class="clear">&nbsp;</div>
    

    More Info:

    • CSS Float Clear Tutorial
    0 讨论(0)
  • 2021-02-05 11:26

    For the sake of bandwidth saving, we shouldn't include <div> for each of <label> and <input> pair

    This solution may serve you better and may increase readability

    <div class="form">
                <label for="product_name">Name</label>
                <input id="product_name" name="product[name]" size="30" type="text" value="4">
                <label for="product_stock">Stock</label>
                <input id="product_stock" name="product[stock]" size="30" type="text" value="-1">
                <label for="price_amount">Amount</label>
                <input id="price_amount" name="price[amount]" size="30" type="text" value="6.0">
    </div>
    

    The css for above form would be

    .form > label
    {
      float: left;
      clear: right;
    }
    
    .form > input
    {
      float: right;
    }
    

    I believe the output would be as following:

    Demo

    0 讨论(0)
  • 2021-02-05 11:27

    I would go with Larry K's solution, but you can also set the display to inline-block if you want the benefits of both block and inline elements.

    You can do this in the div tag by inserting:

    style="display:inline-block;"
    

    Or in a CSS stylesheet with this method:

    div { display:inline-block; }
    

    Hope it helps, but as earlier mentioned, I would personally go for Larry K's solution ;-)

    0 讨论(0)
  • 2021-02-05 11:33

    The default display style for a div is "block." This means that each new div will be under the prior one.

    You can:

    Override the flow style by using float as @Sarfraz suggests.

    or

    Change your html to use something other than divs for elements you want on the same line. I suggest that you just leave out the divs for the "last_name" field

    <form action="/users" method="post"><div style="margin:0;padding:0">
      <div>
        <label for="username">First Name</label>
        <input id="user_first_name" name="user[first_name]" size="30" type="text" />
    
        <label for="name">Last Name</label>
        <input id="user_last_name" name="user[last_name]" size="30" type="text" />
      </div>
      ... rest is same
    
    0 讨论(0)
  • 2021-02-05 11:34

    You could use the {display: inline-flex;} this would produce this: inline-flex

    0 讨论(0)
  • 2021-02-05 11:35

    You should put the input for the last name into the same div where you have the first name.

    <div>
        <label for="username">First Name</label>
        <input id="user_first_name" name="user[first_name]" size="30" type="text" />
        <input id="user_last_name" name="user[last_name]" size="30" type="text" />
    </div>
    

    Then, in your CSS give your #user_first_name and #user_last_name height and float them both to the left. For example:

    #user_first_name{
        max-width:100px; /*max-width for responsiveness*/
        float:left;
    }
    
    #user_lastname_name{
        max-width:100px;
        float:left;
    }
    
    0 讨论(0)
提交回复
热议问题