Rea*_*lar 6 html css css3 css-tables css-grid
我需要使用网格布局,但也需要一条水平线来分隔每一行.
我唯一能找到的就是为每个单元格应用边框,但这只有在有足够的单元格填充每一行时才有效.
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
}
.box {
border-bottom: 2px solid #ffa94d;
padding: 1em;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
<div class="box">Four</div>
</div>Run Code Online (Sandbox Code Playgroud)
有没有办法解决上述问题,以便整行有边框?
添加一个grid-gap等于你的边框宽度然后考虑渐变来实现这一点:
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
grid-row-gap:2px;
background:
repeating-linear-gradient(to bottom,
transparent 0,
transparent 100px,
#ffa94d 100px,
#ffa94d 102px /*+2px here*/
);
}
.box {
padding: 1em;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
<div class="box">Four</div>
</div>Run Code Online (Sandbox Code Playgroud)
另一个想法是考虑添加到 1st,4th,7th .. (3n + 1)th 元素的伪元素:
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
overflow:hidden;
}
.box {
position:relative;
padding: 1em;
}
.box:nth-child(3n + 1)::after {
content:"";
position:absolute;
bottom:0px;
left:0;
width:100vw;
height:2px;
background:#ffa94d;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
<div class="box">Four</div>
</div>Run Code Online (Sandbox Code Playgroud)
小智 6
将您的表格想象成一组单元格(很像 Excel 电子表格)。您可以创建一个简单的单元格类,将其附加到每个网格项以操作单元格而不影响表格数据本身。考虑:
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
grid-row-gap: 20px;
}
.cell {
position: relative;
border-bottom: 2px solid #ffa94d;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
<!-- Here is your first row -->
<div class="cell">One</div>
<div class="cell">Two</div>
<div class="cell">Three</div>
<!-- Here is your second row -->
<div class="cell">Four</div>
<!-- You can extend the line by the number of cells per row -->
<div class="cell"></div>
<div class="cell"></div>
<!-- Organize your html into row groups to easily visualize them -->
<!-- This will produce a blank row with no line -->
<div></div>
<div>-- blank row --</div>
<div></div>
<!-- You can also control where the line begins and ends -->
<div class="box cell">First Column Only</div>
<div></div> <!-- No cells here.. We just want to underline the first column -->
<div></div>
<!-- 2nd and 3rd columns only -->
<div></div>
<div class="cell">Second Column</div>
<div class="cell">Third Column</div>
</div>Run Code Online (Sandbox Code Playgroud)
请注意,我只使用了网格行间距。如果您引入网格间隙或网格列间隙,您的线条将在列间隙处断开(在某些情况下这可能是所需的效果)。
确实,这是一种更复杂的控制分隔网格的水平线的方法,更少的“程序化”和更多的微观管理风格,但是,它确实提供了对将线引入网格的很好的控制。
其他答案也是不错的选择!我只是想提供我的两分钱。