大家好,这是我目前拥有的图片
我有一个 3 行 3 列的网格(每列是最小内容) 我将每个单元格中的元素居中。 看起来不错,但如果我的标题变得很大,开关和类别之间的空间就会变得太大:
现在我尝试在 div 容器内制作中间行并使用 flex,但元素不会居中,因为一个类别的大小比另一个类别的大小大。
我还尝试使类别大小相同,但是当我将整个组件集中在页面上的某个位置时,由于较小类别的空白,它会过于靠右。
知道如何使该空间变得更小,但将开关保持在标题和按钮的正中间吗?
代码如下:
HTML:
<div class="category-switch">
<div class="form-check category-switch__btn">
<input class="form-check-input" type="checkbox" />
<label class="form-check-label form-label">Disable</label>
</div>
<div class="category-switch__switch">
<div class="switch">
<div class="form-check">
<input class="form-check-input" type="checkbox" />
<label class="form-check-label form-label"></label>
</div>
<span class="switch__slider switch__round"></span>
</div>
</div>
<span class="category-switch__category category-switch__category--1">Male</span>
<span class="category-switch__category category-switch__category--2">Female</span>
<span class="category-switch__title">gender</span>
</div>
以及 scss 代码:
.category-switch {
// The grid to place the items
display: grid;
grid-template-columns: repeat(3, min-content);
grid-template-rows: repeat(3, min-content);
gap: 0.8rem;
place-items: center;
justify-items: center;
// Makes the component be the width of its content
width: max-content;
// Title
&__title {
grid-row: 1/2;
grid-column: 2/3;
font-weight: bold;
text-transform: capitalize;
}
// Placing category text
&__category {
&--1 {
grid-row: 2/3;
grid-column: 1/2;
}
&--2 {
grid-row: 2/3;
grid-column: 3/4;
}
}
// The switch position
&__switch {
grid-row: 2/3;
grid-column: 2/3;
}
// The button css
&__btn {
grid-row: 3/4;
grid-column: 2/3;
display: grid;
place-items: center;
position: relative;
width: 7rem;
height: 2.8rem;
background: #d02b2b;
border-radius: 0.5rem;
}
}
尝试使代码尽可能少,并删除了一些与定位scss无关的内容。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
在编辑之前我已经写了这个...... 如果类名不同,很抱歉。 这只是一个模板。
#mainContainer{ display:grid; grid-template-columns: 100px fit-content(10%) 100px; column-gap: 20px; grid-row-gap: 20px; /* adjust your template here */ } #mainContainer div{ display:grid; align-content: center; justify-content: center; border:1px solid #000000; } .top{ font-size:1.6em; font-weight: bold; grid-column-start: 1; grid-column-end: 4; grid-row-start: 1; grid-row-end: 1; } .left{ font-size:1.6em; font-weight: bold; } .right{ font-size:1.6em; font-weight: bold; } .bottom{ background-color: orangered; border-radius: 10px; height:30px; }希望对你有帮助。 祝你有美好的一天。