CSS Grid 中每行后的边框

新手上路,请多包涵

我正在尝试在每一行之后使用内容居中对齐的 CSS 网格制作一个 border-bottom 。我无法理解它。

我想要 .line 填充整个 .wrapper 容器的宽度。

我怎样才能做到这一点?

这是代码:

 * {
  box-sizing: border-box;
}

.outer {
  width: 80%;
  margin: 0 auto;
}

.wrapper {
  border: 2px solid #f76707;
  border-radius: 5px;
  background-color: #fff4e6;
  display: grid;
  grid-template-columns: repeat(3, auto) max-content;
  justify-content: center;
}

.wrapper>div:not(.line) {
  border: 2px solid #ffa94d;
  border-radius: 5px;
  background-color: #ffd8a8;
  padding: 1em;
  color: #d9480f;
}

.line {
  grid-column-start: 1;
  grid-column-end: 6;
  height: 2px;
  border-bottom: 1px solid black;
  width: 100%;
}
 <div class="outer">
  <div class="wrapper">
    <div>1111111</div>
    <div>222</div>
    <div>3333333333</div>
    <div class="line"></div>
    <div>4444</div>
    <div>555555555</div>
    <div>99999999999</div>
  </div>
</div>

原文由 repo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 616
2 个回答

代替 justify-content 将内容居中,您可以在内容前后添加额外的列,两者都使用 1fr

然后将第一个 div 和 --- 放在 .line div 之后到第二列的开头。

小提琴

 * {
  box-sizing: border-box;
}

.outer {
  width: 80%;
  margin: 0 auto;
}

.wrapper {
  border: 2px solid #f76707;
  border-radius: 5px;
  background-color: #fff4e6;
  display: grid;
  grid-template-columns: 1fr repeat(3, auto) 1fr;
}

.wrapper>div:not(.line) {
  border: 2px solid #ffa94d;
  border-radius: 5px;
  background-color: #ffd8a8;
  padding: 1em;
  color: #d9480f;
}

.wrapper > div:first-of-type,
.line + div {
  grid-column: 2;
}

.line {
  grid-column: 1 / -1;
  height: 1px;
  background: black;
}
 <div class="outer">
  <div class="wrapper">
    <div>1111111</div>
    <div>222</div>
    <div>3333333333</div>
    <div class="line"></div>
    <div>4444</div>
    <div>555555555</div>
    <div>99999999999</div>
  </div>
</div>

原文由 sol 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是一个 响应 式解决方案,适用于 可变 数量的项目,并且不添加愚蠢的硬编码 div。基本上每个项目下面都有一行,复杂的部分是确定最后一行不能有一行的项目。该示例使用 flex-box(和 LESS),但这与此处无关,它也适用于网格。

 .grid {
    display: flex;
}
.grid-item{
    position: relative;
    .one-col{
        flex-basis: 100%/1;
        &:nth-last-child(1){
            &:after{ border-bottom: 0; }
        }
    }
    .two-cols{
        flex-basis: 100%/2;
        &:nth-last-child(1),
        &:nth-last-child(2):nth-child(odd){
            &:after{ border-bottom: 0; }
        }
    }
    .three-cols{
        flex-basis: 100%/3;
        &:nth-last-child(1),
        &:nth-last-child(2):nth-child(3n+1),
        &:nth-last-child(2):nth-child(3n+2),
        &:nth-last-child(3):nth-child(3n+1){
            &:after{ border-bottom: 0; }
        }
    }
    .four-cols{
        flex-basis: 100%/4;
        &:nth-last-child(1),
        &:nth-last-child(2):nth-child(4n+1),
        &:nth-last-child(2):nth-child(4n+2),
        &:nth-last-child(2):nth-child(4n+3),
        &:nth-last-child(3):nth-child(4n+1),
        &:nth-last-child(3):nth-child(4n+2),
        &:nth-last-child(4):nth-child(4n+1){
            &:after{ border-bottom: 0; }
        }
    }

    @media screen and (max-width: @screen__sm) {
        .grid-item .one-col;
    }
    @media screen and (min-width: @screen__sm) and (max-width: (@screen__md - 1)) {
        .grid-item .two-cols;
    }
    @media screen and (min-width: @screen__md) and (max-width: (@screen__lg - 1)) {
        .grid-item .three-cols;
    }
    @media screen and (min-width: @screen__lg) {
        .grid-item .four-cols;
    }

    &:after{
        content: "";
        display: block;
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        border-bottom: solid 1px black;
    }

原文由 br4nnigan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题