1

I am a very new developer attempting to make a presentable photo-gallery-esque type thing to practice a bit. I have been leaning on CSS grid heavily for my layout...and I am pretty proud of what I have thus far.

I have four cards each containing an image thumbnail, a header, and some text. When the user hovers over any card they have the option to "view" the image which brings up a full screen modal. Everything works as I have intended...however...when I decrease the screen size some cards end up disappearing off screen!

I am very new to CSS grid and I have tried just about everything I know at this point. Please help me cross the finish line!

The code below works perfectly if just copy-pasted into the html portion on codepen.io.

Thank you in advance for any help you may offer!

const buttons = document.querySelectorAll('button');
const modal = document.querySelector('.modal');
const image = modal.querySelector('img');

buttons.forEach(button => {
  button.addEventListener('click', handleButtonClick);
});

function handleButtonClick(event) {
  const card = event.currentTarget.closest('.card');
  const chosenImage = card.querySelector('img').src;

  image.src = chosenImage;
  modal.classList.add('open');
}

document.addEventListener('click', function(event) {
  const target = event.target;
  const isModal = target.classList[0] === 'modal';

  if (isModal) {
    modal.classList.remove('open');
  }
});
body {
  margin: 0;
  height: 100vh;
  display: grid;
  align-content: center;
  background: linear-gradient(0deg, rgba(130, 109, 118, 1) 0%, rgba(172, 52, 52, 1) 100%);
}

.wrapper {
  display: grid;
  grid-gap: 40px;
  justify-content: center;
  grid-template-columns: repeat(auto-fit, 300px);
  grid-template-rows: 450px;
  grid-auto-rows: 450px;
}

.card {
  border: solid 5px #ac3434;
  border-radius: 0.8rem;
  overflow: hidden;
  background: #3a363670;
  display: grid;
  grid-gap: 4px;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(8, 1fr);
}

.img-wrapper {
  grid-column: 2 / span 3;
  grid-row: 2 / span 3;
  display: grid;
}

.img-wrapper img {
  height: 100%;
  width: 100%;
  object-fit: cover;
  border: solid 3px #ac3434;
  border-radius: 50%;
}

.card-body {
  grid-column: 1 / -1;
  grid-row: 5 / -1;
  padding: 0 10px 0;
  font-family: 'Ubuntu', sans-serif;
}

.card-body h2 {
  font-family: 'Anton', sans-serif;
}

.card-overlay {
  grid-column: 1 / -1;
  grid-row: 1 / -1;
  background: #ac34347a;
  display: grid;
  place-items: center center;
  transform: translateY(100%);
  transition: 0.4s;
}

.card:hover .card-overlay {
  transform: translateY(0%);
}

.card-overlay button {
  background: none;
  color: white;
  text-transform: uppercase;
  position: relative;
  bottom: 78px;
  border: solid 3px white;
  border-radius: 0.4rem;
  font-family: 'Ubuntu', sans-serif;
}

.modal {
  height: 100vh;
  width: 100vw;
  position: fixed;
  background: #0000008f;
  display: grid;
  place-items: center center;
  /* Make modal invisible until triggered */
  opacity: 0;
  /* Makes it so the modal does not log click 
   events */
  pointer-events: none;
}

.open {
  /* Displays the modal */
  opacity: 1;
  pointer-events: all;
}

.modal-inner {
  width: 500px;
}

.modal-inner img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
<div class="wrapper">
  <div class="card">
    <div class="img-wrapper">
      <img src="https://picsum.photos/500">
    </div>
    <div class="card-body">
      <h2>Sunny Walls</h2>
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim cupiditate molestias sed ea sit, dolore quos itaque consectetur doloribus at. Dolor accusamus consequuntur perspiciatis! Deserunt?
      </p>
    </div>
    <div class="card-overlay">
      <button>View ➜</button>
    </div>
  </div>
  <div class="card">
    <div class="img-wrapper">
      <img src="https://picsum.photos/500">
    </div>
    <div class="card-body">
      <h2>Kit-the-Kat</h2>
      <p>
        Lorem ipsum dolor sit, amet consectetur adipisicing elit. Dignissimos quaerat veritatis nobis voluptas minus exercitationem.
      </p>
    </div>
    <div class="card-overlay">
      <button>View ➜</button>
    </div>
  </div>
  <div class="card">
    <div class="img-wrapper">
      <img src="https://picsum.photos/500">
    </div>
    <div class="card-body">
      <h2>Sass in the City</h2>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo accusantium consectetur vel ullam assumenda corrupti id ratione odio, nisi adipisci?
      </p>
    </div>
    <div class="card-overlay">
      <button>View ➜</button>
    </div>
  </div>
  <div class="card">
    <div class="img-wrapper">
      <img src="https://picsum.photos/500">
    </div>
    <div class="card-body">
      <h2>City Things</h2>
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Sint culpa suscipit libero consequatur quod non dolore neque aperiam nihil beatae? Dolores, deserunt.
      </p>
    </div>
    <div class="card-overlay">
      <button>View ➜</button>
    </div>
  </div>
</div>

<div class="modal">
  <div class="modal-inner">
    <img>
  </div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
dataPhix
  • 13
  • 4
  • 1
    The problem is `align-content: center` on your primary grid container. Once you remove that rule, the items no longer disappear. See the duplicate post for an explanation. – Michael Benjamin Feb 12 '20 at 03:24
  • 1
    That solved it! I removed the align-content: center from the main grid container and centered the child grid container with margin: auto 0 auto; – dataPhix Feb 12 '20 at 14:33

1 Answers1

0

You need to use media tags in the css.

Your site is not responsive and when you change screen size it does not resize the components.

https://www.w3schools.com/css/css_rwd_mediaqueries.asp

Kingsley Mitchell
  • 2,412
  • 2
  • 18
  • 25