CSS网格中每行之后的边框

rep*_*epo 4 html css grid css3 css-grid

我正在尝试border-bottom使用CSS网格在每一行之后使内容对齐中心。我无法解决这个问题。

我想.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%;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

sol*_*sol 5

除了justify-content将内容居中,您还可以在内容之前和之后添加其他列1fr

然后将第一个divdiv之后的位置.line放置在第二列的起始位置。

小提琴

* {
  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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

  • 哇,真是个了不起的人。我是用 &lt;table&gt; 从两边推送内容来做到的,但我觉得不使用 css grid 有点内疚;) 谢谢伙计! (2认同)