Biebk
Biebk

Reputation: 161

Flexbox image gallery responsiveness issue

I am on the way for making responsive image gallery using flexbox. For this, my structure of html is as follow:

<main class="image-container">
    <div class="image-wrapper">
        <img src="#">
    </div>
    <div class="image-wrapper">
        <img src="#">
    </div>
    ....so on many 
</main>

Following is my css snippet:

.image-container {
        width: 90%;
       height: 2480px;
        margin: 20px auto 0px;
        background: rgba(209, 217, 223, 0.705);
        display: flex;
        flex-wrap: wrap;
        flex-direction: column;
        justify-content: space-evenly;
        align-content: space-evenly;
        position: relative;            
   }
  .image-wrapper {
       width: 30%;
       margin: 0px auto 8px;
       flex-shrink: 0;
   }
    img{
        width: 100%;  
        height: 100%;
   }

Now in order to make this responsive, I have to make use of media queries. Suppose my laptop screen width is 1280px. And I trying to get 3 columns of images into 2 column at 760px breakpoint. The problem is as I try to reduce screen size , suddenly at 1180px awkward space is generated from the last column. I get it, I have rigid container height as 2480px . Because of that, my height is maintained constant and as width is 90% image are being smaller and 3rd column image is being added into other column. I have tried my best, so any suggestions are welcomed. Is there something is could do in html or css? I am sorry if you are having trouble to understand the scenario.

Upvotes: 1

Views: 259

Answers (3)

Amir Danish
Amir Danish

Reputation: 444

Here i add an example that display images of different heights and its responsive

/*
* The function calc is working wrong in case calculations in the Firefox
*/

.photobox{
  display: inline-block;
}

.photobox__previewbox{
  position: relative;
  overflow: hidden;
}

.photobox__preview{
  display: block;
  max-width: 100%;
}

.photobox__previewbox:before{
  content: "";
}

/* type 1 */

.photobox_type1 .photobox__previewbox:before{
  width: 0;
  height: 0;
  padding: 25%;
  border-radius: 50%;
  
  position: absolute;
  top: 0;
  left: 0;

  background-color: var(--photoboxOverlay, rgba(0, 0, 0, .8));
  transition: transform calc(var(--photoboxAnimationDuration, .4s) / 2) ease calc(var(--photoboxAnimationDuration, .2s) / 2);
  will-change: transform;
  transform: scale(0);
}

.photobox_type1:hover .photobox__previewbox:before{
  transform: scale(2);
  transition-duration: var(--photoboxAnimationDuration, .4s);
  transition-delay: 0s;
}

.photobox_type1 .photobox__label{
  width: 50%;
  transform: translate(-200%, -50%);
  transition: transform var(--photoboxAnimationDuration, .4s) ease-out;
  will-change: transform;
  
  position: absolute;
  top: 50%;
  left: 15%;
}

.photobox_type1:hover .photobox__label{
  transition-duration: var(--photoboxAnimationDuration, .4s);
  transform: translate(0, -50%);
}

/* type 2*/

.photobox_type2 .photobox__previewbox:before{
  width: 0;
  height: 0;
  padding: 25%;
  
  border-radius: 50%;
  background-color: var(--photoboxOverlay, rgba(0, 0, 0, .8));
  
  position: absolute;
  top: 0;
  right: 0;

  transition: transform var(--photoboxAnimationDuration, .2s) ease calc(var(--photoboxAnimationDuration, .2s) / 2);
  will-change: transform;
  transform: scale(0);
}

.photobox_type2:hover .photobox__previewbox:before{
  transform: scale(2);
  transition-duration: var(--photoboxAnimationDuration, .4s);
  transition-delay: 0s;
}

.photobox_type2 .photobox__label{
  width: 50%;
  text-align: right;
  
  transform: translate(200%, -50%);
  transition: transform var(--photoboxAnimationDuration, .4s) ease-out;
  will-change: transform;
  
  position: absolute;
  top: 50%;
  right: 15%;
}

.photobox_type2:hover .photobox__label{
  transition-duration: var(--photoboxAnimationDuration, .4s);
  transform: translate(0, -50%);
}

/* type 3 */

.photobox_type3 .photobox__previewbox:before{
  width: 0;
  height: 0;
  padding: 25%;
  
  background-color: var(--photoboxOverlay, rgba(0, 0, 0, .8));
  border-radius: 50%;
  
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 2;

  transition: transform var(--photoboxAnimationDuration, .4s) ease;
  will-change: transform;
  transform: translate(-50%, -50%) scale(0);
}

.photobox_type3:hover .photobox__previewbox:before{
  transform: translate(-50%, -50%) scale(4);
  transition-duration: calc(var(--photoboxAnimationDuration, .4s) * 2);
}

.photobox_type3 .photobox__label{
  width: 95%;
  text-align: center;
  
  opacity: 0;
  transform: translate(-50%, -50%);
  transition: opacity var(--photoboxAnimationDuration, .4s) ease-out;
  will-change: opacity;
  
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 3;
}

.photobox_type3:hover .photobox__label{
  transition-duration: calc(var(--photoboxAnimationDuration, .4s) / 2);
  transition-delay: calc(var(--photoboxAnimationDuration, .4s) / 2);
  opacity: 1;
}

.photobox_type3 .photobox__preview{
    transition: transform var(--photoboxAnimationDuration, .4s) cubic-bezier(0.71, 0.05, 0.29, 0.9);
  will-change: transform;
    transform: scale(1);
}

.photobox_type3:hover .photobox__preview{
  transform: scale(1.2);
}

/* type 4 */

.photobox_type4 .photobox__previewbox:before{
  width: 0;
  height: 0;
  padding: 25%;
  
  background-color: var(--photoboxOverlay, rgba(0, 0, 0, .8));
  border-radius: 50%;
  
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 2;

  transition: transform var(--photoboxAnimationDuration, .4s) ease;
  will-change: transform;
  transform: translate(-50%, -50%) scale(0);
}

.photobox_type4:hover .photobox__previewbox:before{
  transform: translate(-50%, -50%) scale(4);
  transition-duration: calc(var(--photoboxAnimationDuration, .4s) * 2);
}

.photobox_type4 .photobox__label{
  width: 95%;
  text-align: center;
  
  opacity: 0;
  transform: translate(-50%, -50%);
  transition: opacity var(--photoboxAnimationDuration, .4s) ease-out;
  will-change: opacity;
  
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 3;
}

.photobox_type4:hover .photobox__label{
  transition-duration: calc(var(--photoboxAnimationDuration, .4s) / 2);
  transition-delay: calc(var(--photoboxAnimationDuration, .4s) / 2);
  opacity: 1;
}

.photobox_type4 .photobox__preview{
    transition: transform var(--photoboxAnimationDuration, .4s) cubic-bezier(0.71, 0.05, 0.29, 0.9);
  will-change: transform;
    transform: scale(1) translate(0, 0);
}

.photobox_type4:hover .photobox__preview{
  transform: scale(1.2) translate(4%, 4%);
}
/*
* demo styles for photobox
*/

.photobox{
  color: #fff;
  font-size: 2.5rem;
  font-weight: 700;
  width: 33.33333%;
  --photoboxOverlay: rgba(72, 27, 174, .7);
  /*--photoboxAnimationDuration: .5s;*/
}

@media screen and (max-width: 480px){
  .photobox{
    width: 100%;
  }
}

/*
=====
DEMO
=====
*/

@media (min-width: 768px){

  html{
    font-size: 62.5%;
  }
}

@media (max-width: 767px){

  html{
    font-size: 50%;
  }
}



a{
  text-decoration: none;
  color: inherit;
}

a:hover, a:focus{
  text-decoration: underline;
}

.page{
  flex-grow: 1;
  order: 1;
}

.page__container{
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
}
<div class="page">
  <div class="page__demo">
    <div class="page__container">
      <div class="photobox photobox_type1">
        <div class="photobox__previewbox">
          <img src="https://profiladv.ro/wp-content/uploads/2017/03/spring-awakening-1197602_960_720.jpg" class="photobox__preview" alt="Preview">
          <span class="photobox__label">Spring and beauty of nature</span>
        </div>
      </div>
      <div class="photobox photobox_type2">
        <div class="photobox__previewbox">
          <img src="https://www.cesarsway.com/wp-content/uploads/2015/06/Spring-health-tips.jpg" class="photobox__preview" alt="Preview">
          <span class="photobox__label">Looks so adorable</span>
        </div>
      </div>
      <div class="photobox photobox_type3">
        <div class="photobox__previewbox">
          <img src="https://headtopics.com/images/2019/8/3/independent/fear-and-persecution-in-pakistan-s-hazara-community-1157503460163690496.webp" class="photobox__preview" alt="Preview">
          <span class="photobox__label">Chashm badami</span>
        </div>
      </div>
      <div class="photobox photobox_type4">
        <div class="photobox__previewbox">
          <img src="https://www.sidetracked.com/wp-content/uploads/2014/09/wakhan-corridor-JanBakker.jpg" class="photobox__preview" alt="Phot otaken by Jan Bakker">
          <span class="photobox__label">A Short Walk in the Other Asia</span>
        </div>
      </div>            
    </div>  
  </div>
</div>

Upvotes: 1

ates_irem
ates_irem

Reputation: 286

Here what I did to make this better. I hope I could help you out a little bit.

        .image-container {
            width: 90%;
            height: 2800px;
            min-height:fit-content;
          scroll-behavior:smooth;
          overflow:scroll;
          max-width:1280px;
            margin: 20px auto 20px auto;
            padding: 20px 0px;
            background: rgba(209, 217, 223, 0.705);
            display: flex;
            flex-wrap: wrap;
            flex-direction: column;
            justify-content: flex-start;
            align-content: space-evenly;
            position: relative;

            
       }
 @media screen and (max-width:500px) {
  .image-container {
        flex-direction: column;
        max-height: inherit;
                 
   }
  .image-wrapper {
       width: 90%;
   }
}
@media screen and (max-width:500px) {
  .image-wrapper {
       width: 90%;
   }
}

Upvotes: 1

ates_irem
ates_irem

Reputation: 286

For this kind of problem you should add media query codes to your css file. If you wanted to show 3 images on large screens you should remove flex-direction: row from .image-container so that image-wrappers can be side by side. When the screen shrinks they should wrap. I am adding the codepen link here so that you can review it. If you have any other questions. I can answer them. https://codepen.io/atesirem/pen/ExmPgdP

.image-container {
        width: 90%;
        margin: 20px auto 0px auto;
        padding:20px;
        background: rgba(209, 217, 223, 0.705);
        display: flex;
        flex-wrap: wrap;
        flex-direction: row;
        justify-content: center;
        align-content: space-evenly;
        position: relative;            
   }

  .image-wrapper {
       width: 30%;
       margin: 0px auto 8px;
       flex-shrink: 0;
   }
    img{
        width: 100%;  
        height: auto;
   }

@media screen and (max-width:991px) {
  .image-container {
        flex-wrap: wrap;
   }
}
@media screen and (max-width:768px) {
  .image-container {
                
   }
  .image-wrapper {
       width: 45%;
   }
}
@media screen and (max-width:500px) {
  .image-container {
        flex-direction: column;
                 
   }
  .image-wrapper {
       width: 90%;
   }
}

Upvotes: 1

Related Questions