Magnus Kolstad
Magnus Kolstad

Reputation: 547

Whitespace between borders above and below

I'm messing around with internal borders in CSS. I'm trying to copy this layout for an image grid on my website: enter image description here

I'm not able to recreate the way the horizontal and vertical borders don't touch each other, as highlighted in red here:

enter image description here

This is the closest I get:

enter image description here

(Copying NY Times's code isn't helpful here, as they have a different setup. The upper border is a bottom border of a different div. The bottom border is a top border of another div.)

I realize the reason the borders touch on one side, is because I'm using padding instead of margin. But what other options do I have?

My question is: How can I stop the borders from touching the horizontal line?

Excerpt of one card

/* @media (min-width: 765px) { */
  .cards {
    display: flex;
    flex-wrap: wrap;
    margin-top: 1.1rem;
  }

  .cards article {
    border-right: 1px solid #e2e2e2;
    box-sizing: content-box;
    flex-grow: 1;
    width: 28%;
    border-top: 1px solid #e2e2e2;
    padding: 1.1rem;
    padding-bottom: 0;
    margin-top: 1.2rem;
  }

  .cards article:nth-child(1),
  .cards article:nth-child(2),
  .cards article:nth-child(3) {
    border-top: none;
    padding-top: 0;
  }

  .cards article:nth-child(3n) {
    border-right: none;
    padding-right: 0;
  }

  /* TODO formulate expression */
  .cards article:nth-child(1),
  .cards article:nth-child(4),
  .cards article:nth-child(7),
  .cards article:nth-child(10),
  .cards article:nth-child(13),
  .cards article:nth-child(16),
  .cards article:nth-child(19) {
    padding-left: 0;
  }

  .cards-wrapper .page-title {
    margin-top: 3.2rem;
    margin-bottom: 0;
    font-size: 220%;
  }
/* } */
<article>
  <a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">
    <figure>
    </figure>
  </a>
  <p class="date"><time datetime="2022-03-18">18. mars 2022</time></p>
  <h2 itemprop="headline"><a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">Ullandhaug, tirsdag ettermiddag</a></h2>
  <p class="description">@ Acros</p>
</article>

Upvotes: 0

Views: 89

Answers (1)

James
James

Reputation: 631

With your current HTML structure, I'd use ::after pseudo element to achieve this.

.cards {
  display: flex;
  flex-wrap: wrap;
  margin-top: 1.1rem;
}

.cards article {
  position: relative;
  border-right: 1px solid #e2e2e2;
  box-sizing: content-box;
  flex-grow: 1;
  width: 28%;
  padding: 1.1rem;
  margin: 1.1rem 0;
}

.cards article::after {
  content: '';
  position: absolute;
  bottom: -1.1rem;
  left: 0;
  display: block;
  width: 101%;
  height: 1px;
  background: #e2e2e2;
}

.cards article:nth-child(1),
.cards article:nth-child(2),
.cards article:nth-child(3) {
  border-top: none;
  padding-top: 0;
}

.cards article:nth-child(3n) {
  border-right: none;
  padding-right: 0;
}


/* TODO formulate expression */

.cards article:nth-child(1),
.cards article:nth-child(4),
.cards article:nth-child(7),
.cards article:nth-child(10),
.cards article:nth-child(13),
.cards article:nth-child(16),
.cards article:nth-child(19) {
  padding-left: 0;
}

.cards-wrapper .page-title {
  margin-top: 3.2rem;
  margin-bottom: 0;
  font-size: 220%;
}
<div class="cards">
  <article>
    <a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">
      <figure>
      </figure>
    </a>
    <p class="date"><time datetime="2022-03-18">18. mars 2022</time></p>
    <h2 itemprop="headline"><a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">Ullandhaug, tirsdag ettermiddag</a></h2>
    <p class="description">@ Acros</p>
  </article>
  <article>
    <a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">
      <figure>
      </figure>
    </a>
    <p class="date"><time datetime="2022-03-18">18. mars 2022</time></p>
    <h2 itemprop="headline"><a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">Ullandhaug, tirsdag ettermiddag</a></h2>
    <p class="description">@ Acros</p>
  </article>
  <article>
    <a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">
      <figure>
      </figure>
    </a>
    <p class="date"><time datetime="2022-03-18">18. mars 2022</time></p>
    <h2 itemprop="headline"><a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">Ullandhaug, tirsdag ettermiddag</a></h2>
    <p class="description">@ Acros</p>
  </article>
  <article>
    <a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">
      <figure>
      </figure>
    </a>
    <p class="date"><time datetime="2022-03-18">18. mars 2022</time></p>
    <h2 itemprop="headline"><a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">Ullandhaug, tirsdag ettermiddag</a></h2>
    <p class="description">@ Acros</p>
  </article>
  <article>
    <a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">
      <figure>
      </figure>
    </a>
    <p class="date"><time datetime="2022-03-18">18. mars 2022</time></p>
    <h2 itemprop="headline"><a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">Ullandhaug, tirsdag ettermiddag</a></h2>
    <p class="description">@ Acros</p>
  </article>
  <article>
    <a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">
      <figure>
      </figure>
    </a>
    <p class="date"><time datetime="2022-03-18">18. mars 2022</time></p>
    <h2 itemprop="headline"><a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">Ullandhaug, tirsdag ettermiddag</a></h2>
    <p class="description">@ Acros</p>
  </article>
  <article>
    <a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">
      <figure>
      </figure>
    </a>
    <p class="date"><time datetime="2022-03-18">18. mars 2022</time></p>
    <h2 itemprop="headline"><a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">Ullandhaug, tirsdag ettermiddag</a></h2>
    <p class="description">@ Acros</p>
  </article>
  <article>
    <a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">
      <figure>
      </figure>
    </a>
    <p class="date"><time datetime="2022-03-18">18. mars 2022</time></p>
    <h2 itemprop="headline"><a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">Ullandhaug, tirsdag ettermiddag</a></h2>
    <p class="description">@ Acros</p>
  </article>
  <article>
    <a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">
      <figure>
      </figure>
    </a>
    <p class="date"><time datetime="2022-03-18">18. mars 2022</time></p>
    <h2 itemprop="headline"><a itemprop="url" href="/foto/ullandhaug-tirsdag-ettermiddag/">Ullandhaug, tirsdag ettermiddag</a></h2>
    <p class="description">@ Acros</p>
  </article>
</div>

Upvotes: 1

Related Questions