gpt4 book ai didi

html - 在 CSS Grid 中使网格项内的内容等于高度

转载 作者:行者123 更新时间:2023-11-28 10:29:20 26 4
gpt4 key购买 nike

我想弄清楚如何让我的网格的两列具有相同的高度。这是我所追求的:

xxxxxxxxxxxxxxxxx   xxxxxxxxxxxxxxxxxxx 
x qqqqqqqqqqqqq x   x qqqqqqqqqqqqqqq x
x qqqqqqqqqqqqq x   x qqqqqqqqqqqqqqq x
x x   x qqqqqqqqqqqqqqq x
x x   x qqqqqqqqqqqqqqq x
xxxxxxxxxxxxxxxxx   xxxxxxxxxxxxxxxxxxx

This is where the x's represent a border of a grid (display: grid) and the q's are labels and input fields.Here is all the code and a link to the same on codepen.

.container {
font-family: Arial, Helvetica, Sans-Serif;
color: #666;
font-size: 12px;
position: absolute;
width: 100%;
margin: 0 auto;
border: 1px solid #666;
background-color: #fff;
height: 100%;
}

.outerGrid {
display: grid;
grid-template-columns: 1fr 2fr 3fr 1fr;
grid-template-rows: 80px 1fr 1fr 30px;
grid-gap: 20px;
grid-template-areas:
"title title title title"
". companyInfo contactInfo . "
". demoInfo demoInfo . "
"footer footer footer footer";
}

.companyInfoContainer {
grid-area: companyInfo;
}

.companyInfoBox {
padding: 20px;
border: 1px solid #666;
}

.contactInfoContainer {
grid-area: contactInfo;
}

.contactInfoBox {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: auto;
grid-gap: 20px;
grid-template-areas: 'contactLeft contactRight';
border: 1px solid #666;
padding: 20px;
}

.titleRow {
display: flex;
-webkit-box-pack: justify;
justify-content: space-between;
align-content: flex-start;
margin-bottom: 10px;
color: #333;
font-size: 16px;
}

.formControl {
-webkit-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
display: block;
height: 37px;
padding: 5px;
line-height: 1.25;
background-color: #fff;
width: 100%;
background-clip: padding-box;
border: 1px solid rgba(0,0,0,.15);
border-radius: .25rem;
transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
margin-bottom: 5px;
background-image: none;
}

.formTitleTop {
margin-bottom: 10px;
}

.formTitle {
margin: 10px 0;
}

...和 ​​html:

<div class='container'>
<div class="outerGrid">
<div class="companyInfoContainer">
<div class="titleRow">
<div>Company Information</div>
<div>* Required</div>
</div>
<div class="companyInfoBox">
<div class="formTitleTop">Company Name*</div><input type="text" class="formControl" value="">
<div class="formTitle">Website (leave blank if not website)</div>
<input type="text" class="formControl" value=""></div>
</div>
<div class="contactInfoContainer">
<div class="titleRow">
<div>Contact Information</div>
<div class="required">* Required</div>
</div>
<div class="contactInfoBox">
<div class="contactLeft">
<div class="formTitleTop">First Name*</div><input type="text" class="formControl" value="">
<div class="formTitle">Last Name*</div><input type="text" class="formControl" value="">
<div class="formTitle">Role*</div><input type="text" class="formControl" value=""><input type="button" value="Add Additional Contacts"></div>
<div class="contactRight">
<div class="formTitleTop">Phone Number*</div><input type="text" class="formControl" value="">
<div class="formTitle">Mobile Phone Number</div><input type="text" class="formControl" value="">
<div class="formTitle">Email Address*</div><input type="text" class="formControl" value=""></div>
</div>
</div>
</div>
</div>

代码笔:https://codepen.io/GuitarJ/pen/QqZRYR

我似乎无法让左列拉伸(stretch)到右列高度的高度(我认为这也是行高。我希望 css 网格有办法做到这一点,但很难搜索这个确切的问题。

我也试过将高度设置为 100%,但它不符合网格间隙并且下降得太远。

如何让左边为行高?

最佳答案

那些网格项已经是相同的高度。它们都是第二行的高度,即 1fr,由网格容器上的 grid-template-rows 定义。

如果您在这两项(.companyInfoContainer.contactInfoContainer)周围加上边框,您就会明白我的意思。

revised demo

您似乎希望网格项(.companyInfoBox.contactInfoBox)的带边框子项占用所有可用高度。

由于网格项不是网格容器,you can't use grid properties on the children .

一个简单有效的解决方案是让网格元素成为 flex 容器。然后您可以将 flex 属性应用于子项,这可以使它们占用可用空间。

.companyInfoContainer {
display: flex;
flex-direction: column;
}

.companyInfoBox {
flex: 1;
}

.contactInfoContainer {
display: flex;
flex-direction: column;
}

.contactInfoBox {
flex: 1
}

revised demo

更多详情:Descendant element not responding to grid properties

关于html - 在 CSS Grid 中使网格项内的内容等于高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46737154/

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