【问题标题】:Aligning form labels in a CSS grid?在 CSS 网格中对齐表单标签?
【发布时间】:2019-11-18 00:33:20
【问题描述】:

我正在学习使用带有表格的网格,并坚持让我的表格在其中正常工作。大部分都可以,直到我进入复选框,标签不对齐。我希望那些标签位于复选框的右侧,这与左侧的所有其他标签不同。相反,标签会被撞到下一行。

这是我的代码:

@import url("https://fonts.googleapis.com/css?family=Lato:400,400i,700");
body {
  font-family: "Lato", sans-serif;
  color: #fafafa;
}

form {
  display: grid;
  grid-template-columns: 1fr 1em 2fr;
  border-radius: 12px;
  padding: 1em;
  background: #009688;
  margin: 2rem auto 0 auto;
  max-width: 600px;
}

form input {
  background: #fff;
  border: 1px solid #9c9c9c;
}

form button {
  background: lightgrey;
  padding: 0.7em;
  width: 100%;
  border: 0;
}

form button:hover {
  background: gold;
}

label {
  padding: 0.5em 0.5em 0.5em 0;
}

input {
  padding: 0.7em;
  margin-bottom: 0.5rem;
}

input:focus {
  outline: 3px solid gold;
}

@media (min-width: 400px) {
  form {
    grid-gap: 16px;
  }
  label {
    text-align: right;
    grid-column: 1 / 3;
  }
  input[type="checkbox"] {
    grid-column: 3 / 3;
    justify-self: start;
    margin: 0;
  }
  input,
  button {
    grid-column: 3 / 4;
  }
  textarea+label {
    align-self: start;
  }
}
<form class="form1" action="">
  <label for="firstName" class="first-name">First Name</label>
  <input id="firstName" type="text">

  <label for="lastName" class="last-name">Last Name</label>
  <input id="lastName" type="text">

  <label for="email">Email</label>
  <input id="email" type="text">
  <label for="experience">Experience</label>
  <select id="experience" name="experience">
    <option value="1">Less than a year</option>
    <option value="2">1 - 2 years</option>
    <option value="3">3 - 5 years</option>
    <option value="5">5 years or more</option>
  </select>

  <input id="bootcamp" name="bootcamp" type="checkbox" />
  <label for="bootcamp">Bootcamp</label>

  <input id="tech" name="tech" type="checkbox" />
  <label for="tech">Tech School</label>

  <input id="college" name="college" type="checkbox" />
  <label for="college">College</label>

  <textarea id="comments" name="comments" rows="5" cols="20"></textarea>
  <label for="comments">Comments</label>

  <button>Submit</button>
</form>

【问题讨论】:

    标签: html css css-grid


    【解决方案1】:

    调整您的 HTML 代码以始终保持结构(标签然后输入)。您还可以通过仅包含 2 列来简化网格并考虑间隙:

    @import url("https://fonts.googleapis.com/css?family=Lato:400,400i,700");
    body {
      font-family: "Lato", sans-serif;
      color: #fafafa;
    }
    
    form {
      display: grid;
      grid-template-columns: 1fr 2fr;
      grid-gap: 1em;
      border-radius: 12px;
      padding: 1em;
      background: #009688;
      margin: 2rem auto 0 auto;
      max-width: 600px;
      align-items: center;
    }
    
    form input {
      background: #fff;
      border: 1px solid #9c9c9c;
    }
    
    form button {
      background: lightgrey;
      padding: 0.7em;
      width: 100%;
      border: 0;
    }
    
    form button:hover {
      background: gold;
    }
    
    label {
      padding: 0.5em 0.5em 0.5em 0;
    }
    
    input {
      padding: 0.7em;
      margin-bottom: 0.5rem;
    }
    
    input:focus {
      outline: 3px solid gold;
    }
    
    select {
      height: 100%;
    }
    
    @media (min-width: 400px) {
      form {
        grid-gap: 16px;
      }
      label {
        text-align: right;
        grid-column: 1;
      }
      input[type="checkbox"] {
        justify-self: start;
        margin: 0;
      }
      input,
      button {
        grid-column: 2;
      }
      textarea+label {
        align-self: start;
      }
    }
    <form class="form1" action="">
      <label for="firstName" class="first-name">First Name</label>
      <input id="firstName" type="text">
    
      <label for="lastName" class="last-name">Last Name</label>
      <input id="lastName" type="text">
    
      <label for="email">Email</label>
      <input id="email" type="text">
      <label for="experience">Experience</label>
      <select id="experience" name="experience">
        <option value="1">Less than a year</option>
        <option value="2">1 - 2 years</option>
        <option value="3">3 - 5 years</option>
        <option value="5">5 years or more</option>
      </select>
    
      <label for="bootcamp">Bootcamp</label>
      <input id="bootcamp" name="bootcamp" type="checkbox">
    
      <label for="tech">Tech School</label>
      <input id="tech" name="tech" type="checkbox">
    
    
      <label for="college">College</label>
      <input id="college" name="college" type="checkbox">
    
      <label for="comments">Comments</label>
      <textarea id="comments" name="comments" rows="5" cols="20"></textarea>
    
      <button>Submit</button>
    </form>

    【讨论】:

      【解决方案2】:

      HTML:

      <div>
        <input id="bootcamp" name="bootcamp" type="checkbox" />
        <label for="bootcamp">Bootcamp</label></br>
        <input id="tech" name="tech" type="checkbox" />
        <label for="tech">Tech School</label></br>
        <input id="college" name="college" type="checkbox" />
        <label for="college"> College</label>
      </div>
      
        <label for="comments">Comments</label>
        <textarea id="comments" name="comments" rows="5" cols="20"></textarea>
      

      并将其添加到您的 CSS 中

          div{
           grid-column: 3 /4;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-11
        • 1970-01-01
        相关资源
        最近更新 更多