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

ifmq2ha2  于 2023-05-08  发布在  其他
关注(0)|答案(2)|浏览(138)

我需要保持中间的网格项为空。我认为这将有可能与以下属性: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>
xqnpmsa8

xqnpmsa81#

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

.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>
xoefb8l8

xoefb8l82#

.content-container {
  display: grid;
  grid-template-columns: auto auto;
  grid-gap: 10px;
  background-color: #2196F3;
  padding: 10px;
}

.content-container > div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}

.item-full{
    grid-column: 1/-1
}
<!DOCTYPE html>
<html>
<head>

</head>
<body>
<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 class="item-full">
    <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>

</body>
</html>

我有其他的解决方案,因为在这种情况下,我们可以选择精确字段可以被选择,并且该字段也有完整的大小。

<!DOCTYPE html>
<html>
<head>
<style>

.content-container {
  display: grid;
  grid-template-columns: auto auto;
  grid-gap: 10px;
  background-color: #2196F3;
  padding: 10px;
}

.content-container > div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}

.item-full{
    grid-column: 1/-1
}
</style>
</head>
<body>
<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 class="item-full">
    <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>

</body>
</html>

相关问题