gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:06:02 25 4
gpt4 key购买 nike

Design for the grid system
我需要将中间的网格项保持为空。我认为可以使用以下属性: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>

最佳答案

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

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

关于html - 如何使用CSS网格跳过一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62679082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com