Search code examples
cssformslabelalignment

How to align input fields among themselves without getting in trouble with label widths


I am trying to design a form so, that the input fields are among themselves. I tried it by giving the labels before the input-fields a width so that even if the label-text is shorter, space is the same. It worked in a youtube video, but I can't get it working for some reason. Whichever width I give it to the label, nothing changes. I guess it's a really simple error made by me, which gets me hardly frustrated.

body,
html {
  background-color: aquamarine;
  background-image: url(../background.jpg);
  height: 100%;
  overflow-y: hidden;
  margin: 0;
}

#Wrapper {
  margin-left: 15%;
  margin-right: 15%;
  height: 100vh;
}


/*
    ************************************************
    Hier beginnt das Header-Styling"
    ************************************************
    */

header {
  position: relative;
  background-color: blueviolet;
  margin: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex: 0 1 60%;
  padding: 0 1rem;
  border-radius: 0em 0em 1em 1em;
  height: 56px;
}

header img {
  width: 2.5em;
  height: 2.5em;
}

#branding {
  position: absolute;
  left: 2em;
  margin: 0;
  margin-left: 0.3em;
  font-family: 'Yanone Kaffeesatz', sans-serif;
  color: lawngreen;
}

.nav {
  display: flex;
  flex: 0 1 25%;
  justify-content: space-between;
  align-items: center;
}

.navitem {
  list-style: none;
}

.navitem a {
  font-size: 20px;
  margin-top: 0;
  color: black;
  text-decoration: none;
  font-family: 'Anton', sans-serif;
}

.navitem a {}

.navitem a:hover {
  color: rgba(0, 0, 0, 0.6);
  cursor: pointer;
}


/*
    ************************************************
    Header-Styling ENDE
    ************************************************
    */

main {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 80%;
  height: calc(100vh - 56px);
  background-color: white;
  margin: 0;
  margin-right: auto;
  margin-left: auto;
  overflow-y: auto;
  border: 1px solid Maroon;
  border-top: 0px;
}

main h2 {
  margin: 1em;
  font-family: 'Anton', sans-serif;
}

main h4 {
  font-size: 2em;
}


/*
    ************************************************
    Hier beginnt das Tabellen-Styling"
    ************************************************
    */

table {
  padding: 1em;
  background-color: lightgray;
}

table tr {}

table th {
  font-size: 1.5em;
}

table td {
  font-weight: 700;
  text-align: center;
  border-right: 1px solid gray;
  border-bottom: 1px solid gray;
  padding: 3em;
}

table td:last-child {
  border-right: 0px;
  padding: 0.3em;
}

td img {
  height: 11em;
  width: 11em;
}


/*
    ************************************************
    Tabellen-Styling ENDE
    ************************************************
    */


/*
    ************************************************
    Hier beginnt das Formular Styling
    ************************************************
    */

form {
  padding: 8em;
  background-color: slategray;
}

.forms label {
  width: 2em;
}
<link href="https://fonts.googleapis.com/css?family=Anton|Baloo+Bhaijaan|Gloria+Hallelujah|PT+Sans+Narrow|Righteous|Titillium+Web|Yanone+Kaffeesatz" rel="stylesheet">

<div id="Wrapper">

  <header>
    <img src="logo.png">
    <h1 id="branding">Einfache-Rezepte</h1>
    <ul class="nav">
      <li class="navitem active"><a href="index.html">Startseite</a></li>
      <li class="navitem"><a href="#">Rezepte</a></li>
      <li class="navitem"><a href="contact.html">Kontakt</a></li>
    </ul>
  </header>
  <main>
    <form action="mailto:Email" method="post">
      <h3>Kontaktiere uns!</h3>
      <div class="forms">
        <label for="vname">Vorname:</label>
        <input type="text" id="vname" name="Vorname">
      </div>
      <div class="forms">
        <label for="nname">Nachname:</label>
        <input type="text" id="nname" name="Nachname">
      </div>
      <div class="forms">
        <label for="msg">Ihre Nachricht:</label>
        <textarea name="nachricht" cols="40" rows="10"></textarea>
      </div>
      <input type="submit" value="Absenden">
      <input type="reset" value="Zurücksetzen">
    </form>
  </main>
</div>

CodePen: https://codepen.io/anon/pen/qJeQNe


Solution

  • You are missing display:inline-block; on label style. By default label elements are inline and to assign a width you need to change it to inline-block.

    Check the Snippet below:

    body,
    html {
      background-color: aquamarine;
      background-image: url(../background.jpg);
      height: 100%;
      overflow-y: hidden;
      margin: 0;
    }
    
    #Wrapper {
      margin-left: 15%;
      margin-right: 15%;
      height: 100vh;
    }
    
    
    /*
        ************************************************
        Hier beginnt das Header-Styling"
        ************************************************
        */
    
    header {
      position: relative;
      background-color: blueviolet;
      margin: 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex: 0 1 60%;
      padding: 0 1rem;
      border-radius: 0em 0em 1em 1em;
      height: 56px;
    }
    
    header img {
      width: 2.5em;
      height: 2.5em;
    }
    
    #branding {
      position: absolute;
      left: 2em;
      margin: 0;
      margin-left: 0.3em;
      font-family: 'Yanone Kaffeesatz', sans-serif;
      color: lawngreen;
    }
    
    .nav {
      display: flex;
      flex: 0 1 25%;
      justify-content: space-between;
      align-items: center;
    }
    
    .navitem {
      list-style: none;
    }
    
    .navitem a {
      font-size: 20px;
      margin-top: 0;
      color: black;
      text-decoration: none;
      font-family: 'Anton', sans-serif;
    }
    
    .navitem a {}
    
    .navitem a:hover {
      color: rgba(0, 0, 0, 0.6);
      cursor: pointer;
    }
    
    
    /*
        ************************************************
        Header-Styling ENDE
        ************************************************
        */
    
    main {
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 80%;
      height: calc(100vh - 56px);
      background-color: white;
      margin: 0;
      margin-right: auto;
      margin-left: auto;
      overflow-y: auto;
      border: 1px solid Maroon;
      border-top: 0px;
    }
    
    main h2 {
      margin: 1em;
      font-family: 'Anton', sans-serif;
    }
    
    main h4 {
      font-size: 2em;
    }
    
    
    /*
        ************************************************
        Hier beginnt das Tabellen-Styling"
        ************************************************
        */
    
    table {
      padding: 1em;
      background-color: lightgray;
    }
    
    table tr {}
    
    table th {
      font-size: 1.5em;
    }
    
    table td {
      font-weight: 700;
      text-align: center;
      border-right: 1px solid gray;
      border-bottom: 1px solid gray;
      padding: 3em;
    }
    
    table td:last-child {
      border-right: 0px;
      padding: 0.3em;
    }
    
    td img {
      height: 11em;
      width: 11em;
    }
    
    
    /*
        ************************************************
        Tabellen-Styling ENDE
        ************************************************
        */
    
    
    /*
        ************************************************
        Hier beginnt das Formular Styling
        ************************************************
        */
    
    form {
      padding: 8em;
      background-color: slategray;
    }
    
    .forms label {
      width: 5em;
      display:inline-block;
    }
    <link href="https://fonts.googleapis.com/css?family=Anton|Baloo+Bhaijaan|Gloria+Hallelujah|PT+Sans+Narrow|Righteous|Titillium+Web|Yanone+Kaffeesatz" rel="stylesheet">
    
    <div id="Wrapper">
    
      <header>
        <img src="logo.png">
        <h1 id="branding">Einfache-Rezepte</h1>
        <ul class="nav">
          <li class="navitem active"><a href="index.html">Startseite</a></li>
          <li class="navitem"><a href="#">Rezepte</a></li>
          <li class="navitem"><a href="contact.html">Kontakt</a></li>
        </ul>
      </header>
      <main>
        <form action="mailto:Email" method="post">
          <h3>Kontaktiere uns!</h3>
          <div class="forms">
            <label for="vname">Vorname:</label>
            <input type="text" id="vname" name="Vorname">
          </div>
          <div class="forms">
            <label for="nname">Nachname:</label>
            <input type="text" id="nname" name="Nachname">
          </div>
          <div class="forms">
            <label for="msg">Ihre Nachricht:</label>
            <textarea name="nachricht" cols="40" rows="10"></textarea>
          </div>
          <input type="submit" value="Absenden">
          <input type="reset" value="Zurücksetzen">
        </form>
      </main>
    </div>

    You can test it here also.