cssgridoverflowcss-gridword-wrap

Having trouble wrapping text in a grid on the 3rd text line versus first


this is my first time using a grid layout. I'm having a bit of trouble figuring out how to wrap text on the 3rd line versus how it appears now on the first.

This is how it looks now:

Current Grid - Image

I'd love to achieve this result:

Desired Grid - Image

Here's the current HTML:

<ul class="grid">
    <li class="grid__item">
        <h3>Week 1</h3>
        <h4><a href="#">Course Welcome and Orientation</a></h4>
    </li>
    <li class="grid__item">
        <h3>Week 2</h3>
        <h4><a href="#">Landscapes of Early Cultures</a></h4>
    </li>
    <li class="grid__item">
        <h3>Week 3</h3>
        <h4><a href="#">The Enlightened Landscape: Western Europe in the 15th and 16th Centuries</a></h4>
    </li>
</ul>

Here's the accompanying CSS:

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  grid-gap: 10px;
  padding: 0px 0 50px 0;
  justify-content: start;
  margin: 0;
  list-style: none;
      }

.grid__item {
  background: #2774ae;
  border: 1px solid #ccc;
  padding: 10px 0 10px 0;
  margin: 0 !important;
  border-radius: 5px;
  position: relative;
  transition: all 0.2s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
  min-height: 190px;
  
  &:hover,
  &:focus-within {
    background-color: #005587 !important;
  }

  a {
    position: relative;
    z-index: 1;
    display: inline-block;
   color: #ffffff !important;
   text-decoration: none !important;
   text-decoration-skip-ink: auto;
  }


  h4 {
    font-size: 1.05rem !important;
    font-weight: 700;
    letter-spacing: 1px;
    color: #ffffff !important;
    margin: 1rem 1.5rem !important;
   text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  display: -webkit-box;
-webkit-line-clamp: 3;
  max-width: 190px;
-webkit-box-orient: vertical;
  


}

h4 a {
    position: static;
    display: inline-block !important;
    color: #ffffff !important;
    text-decoration: none !important;
    text-decoration-skip-ink: auto;
}

h4 a:hover,
h4 a:focus {
    color: #ffffff !important;
    background-color: transparent !important;
}

h4 a:before {
    content: "";
    display: block;
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
transition: background-color 0.1s ease-out;
    background-color: transparent; }

  
  h3 {
  font-weight: 500;
    font-size: 1.05rem !important;
    line-height: 1;
    color: #ffffff !important;
    text-align: left !important;
    margin: 1.5rem 1.5rem 0.25rem 1.5rem !important;
}
  
  h3:after {
  display: block;
  content: '';
  width: 55px;
  height: 4px;
  background: #FFD100;
  margin: 1rem 0 0.7rem 0;
  border-radius: 99px;
}


  ul {
    display: flex;
    margin: 0 0 6px 0 !important;
}

ul li {
    margin-right: 10px !important;
}

}

I've exhausted so many options. Unfortunately, I just can't figure out how to make it work.


Solution

  • Remove the white-space: nowrap css attribute from your .h4 class. Also remove display: inline-block from a couple of spots, and change min-height to height for .grid__item.

    .grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
      grid-gap: 10px;
      padding: 0px 0 50px 0;
      justify-content: start;
      margin: 0;
      list-style: none;
          }
    
    .grid__item {
      background: #2774ae;
      border: 1px solid #ccc;
      padding: 10px 0 10px 0;
      margin: 0 !important;
      border-radius: 5px;
      position: relative;
      transition: all 0.2s ease;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
      height: 190px; /* change this to height */
      
      &:hover,
      &:focus-within {
        background-color: #005587 !important;
      }
    
      a {
        position: relative;
        z-index: 1;
       /* display: inline-block;*/
       color: #ffffff !important;
       text-decoration: none !important;
       text-decoration-skip-ink: auto;
      }
    
    
      h4 {
        font-size: 1.05rem !important;
        font-weight: 700;
        letter-spacing: 1px;
        color: #ffffff !important;
        margin: 1rem 1.5rem !important;
       text-overflow: ellipsis;
        overflow: hidden;
     /*   white-space: nowrap;*/
      display: -webkit-box;
    -webkit-line-clamp: 3;
      max-width: 190px;
    -webkit-box-orient: vertical;
      
    
    
    }
    
    h4 a {
        position: static;
     /*   display: inline-block !important;*/
        color: #ffffff !important;
        text-decoration: none !important;
        text-decoration-skip-ink: auto;
    }
    
    h4 a:hover,
    h4 a:focus {
        color: #ffffff !important;
        background-color: transparent !important;
    }
    
    h4 a:before {
        content: "";
        display: block;
        position: absolute;
        z-index: 0;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
    transition: background-color 0.1s ease-out;
        background-color: transparent; }
    
      
      h3 {
      font-weight: 500;
        font-size: 1.05rem !important;
        line-height: 1;
        color: #ffffff !important;
        text-align: left !important;
        margin: 1.5rem 1.5rem 0.25rem 1.5rem !important;
    }
      
      h3:after {
      display: block;
      content: '';
      width: 55px;
      height: 4px;
      background: #FFD100;
      margin: 1rem 0 0.7rem 0;
      border-radius: 99px;
    }
    
    
      ul {
        display: flex;
        margin: 0 0 6px 0 !important;
    }
    
    ul li {
        margin-right: 10px !important;
    }
    
    }