【问题标题】:How to align input fields among themselves without getting in trouble with label widths [duplicate]如何在不遇到标签宽度问题的情况下对齐输入字段[重复]
【发布时间】:2018-11-01 13:52:10
【问题描述】:

我正在尝试设计一个表单,以便输入字段相互之间。我通过给输入字段之前的标签一个宽度来尝试它,这样即使标签文本更短,空间也是相同的。它在 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>

代码笔:https://codepen.io/anon/pen/qJeQNe

【问题讨论】:

    标签: css forms label alignment


    【解决方案1】:

    您在标签样式上缺少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 测试它。

    【讨论】:

    • 哦,伙计,你是对的..这是一个简单的缺失:)谢谢!
    • 总是使用 inline-block 让对象相互对齐
    猜你喜欢
    • 2012-01-01
    • 2012-03-05
    • 2015-09-22
    • 2019-02-26
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多