CSS Grid中每行之后的边框

时间:2018-06-08 22:42:58

标签: html css css3 grid 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>

3 个答案:

答案 0 :(得分:5)

您可以使用justify-content在内容之前和之后添加其他列,而不是1fr将内容置于中心位置。

然后将div之后的div.line置于第二列的开头。

Fiddle

* {
  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>

答案 1 :(得分:1)

我使用nth-of-type并将该行切换为其他类型(<span>)取得了一些成功。

我还为行跨越添加了第一和第六列,
而其他项只占据第2-5列。

&#13;
&#13;
* {
  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;
  justify-content: center;
}

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

.wrapper>div:nth-of-type(3n+1) {
  grid-column: 2;
}

.line {
  grid-column: 1/6;
  height: 2px;
  border-bottom: 1px solid black;
}
&#13;
<div class="outer">
  <div class="wrapper">
    <div>1111111</div>
    <div>222</div>
    <div>3333333333</div>
    <span class="line"></span>
    <div>4444</div>
    <div>555555555</div>
    <div>6666666</div>
    <span class="line"></span>
    <div>77777</div>
    <div>888888888</div>
    <div>99</div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

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

.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;
    }