如果有人能给我一些帮助,我在表格布局方面遇到了一些问题。我理解的是,如果我的容器是固定宽度的,那么在`grid-template-columns`中使用`fr`代替百分比宽度将仅考虑可用的自由空间,因此`grid-gap`不会导致网格溢出其容器,正如这个答案所描述的:
我想这个问题的部分原因在于输入元素有某种固定的最小宽度,但即使如此,将.field或inputs的宽度固定为适合的较小值也不是我想要的解决方法。
还有这里:解决方法是,尝试使用 fr 单位而不是百分比单位,它只适用于自由空间。这意味着 fr 长度是在应用任何 grid-gap 长度之后计算的。
但是我的代码似乎仍然导致网格溢出(我只关心水平方向): https://codepen.io/nwoodward/pen/bGLpBbPfr 单位仅适用于容器中的自由空间。
* {
margin: 0;
}
.container {
display: flex;
justify-content: center;
width: 100%;
height: 100vh;
background-color: firebrick;
}
.form {
display: flex;
flex-direction: column;
padding: 2rem;
margin: 2rem;
width: 25rem;
background-color: white;
}
.content {
border: 1px solid red;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 4rem;
width: 100%;
}
<div class="container">
<form class="form">
<div class="content">
<div class="field">
<label for="title" class="label">Title</label>
<input type="text" placeholder="Job Title" id="title" class="input">
<i class="icon icon--success"></i>
<i class="icon icon--fail">
</i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="company" class="label">Company</label>
<select name="company" id="company" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="location" class="label">Location</label>
<select name="location" id="location" class="input">
<!-- options added in js -->
</select>
<i class="icon"></i>
<i class="icon"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="wage" class="label">Wage</label>
<input type="text" placeholder="Wage" id="wage" class="input">
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="type" class="new-job__label">Type</label>
<select name="type" id="type" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="position" class="label">Position</label>
<select name="position" id="position" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="pqe" class="label">PQE</label>
<select name="pqe" id="pqe" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="featured" class="label">Featured</label>
<select name="featured" id="featured" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
</div>
<button class="new-job__submit">Submit</button>
</form>
</div>
grid-gap: 4rem
?这太多了,会导致溢出。 - Arman Ebrahimifixed/max-height
但没有添加适当的溢出规则而引起的。要么具有fixed
或max-height
的父元素只需要将overflow: auto
作为溢出规则,要么需要将属性更改为min-height
。 - tacoshy媒体查询
来将布局更改为单列之外,你没有太多可以做的了。 - tacoshy