如何使用CSS网格跳过一列?

Ezr*_*ab_ 5 html css css-grid

网格系统设计

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

grid-template-areas:
  "header . header"
  "main . ."
  "footer . footer";
Run Code Online (Sandbox Code Playgroud)

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

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

grid-template-areas:
  "header . header"
  "main . ."
  "footer . footer";
Run Code Online (Sandbox Code Playgroud)
.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;
  }
Run Code Online (Sandbox Code Playgroud)

Tem*_*fif 14

您不需要所有这些代码,您可以像下面这样简化:

.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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

另一个简化:

.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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

  • @Ezrab_ 总是先让你的网格为你放置项目,然后在需要时进行调整。如果你一开始就自己放置所有元素,你会迷失方向,而且效率不高,尤其是当布局很简单时。 (2认同)