html - 跨整行 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;
}
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
<div class="box">Four</div>
</div>
有没有办法解决上述问题,使整行都有边框?
解决方案
添加grid-gap
等于边框宽度的a,然后考虑渐变来实现这一点:
.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;
}
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
<div class="box">Four</div>
</div>
另一个想法是考虑添加到第 1、4、7 ..(3n + 1)
个元素的伪元素:
.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;
}
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
<div class="box">Four</div>
</div>
推荐阅读
- firebase - Is Firebase free version (spark) is sufficinet for project with almost 1000 active users per month?
- c++ - 创建直方图以显示每个类别/箱中有多少项目,并带有星号
- visual-c++ - 在 Windows 上什么情况下非增量链接会导致抛出异常,而增量链接不会?
- php - 传递数组后尝试获取非对象的属性
- javascript - 在提交按钮上单击,执行函数然后提交表单 react.js
- swift - onYouTubeIframeAPIReady 在 iOS 应用程序的 WKWebView 中不会触发
- python - 检查是否存在重复,然后在行中附加一个唯一的数字?
- .net - CLR 是否在 JIT 编译期间进行类似数据库的查询?
- r - 在 R Shiny 应用程序 UI 出现之前让服务员功能运行
- excel-formula - 在 Excel 中使用多项式回归