Could really use a hand getting this card grid to be more responsive

Recently, I delved into using HTML & CSS for work.

However, I encountered a major hurdle: creating responsive Grid card elements.

I have four card elements in a column, each with text that increases opacity when hovering over the card. When viewed on a tablet, it should only display 3 cards in a column (2 cards for phones) and resize the text accordingly. I attempted to utilize my usual media queries but they didn't do the trick. The cards wouldn't transition to the next column upon resizing the page.

I scoured Google for examples but couldn't locate anything useful. So, I'm reaching out to see if any of you can assist me.

Best regards :)

My Code:

figure {
  border-radius: 1rem;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  margin: 10px;
}

figure>* {
  grid-area: 1/1;
  transition: .4s;
}

figure img {
  width: 100%;
  height: auto;
}

figure:hover figcaption {
  --_i: 0%;
  background-color: #ed161f;
}

figure:hover img {
  transform: scale(1.2);
}

@supports not (-webkit-mask-clip: text) {
  figure figcaption {
    -webkit-mask: none;
    color: #fff;
  }
}

.hover-content {
  position: absolute;
  top: 1rem;
  left: 1rem;
  right: 1rem;
  bottom: 1rem;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  opacity: 0;
  transition: opacity 0.4s;
  background-color: #ed161f;
}

figure:hover .hover-content {
  opacity: 1;
}

.hover-content p {
  color: #fff;
  font-family: 'Roboto', sans-serif;
  font-size: 1.25rem;
  margin: 0;
  padding-top: 20px;
}

.card-grid {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: auto auto auto auto;
  grid-auto-flow: column;
  place-content: center;
  background: #425a52;
}

@media only screen and (max-width: 1000px) {
              .card-grid {
                    display: grid;
                    grid-template-columns: auto auto auto;
              }
        }

        @media only screen and (max-width: 800px) {
              .card-grid {
                    display: grid;
                    grid-template-columns: auto auto;
              }
        }
<div class="card-grid">
  <figure>
    <img src="/img/hans_jorg.jpg" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          ... has been a municipal council since 1997, city council since 1999, deputy mayor from 2004-2014, mayor since 2014, Federal Board member of the Austrian Municipal Association, state board member of the Salzburg Municipal Association, state chair of municipal representatives
        </p>
        <p>
          Areas of expertise include Finance, Construction, Urban Planning, Social Affairs, Economics, City Marketing
        </p>
      </div>
    </figcaption>
  </figure>

  <figure>
    <img src="/img/hans_jorg.jpg" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          ... has been a municipal council since 1997, city council since 1999, deputy mayor from 2004-2014, mayor since 2014, Federal Board member of the Austrian Municipal Association, state board member of the Salzburg Municipal Association, state chair of municipal representatives
        </p>
        <p>
          Areas of expertise include Finance, Construction, Urban Planning, Social Affairs, Economics, City Marketing
        </p>
      </div>
    </figcaption>
  </figure>

  <figure>
    <img src="/img/hans_jorg.jpg" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          ... has been a municipal council since 1997, city council since 1999, deputy mayor from 2004-2014, mayor since 2014, Federal Board member of the Austrian Municipal Association, state board member of the Salzburg Municipal Association, state chair of municipal representatives
        </p>
        <p>
          Areas of expertise include Finance, Construction,Urban Planning, Social affairs, economics, City Marketing
        </p>
      </div>
    </figcaption>
  </figure>

  <figure>
    <img src="/img/hans_jorg.jpg" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          ... has been a municipal council since 1997, city council since 1999, deputy mayor from 2004-2014, mayor since 2014, Federal Board member of the Austrian Municipal Association, state board member of the Salzburg Municipal Association, state chair of municipal representatives
        </p>
        <p>
          Areas of expertise include Finance, Construction, Urban Planning, Social affairs, economics, City Marketing
        </p>
      </div>
    </figcaption>
  </figure>
</div>

Answer №1

Ensure your columns are wrapped by using CSS grid-template-columns with a value of repeat(auto-fit, 200px);. Remember to exclude grid-auto-flow: column;

* { margin: 0; box-sizing: border-box; }

.card-grid {
  min-height: 100dvh;
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(auto-fit, 140px);
  place-content: center;
  background: #425a52;

  & figure {
    border-radius: 1rem;
    overflow: hidden;
    cursor: pointer;
    position: relative;

    & >* {
      grid-area: 1/1;
      transition: .4s;
    }

    & img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    &:hover figcaption {
      --_i: 0%;
      background-color: #ed161f;
    }

    &:hover .hover-content {
      opacity: 1;
    }

    &:hover img {
      transform: scale(1.2);
    }
  }

  .hover-content {
    position: absolute;
    top: 1rem;
    left: 1rem;
    right: 1rem;
    bottom: 1rem;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    opacity: 0;
    transition: opacity 0.4s;
    background-color: #ed161f;


    & p {
      color: #fff;
      font-family: 'Roboto', sans-serif;
      font-size: 1.25rem;
      margin: 0;
      padding-top: 20px;
    }
  }
}

@supports not (-webkit-mask-clip: text) {
  figure figcaption {
    -webkit-mask: none;
    color: #fff;
  }
}
<div class="card-grid">
  <figure>
    <img src="https://picsum.photos/id/100/300/300" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          Serving since 1997 as Community Representative, City Councilor since 1999, Deputy Mayor from 2004-2014, Mayor since 2014, Federal Board Member Austrian Municipal Association, State Board Salzburg Municipal Association, State Chairman Community Representatives Association
        </p>
        <p>
          Areas of Expertise<br> Finance, Construction, Spatial Planning, Social Affairs, Economy, City Marketing
        </p>
      </div>
    </figcaption>
  </figure>

  <figure>
    <img src="https://picsum.photos/id/101/300/300" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          Since 1997 Community Representative, City Councilor since 1999, Deputy Mayor 2004-2014, Mayor since 2014, Federal Board Member Austrian Municipal Association, State Board Salzburg Municipal Association, State Chairman Community Representatives Association
        </p>
        <p>
          Areas of Expertise<br> Finance, Construction, Spatial Planning, Social Affairs, Economy, City Marketing
        </p>
      </div>
    </figcaption>
  </figure>

  <figure>
    <img src="https://picsum.photos/id/102/300/300" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          Since 1997 Community Representative, City Councilor since 1999, Deputy Mayor 2004-2014, Mayor since 2014, Federal Board Member Austrian Municipal Association, State Board Salzburg Municipal Association, State Chairman Community Representatives Association
        </p>
        <p>
          Areas of Expertise<br> Finance, Construction, Spatial Planning, Social Affairs, Economy, City Marketing
        </p>
      </div>
    </figcaption>
  </figure>

  <figure>
    <img src="https://picsum.photos/id/103/300/300" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          Since 1997 Community Representative, City Councilor since 1999, Deputy Mayor 2004-2014, Mayor since 2014, Federal Board Member Austrian Municipal Association, State Board Salzburg Municipal Association, State Chairman Community Representatives Association
        </p>
        <p>
          Areas of Expertise<br> Finance, Construction, Spatial Planning, Social Affairs, Economy, City Marketing
        </p>
      </div>
    </figcaption>
  </figure>
</div>

Answer №2

To start, incorporate media queries into your .card-grid CSS class in order to modify the number of columns depending on the screen width. You can also customize the text size within each card for various screen sizes.

figure {
  border-radius: 1rem;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  margin: 10px;
}

figure>* {
  grid-area: 1/1;
  transition: .4s;
}

figure img {
  width: 100%;
  height: auto;
}

figure:hover figcaption {
  --_i: 0%;
  background-color: #ed161f;
}

figure:hover img {
  transform: scale(1.2);
}

@supports not (-webkit-mask-clip: text) {
  figure figcaption {
    -webkit-mask: none;
    color: #fff;
  }
}

.hover-content {
  position: absolute;
  top: 1rem;
  left: 1rem;
  right: 1rem;
  bottom: 1rem;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  opacity: 0;
  transition: opacity 0.4s;
  background-color: #ed161f;
}

figure:hover .hover-content {
  opacity: 1;
}

.hover-content p {
  color: #fff;
  font-family: 'Roboto', sans-serif;
  font-size: 1.25rem;
  margin: 0;
  padding-top: 20px;
}

.card-grid {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: auto auto auto auto;
  grid-auto-flow: column;
  place-content: center;
  background: #425a52;
}
<div class="card-grid">
  <figure>
    <img src="/img/hans_jorg.jpg" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          ... has been a municipal councilor since 1997, city councilor since 1999, deputy mayor from 2004-2014 and mayor since 2014 National board member Austrian Association of Municipalities, State Board Salzburg Municipal Association, State Chairman Community representatives association
        </p>
        <p>
          >Competence areas<br> Finance, Construction, Spatial planning, Social affairs, Economy, City marketing
        </p>
      </div>
    </figcaption>
  </figure>

  <figure>
    <img src="/img/hans_jorg.jpg" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
         ... has been a municipal councilor since 1997, city councilor since 1999, deputy mayor from 2004-2014 and mayor since 2014 National board member Austrian Association of Municipalities, State Board Salzburg Municipal Association, State Chairman Community representatives association
        </p>
        <p>
          Competence areas<br> Finance, Construction, Spatial planning, Social affairs, Economy, City marketing
        </p>
      </div>
    </figcaption>
  </figure>

  <figure>
    <img src="/img/hans_jorg.jpg" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          ... has been a municipal councilor since 1997, city councilor since 1999, deputy mayor from 2004-2014 and mayor since 2014 National board member Austrian Association of Municipalities, State Board Salzburg Municipal Association, State Chairman Community representatives association
        </p>
        <p>
          Competence areas<br> Finance, Construction, Spatial planning, Social affairs, Economy, City marketing
        </p>
      </div>
    </figcaption>
  </figure>

  <figure>
    <img src="/img/hans_jorg.jpg" alt="Portrait">
    <figcaption>
      <div class="hover-content">
        <p>
          ... has been a municipal councilor since 1997, city councilor since 1999, deputy mayor from 2004-2014 and mayor since 2014 National board member Austrian Association of Municipalities, State Board Salzburg Municipal Association, State Chairman Community representatives association 
        </p>
        <p>
          Competence areas<br> Finance, Construction, Spatial planning, Social affairs, Economy, City marketing
        </p>
      </div>
    </figcaption>
  </figure>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for keeping buttons anchored at the bottom of the page during scrolling

As a newcomer to Ionic, HTML, and CSS, I am currently exploring how to keep a button fixed while scrolling through the page. Despite specifying a fixed position in the CSS, the button does not remain fixed as intended. The button seems to be having troubl ...

Issue with Wordpress css rendering on Internet Explorer

My webpage is functioning well in Chrome and Firefox, but I'm facing compatibility issues with Internet Explorer. I've identified several bugs related to tables and layout, however, I'm struggling to resolve the font and text-transform prob ...

Ways to activate the date input field and reformat it for smooth insertion or selection in mysqli

I would like to enable the input of dates in UK format - DD/MM/YYYY on an HTML form. Before any PHP code is executed, such as queries (e.g. select, insert, etc.), I want PHP to convert the entered date from DD/MM/YYYY to YYYY-MM-DD. HTML <div class="f ...

Jspsych: Centering and aligning 3 p tags to the left on the page

Feeling pretty stuck at the moment. Here's what I have visually right now: I added a caption in Comic Sans and an arrow using MS Paint. Each line with color swatches is wrapped in a p tag. The goal is to center these p tags on the page (as they are ...

Learn how to organize a div element containing select options using categories such as gender and shoe size, similar to the filtering

Is there a way to sort div elements using a select menu, similar to how it is done on shopping websites? I am struggling with modifying my JS code in order to implement multiple selects. Although I believe my JS code is correct, it doesn't seem to be ...

Transferring content from a div class to an input class

I am seeking help to extract text from a div class and transfer it to an input class. Here is the code I have written: import os import time from selenium import webdriver from pyvirtualdisplay import Display from selenium.webdriver.common.by import By fr ...

Balancing heights with jQuery

Is there a way to set the height of an h3 element based on the tallest version of its siblings, but only for certain elements? I have a list where CSS organizes items into rows of 3. The last li element in each row has the class "endRow". I want to find t ...

What is the most effective method for tracking file upload progress using SSE?

I am currently working on creating a file upload progress feature that is compatible with older browsers using AJAX. In HTML5, there is the xhr.upload.progress event which works well for modern browsers, but I need an alternative for non-XHR2 browsers. I h ...

Setting the color for the :hover and :active states on a react JSX component: A step-by-step guide

<button onClick={fetchExchangeRates} style={{ backgroundColor: `${selectedColor}` }} className="hover text-white px-3 py-1 flex justify-center items-center gap-2 text- rounded-md" > Convert <FontAwesomeIcon className=&apo ...

What is the process for accessing an image via localhost on the same computer?

I created a program that saves images locally and stores their URLs in a database. When retrieving this data via ajax, the URLs are fetched and appended to a div class using the following code: $( document ).ready(function() { $.ajax({ url: "/ ...

What is preventing this brief code from functioning properly? I am trying to extract a value from an input field and add it to a div element

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="//ajax.googleapis.com/ajax/libs/jque ...

I am currently seeking a way to validate if a variable corresponds to the choice made in the dropdown menu. Any suggestions on how to accomplish this task?

I have put together a simple drop down menu. My goal is to grab the currently selected value from the drop down list, store it in a variable, and display it in the console. The ultimate objective is to compare that variable with another one to determine if ...

Loop through items in a list using Angular.js and display each item within an <

I am facing an issue where the model returned from the server contains html tags instead of plain text, such as b tag or i tag. When I use ng-repeat to create a list based on this model, the html is displayed as pure text. Is there a filter or directive av ...

Jquery allows for the toggling of multiple checkboxes

I have a group of check-boxes that I would like to toggle their content when checked. Currently, I am using jQuery to achieve this functionality, but I am searching for a way to optimize my code so that I do not need to write a separate function for each ...

Is Swiper carousel navigation secretly operating without being seen?

I've got a website that utilizes the Swiper carousel from SwiperJS, find it here: An issue I am facing is that the navigation elements are invisible but functional, with the pagination feature unaffected. This peculiar behavior is observed only in Sa ...

In a React application, adjusting the main container height to 100vh results in the window.scrollY position being set to

I was facing problems with flashing on the Safari/Firefox browsers. By setting the main container height to max-height: 100vh, I managed to resolve the flickering issue. However, I am also looking for a solution to keep track of the window.scrollY positi ...

What might be the reason for the border not reaching the bottom of the popup in my chrome extension?

I am currently working on a Chrome extension and using Bootstrap 5 with Sass to style its popup. However, I have encountered an issue where the border of the popup does not extend to the bottom as expected. What could be causing this problem? popup.html & ...

PHPWord setup complication

I am struggling to run a PHPWord script. When attempting to execute the sample example Text.php, it does not display anything. I have verified that the class is loading successfully. You can find more information in the documentation. If anyone has encou ...

Ways to Press the Enter Key on Different Browsers

Looking for a solution to get the keypress action working properly? I have a chat feature where I need to send messages. Here is what I have in the Form-tag (JSP based): function SendMessage() { if (event.keyCode===13) { ale ...

When pasting Arabic text into an input box, the words in HTML appear to be jumbled and shuffled around

When I replicate this text يف عام and input it into a box, the output is shown as follows عام يف ...