Search code examples
csscss-grid

CSS grid row consistent height


I'm trying to set up a simple CSS grid. First row is two columns, one with the title and a second with an image. Then second row has all the content spanning both columns.

Problem I have is that the title and image have different heights (row 1: the border-bottom is not inline).

Since the image is set to a small height, I'm not sure why row 1 col 2 is so big. Any advice to get it to the same height as row 1 col 1 using just CSS (I have limited ability to adjust the html).

https://jsfiddle.net/05jsnmvz/

Snippet:

@import url('https://fonts.googleapis.com/css2?family=Raleway:wght@400;900&family=Source+Sans+Pro&display=swap');
:root {
  --title: #3c3b3b;
  --secondarytext: #5b5b66;
  --maintext: #646464;
  --cardbg: #fff;
  --pagebg: #f4f4f4;
  --borders: 1px solid #e6e6e6;
}

*,
::before,
::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  scroll-behavior: smooth;
}

html {
  font-family: 'Source Sans Pro';
  font-size: 12pt;
  line-height: 1.5;
  background: var(--pagebg);
}

body {
  width: 70%;
  width: 120ch;
  margin: 5% auto;
  border: var(--borders);
  background: var(--cardbg);
  color: var(--maintext);
}

#content {
  display: grid;
  grid-template-columns: 3fr 1fr;
}

.title {
  grid-row: 1;
  grid-column: 1;
  font-family: 'Raleway';
  text-transform: uppercase;
  letter-spacing: 0.5rem;
  font-size: 150%;
  font-weight: bold;
  text-align: left;
  color: var(--title);
  padding: 3rem 3rem;
  padding-top: 4rem;
  margin-bottom: 3rem;
  border-bottom: var(--borders);
}

.title .subtitle {
  font-size: 50%;
  letter-spacing: 0.2rem;
  color: var(--secondarytext);
  font-weight: normal;
}

#thumbnail {
  grid-row: 1;
  grid-column: 2;
  border-bottom: var(--borders);
  margin: 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#thumbnail img {
  max-height: 4rem;
  max-width: 4rem;
}

.article {
  grid-column: 1 / span 2;
  padding: 0rem 3rem;
}

p {
  margin-bottom: 1.5rem;
}
<div id="content" class="content">
  <h1 class="title">My Title
    <br>
    <span class="subtitle">April 2023</span>
  </h1>
  <div id="thumbnail">
    <img src="https://placekitten.com/g/200/200">
  </div>

  <div class="article">
    <p>
      Main content here.
    </p>
  </div>
</div>


Solution

  • Simply add margin-bottom: 48px; to the #thumbnail like you did for the .title.

    See the snippet below.

    @import url('https://fonts.googleapis.com/css2?family=Raleway:wght@400;900&family=Source+Sans+Pro&display=swap');
    :root {
      --title: #3c3b3b;
      --secondarytext: #5b5b66;
      --maintext: #646464;
      --cardbg: #fff;
      --pagebg: #f4f4f4;
      --borders: 1px solid #e6e6e6;
    }
    
    *,
    ::before,
    ::after {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
      scroll-behavior: smooth;
    }
    
    html {
      font-family: 'Source Sans Pro';
      font-size: 12pt;
      line-height: 1.5;
      background: var(--pagebg);
    }
    
    body {
      width: 70%;
      width: 120ch;
      margin: 5% auto;
      border: var(--borders);
      background: var(--cardbg);
      color: var(--maintext);
    }
    
    #content {
      display: grid;
      grid-template-columns: 3fr 1fr;
    }
    
    .title {
      grid-row: 1;
      grid-column: 1;
      font-family: 'Raleway';
      text-transform: uppercase;
      letter-spacing: 0.5rem;
      font-size: 150%;
      font-weight: bold;
      text-align: left;
      color: var(--title);
      padding: 3rem 3rem;
      padding-top: 4rem;
      margin-bottom: 3rem;
      border-bottom: var(--borders);
    }
    
    .title .subtitle {
      font-size: 50%;
      letter-spacing: 0.2rem;
      color: var(--secondarytext);
      font-weight: normal;
    }
    
    #thumbnail {
      grid-row: 1;
      grid-column: 2;
      border-bottom: var(--borders);
      margin: 0;
      padding: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 48px; /* Added */
    }
    
    #thumbnail img {
      max-height: 4rem;
      max-width: 4rem;
    }
    
    .article {
      grid-column: 1 / span 2;
      padding: 0rem 3rem;
    }
    
    p {
      margin-bottom: 1.5rem;
    }
    <html>
    
    <body>
    
      <div id="content" class="content">
        <h1 class="title">My Title
          <br>
          <span class="subtitle">April 2023</span>
        </h1>
        <div id="thumbnail">
          <img src="https://placekitten.com/g/200/200">
        </div>
    
        <div class="article">
          <p>
            Main content here.
          </p>
        </div>
      </div>
    
    </body>
    
    </html>