【问题标题】:How to skip one column using CSS grid?如何使用 CSS 网格跳过一列?
【发布时间】:2020-12-30 20:10:30
【问题描述】:

我需要保持中间网格项为空。我认为使用以下属性是可能的:grid-template-areas

grid-template-areas:
  "header . header"
  "main . ."
  "footer . footer";

但不幸的是,这并没有给出正确的显示。

所以我使用了 5 个名为 item-a 到 item-e 的类,然后在 css 中设置网格中的正确位置。然而,这并不是一种非常有效的方法。有谁知道如何提高效率?

.content-container {
    width: 51.5em;
    height: 30em;
    padding: 2.125em 1.5em;
    display: grid;
    grid-template-columns: 45% auto 45%;
    grid-template-rows: auto;
  }

  .item-a {
    grid-column: 1;
    grid-row: 1 / 3;
  }

  .item-b {
    grid-column: 3;
    grid-row: 1 / 3;
  }

  .item-c {
    grid-column: 1;
    grid-row: 2 / 3;
  }

  .item-d {
    grid-column: 1;
    grid-row: 3 / 3;
  }

  .item-e {
    grid-column: 3;
    grid-row: 3 / 3;
  }
<div class="content-container">
  <div class="item-a">
    <label for="firstName" class="label">First Name</label>
    <input id="firstName" type="text" class="input" placeholder="Enter your first name">
  </div>
  <div class="item-b">
    <label for="lastName" class="label">Last Name</label>
    <input id="lastName" type="text" class="input" placeholder="Enter your last name">
  </div>
  <div class="item-c">
    <label for="email" class="label">Email</label>
    <input id="email" type="email" class="input" placeholder="Enter your email">
  </div>
  <div class="item-d">
    <label for="password" class="label">Password</label>
    <input id="password" type="password" class="input" placeholder="Enter your password">
  </div>
  <div class="item-e">
    <label for="passwordConfirmation" class="label">Confirm Password</label>
    <input id="passwordConfirmation" type="password" class="input" placeholder="Enter your password again">
  </div>
</div>

【问题讨论】:

    标签: html css css-grid


    【解决方案1】:

    你不需要所有的代码,你可以简化如下:

    .content-container {
      max-width: 51.5em;
      height: 20em;
      padding: 2.125em 1.5em;
      display: grid;
      grid-template-columns: 1fr 1fr; /* define only 2 columns*/
      column-gap: 4em; /* control the gap between both columns*/
      border:1px solid;
    }
    
    .item-d {
      grid-column: 1; /* move the passwer to the first column instead of the second one*/
    }
    
    input {
      display: block;
      width:100%;
      box-sizing:border-box;
    }
    <div class="content-container">
      <div>
        <label for="firstName" class="label">First Name</label>
        <input id="firstName" type="text" class="input" placeholder="Enter your first name">
      </div>
      <div>
        <label for="lastName" class="label">Last Name</label>
        <input id="lastName" type="text" class="input" placeholder="Enter your last name">
      </div>
      <div>
        <label for="email" class="label">Email</label>
        <input id="email" type="email" class="input" placeholder="Enter your email">
      </div>
      <div class="item-d">
        <label for="password" class="label">Password</label>
        <input id="password" type="password" class="input" placeholder="Enter your password">
      </div>
      <div>
        <label for="passwordConfirmation" class="label">Confirm Password</label>
        <input id="passwordConfirmation" type="password" class="input" placeholder="Enter your password again">
      </div>
    </div>

    另一个简化:

    .content-container {
      max-width: 51.5em;
      height: 20em;
      padding: 2.125em 1.5em;
      display: grid;
      column-gap: 4em;
      border:1px solid;
    }
    
    .content-container :nth-child(2) {
      grid-column: 2;
      grid-row:span 2;
    }
    
    input {
      display: block;
      width:100%;
      box-sizing:border-box;
    }
    <div class="content-container">
      <div>
        <label for="firstName" class="label">First Name</label>
        <input id="firstName" type="text" class="input" placeholder="Enter your first name">
      </div>
      <div>
        <label for="lastName" class="label">Last Name</label>
        <input id="lastName" type="text" class="input" placeholder="Enter your last name">
      </div>
      <div>
        <label for="email" class="label">Email</label>
        <input id="email" type="email" class="input" placeholder="Enter your email">
      </div>
      <div >
        <label for="password" class="label">Password</label>
        <input id="password" type="password" class="input" placeholder="Enter your password">
      </div>
      <div>
        <label for="passwordConfirmation" class="label">Confirm Password</label>
        <input id="passwordConfirmation" type="password" class="input" placeholder="Enter your password again">
      </div>
    </div>

    【讨论】:

    • 还有没有办法使用grid-template-areas 属性来做到这一点?
    • @Ezrab_ 是的,但对于像这样的简单布局不需要。自动放置将为您完成 90% 的工作,您只需一条指令即可移动一个元素
    • 谢谢!但我试图通过以不同方式创建相同的网格来了解网格的工作原理。这就是为什么我想知道一个人会怎么做。
    • @Ezrab_ 总是首先让您的网格为您放置项目,然后在需要时进行调整。如果你一开始就把所有的元素都放在自己身上,你会迷失方向,而且效率不高,尤其是在布局简单的时候。
    猜你喜欢
    • 1970-01-01
    • 2020-10-05
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    相关资源
    最近更新 更多