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

The radio button that disables other inputs only functions correctly for a single element when using Query Selector, but does not work with Query

I have attempted to develop a form section that is disabled when the user selects option A and enabled when they choose option B. document.getElementById('delivery').onclick = function() { var disabled = document.querySelectorAll(".dis ...

Methods for encoding and decoding special characters using either JavaScript or jQuery

I am looking for a solution to encode and decode various special characters using JavaScript or jQuery... ~!@#$%^&*()_+|}{:"?><,./';[]\=-` I attempted to encode them using the following code snippet... var cT = encodeURI(oM); // ...

tips for integrating html5 elements with django forms

I am interested in utilizing the following code: # extra.py in yourproject/app/ from django.db.models import FileField from django.forms import forms from django.template.defaultfilters import filesizeformat from django.utils.translation import ugettext_ ...

Tips for fixing alignment problems using CSS

I am experiencing a problem where I am unable to figure out how to align the content in one column with text positioned next to it in a right-aligned format. /* Personal Details */ .personal-info { width: 90%; padding: 25px; border-bottom: 1px so ...

Inconsistency with Mobile Viewport Problem

Apologies for the chaos below, but I've spent quite some time attempting to fix this on my own. It's time to surrender and seek assistance! Here are some screenshots to illustrate the issue: The problem is that sometimes the webpage functions c ...

Enclose Words Inside Unconventional Outline

Place text within a uniquely shaped border… I am attempting to achieve this goal in the most responsive and backward compatible way possible. I understand that compromise may be necessary. I have made progress with the help of this thread, but the svg ...

Connecting Bootstrap Tabs Dropdown to Website LinksIncorporating Bootstrap Tabs Dropdown Menu

Having an issue with my dropdown list in the Twitter Bootstrap tab - it's not responding when clicked. I've checked Stackoverflow for solutions but nothing has worked so far. I even tried removing the data-toggle='tab' attribute. Just ...

Aligning images and input fields vertically with relative measurements

I'm looking for a way to vertically position two images, one on the left and one on the right, so that they are centered between labels and input fields within a containing div. Is it achievable using only relative lengths? Check out this jsfiddle fo ...

Tips for including HTML tags in strings without causing issues with nested tags

I am struggling with a string that contains both text and HTML tags, specifically <a href=""></a> tags. My goal is to apply the "i" tag around the text outside of the "a" tags without creating nested tags like: <i><a href=""></a ...

What is the best method for eliminating a character from all elements in jQuery classes?

I am working on an Angular 4 app where every .inner-page class in a html element includes a "/". For instance: <div _ngcontent-c0="" class="inner-page /login"> <div _ngcontent-c0="" class="inner-page /register"> I need to eliminate the "/" c ...

Tips for placing multiple images onto one canvas

I am currently working on a web application that allows users to create sketches, define their positions and sizes, and then save them to a database. In another section of the website, I aim to retrieve these images and display them on a single canvas. To ...

Adjust the quantity of images shown in the Flex Slider carousel

My website includes a flex slider with a carousel, but it seems that I did not configure the properties of the slider correctly (or it could be a CSS issue) as shown here: . The last image in the carousel is only partially visible. While I am able to clic ...

Content is the main driver for CSS Flexbox dynamic aspect-ratio code

When creating a grid layout for a webpage, I opted to use Flexbox. My goal was to incorporate some automatic functionality so that additional grid boxes could be included without the need to manually add classes or styles in the HTML code. One particular f ...

How come there is such a large space for clicking between my logo and the navigation bar, and what can be done to eliminate it?

* { box-sizing: border-box; padding: 0; margin: 0; } ul { list-style-type: none; } .nav-links, .logo { text-decoration: none; color: #000; } .logo { max-width: 80%; width: 20%; height: 20%; } .navbar img { width: 10%; height: auto; di ...

The height of the absolute div tag does not match the height of the parent div

I am currently designing an editor that requires a line-number bar (gutter) on the left side. I have set a div with a height of 100% and its parent div with overflow: auto. My expectation is that if the content exceeds the given height, the editor-body sh ...

Interact with HTML Radio Buttons to Trigger Input Opening

I need to have a message saying "We're sorry..." and a "black box" displayed only when the radio button is set to YES, otherwise keep it hidden. Can I achieve this using JavaScript only, or is there a way to do it with HTML alone? <h3 >Did you ...

Function for editing a button in an AngularJS single page application

I am a beginner in AngularJS and I'm currently working on a project to create a single page application for tracking expenses. However, I'm facing some challenges with my code. Although I have successfully implemented most of the functions, I am ...

The padding of dynamically generated bootstrap buttons diminishes

I am facing an issue with a bootstrap button bar that is using the well class. The problem occurs when I create it twice in my code: once using HTML and another using dynamic javascript. The bootstrap functions properly when created with HTML. However, t ...

Include a border around the entire tooltip

I'm having trouble adding a border to my tooltip that follows the outer lines of the entire tooltip. Currently, I've only been able to add a border to the upper part of the tooltip, which overlaps with the arrow section and isn't the desired ...

Safari's Web Audio API suffering from subpar performance and various shortcomings

For my University project, I am developing an HTML and JavaScript-based mp3 player using the Web Audio API. You can check out the progress of this project by visiting this link: While everything is running smoothly on Firefox and Chrome, Safari is posing ...