Align form elements in CSS

后端 未结 3 1061
耶瑟儿~
耶瑟儿~ 2020-11-29 07:31

I\'m new to CSS and have a simple login form that I\'m trying to align correctly. Basically two columns with the labels and the Login button in one column and the t

相关标签:
3条回答
  • 2020-11-29 07:53

    This is one approach that works:

    form {
      width: 80%;
      margin: 0 auto;
    }
    
    label,
    input {
      /* In order to define widths */
      display: inline-block;
    }
    
    label {
      width: 30%;
      /* Positions the label text beside the input */
      text-align: right;
    }
    
    label+input {
      width: 30%;
      /* Large margin-right to force the next element to the new-line
               and margin-left to create a gutter between the label and input */
      margin: 0 30% 0 4%;
    }
    
    
    /* Only the submit button is matched by this selector,
           but to be sure you could use an id or class for that button */
    
    input+input {
      float: right;
    }
    <form action="#" method="post">
      <!-- note that I've added a 'for' attribute to
           both of the <label> elements, which is
           equal to the 'id' attribute of the relevant
           <input> element; this means that clicking
           the <label> will focus the <input>: -->
      <label for="username">Username</label>
      <input id="username" type="text" />
    
      <label for="password">Password</label>
      <input id="password" type="password" />
    
      <input type="submit" value="submit" />
    </form>

    JS Fiddle demo.

    Adjust dimensions and margins to suit your use-case and aesthetic of course.

    Of course, currently, there are other means by which this can work, such as CSS Grid:

    *,
    ::before,
    ::after {
      /* selecting all elements on the page, along with the ::before
         and ::after pseudo-elements; resetting their margin and
         padding to zero and forcing all elements to calculate their
         box-sizing the same way, 'border-box' includes the border-widths,
         and padding, in the stated width: */
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    
    form {
      /* Using CSS Grid to lay out the elements in two-dimensions: */
      display: grid;
      /* specifying a 0.5em gutter/gap between adjacent elements: */
      gap: 0.5em;
      /* declaring a number of named grid areas in order to lay out
         the child elements; the areas identified with a period (.)
         are 'empty' areas, whereas the areas named by strings are
         used, later, to place elements according to those names: */
      grid-template-areas:
        "usernameLabel . usernameInput"
        "passwordLabel . passwordInput"
        ". . submit";
      /* declaring the size of each of the three columns; 1fr is
         one fractional unit of the available space, and is the
         size of the first and last of the columns. The central
         column is declared as being 0.5em in width: */
      grid-template-columns: 1fr 0.5em 1fr;
      margin: 1em auto;
      width: 80%;
    }
    
    label {
      /* placing all <label> elements in the grid column 1 (the first): */
      grid-column: 1;
      /* aligning text-content to the right in order to position the label
         text near to the relevant <input>: */
      text-align: right;
    }
    
    label::after {
      content: ':';
    }
    
    input {
      grid-column: 3;
    }
    
    button {
      /* positioning the <button> element in the grid-area identified
         by the name of 'submit': */
      grid-area: submit;
    }
    <form action="" method="post">
      <label for="username">Username</label>
      <input id="username" type="text" />
    
      <label for="password">Password</label>
      <input id="password" type="password" />
    
      <input type="submit" value="submit" />
    </form>

    JS Fiddle demo.

    References:

    • Adjacent-sibling (+) combinator.
    • box-sizing.
    • display.
    • float.
    • gap.
    • grid-area.
    • grid-template-areas.
    • grid-template-columns.
    • grid-template-rows.
    • margin.
    • width.

    Bibliography:

    • "Basic Concepts of grid layout."
    0 讨论(0)
  • 2020-11-29 07:54

    You can also use the position property in CSS to align your label and input in certain specified way as you want. By this, they get arranged in accordance to the parent element.

    form {
        position: relative;
        width: 70%;
        text-align: right;
    }
    
    label {
        width: 30%;
        position: absolute;
        left: 10%;
        text-align: right;
        margin-right: 30px;
    }
    
    input[type=submit] {
        position: absolute;
        left: 25%;
        background-color: #9AE5F8;
    }
    

    Here is the link to its jsfiddle.

    0 讨论(0)
  • 2020-11-29 08:05

    For what you are asking, I'd simply put them in a table.

      <form action="" method="post">
        <table>
            <tr>
                <td><label> Username </label></td>
                <td><input type="text"/></td>
            </tr>
            <tr>
                <td><label> Password </label></td>
                <td> <input type="password"/></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="submit"/></td>
            </tr>
        </table>
      </form>      
    

    Here's what it will look like http://jsfiddle.net/wCSAn/

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