如何在彼此之间对齐输入字段而不会遇到标签宽度的麻烦

时间:2018-11-01 13:52:10

标签: css forms label alignment

我正在尝试设计一种表单,以使输入字段相互之间。我尝试通过在输入字段之前给标签指定宽度来尝试这样做,即使标签文本较短,空格也一样。它在youtube视频中有效,但由于某种原因我无法使其正常工作。无论我给标签加上多少宽度,都不会改变。我想这是我犯的一个非常简单的错误,这几乎让我感到沮丧。

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

1 个答案:

答案 0 :(得分:0)

您在标签样式上缺少display:inline-block;。默认情况下,标签元素是内联的,要分配宽度,您需要将其更改为inline-block

检查以下代码段:

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>

您也可以here对其进行测试。