`CSS gallery that adjusts its layout after a certain number of rows`

My goal is to create a responsive gallery following this tutorial. Here is my attempt on codesandbox.io

However, I'm facing an issue where only four columns are shown instead of the expected five. The image below illustrates the problem:

https://i.sstatic.net/ZijvS.jpg

Code CSS (media query not included)

#gallery {
  line-height: 0;
  -webkit-column-count: 5; /* split it into 5 columns */
  -webkit-column-gap: 5px; /* give it a 5px gap between columns */
  -moz-column-count: 5;
  -moz-column-gap: 5px;
  column-count: 5;
  column-gap: 5px;
}

#gallery img {
  width: 100% !important;
  height: auto !important;
  margin-bottom: 5px; /* to match column gap */
}

HTML

<body>
<div id="gallery">
  
   <img src="https://images.unsplash.com/photo-1620920709484-995504fbecd2?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80">
   <!-- More images here -->

  </div>
 
 </body>

If you have any insights or solutions, please share them. Appreciate your help.

//EDIT I came across what seems like a great gallery example at https://codepen.io/maxvoltar/pen/eYOPdMG

Answer №1

Although I'm not entirely sure why the grid is causing issues in this particular scenario, I managed to resolve the problem by making some adjustments to your HTML and CSS for better specificity.

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />

    <title>Image Gallery</title>
    <meta name="description" content="Responsive Image Gallery" />
    <meta name="author" content="Tim Wells" />
    <link rel="stylesheet" href="/src/styles.css" />
    <style type="text/css"></style>
  </head>
  <body>
    <div id="gallery">
      <div class="combo">
        <img
          src="https://images.unsplash.com/photo-1620827252031-146d52644aac?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=967&q=80"
        />
        <img
          src="https://images.unsplash.com/photo-1620920709484-995504fbecd2?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80"
        />
      </div>
      ... <!-- More image combos -->
    </div>
  </body>
</html>

CSS

#gallery {
  height: 100%;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  max-width: 1400px;
  margin: 0 auto;
}

#gallery img {
  object-fit: contain;
  width: 100%;
  padding-bottom: 2px;
}

.combo {
  margin: 0;
  padding: 0 2px;
  width: 50%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

@media only screen and (min-width: 760px) {
  .combo {
    width: 33%;
  }
}

@media only screen and (min-width: 1200px) {
  .combo {
    width: 20%;
  }
}

Instead of using grid for containers with an unknown number of items, I recommend using flexbox, especially when dealing with responsiveness.

Grid works best for a fixed number of specific items that need to be positioned responsively.

It's also important to set a maximum width for your container unless there is a specific reason not to do so.

You can view the updated sandbox with flexbox and width constraints here.

Answer №2

The tutorial's layout is determined by the number and dimensions of the images, with columns stacking vertically to display the content. In this example, 24 images (both small and big) are used in the tutorial.

By adding two more images, you can see the layout adjust accordingly when viewing the code in full screen mode on a high-resolution display with five columns.

#gallery {
  line-height: 0;
  -webkit-column-count: 5; /* split it into 5 columns */
  -webkit-column-gap: 5px; /* give it a 5px gap between columns */
  -moz-column-count: 5;
  -moz-column-gap: 5px;
  column-count: 5;
  column-gap: 5px;
}

#gallery img {
  width: 100% !important;
  height: auto !important;
  margin-bottom: 5px; /* to match column gap */
}

@media (max-width: 1200px) {
  #gallery {
    -moz-column-count: 4;
    -webkit-column-count: 4;
    column-count: 4;
  }
}

@media (max-width: 1000px) {
  #gallery {
    -moz-column-count: 3;
    -webkit-column-count: 3;
    column-count: 3;
  }
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />

    <title>Image Gallery</title>
    <meta name="description" content="Responsive Image Gallery" />
    <meta name="author" content="Tim Wells" />

    <style type="text/css"></style>
  </head>


  <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />

    <title>Image Gallery</title>
    <meta name="description" content="Responsive Image Gallery" />
    <meta name="author" content="Tim Wells" />
    <link rel="stylesheet" href="/src/styles.css" />
    <style type="text/css"></style>
  </head>
  <body>
    <div id="gallery">
      [insert image URLs here]
    </div>
  </body>
</html>

To potentially address any layout issues, consider utilizing flexbox along with modifying the HTML structure accordingly.

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

What is the best way to display a webpage within an iOS app using PhoneGap?

In my iOS phonegap app, I am looking to have a single view that displays a web page. Can someone guide me on how to load this specific view with a particular URL using JavaScript? Although I primarily develop native iOS applications and do not have expert ...

Troubleshooting: Why is my CSS background image URL not displaying?

I'm struggling to understand why the background image is not showing up. However, when I use content:url(), it does appear. The issue with using content:url() is that I can't control the size of the image. Below, you will find the complete code f ...

Unable to Toggle the 'Checked' Prop on Selection with React Bootstrap CheckBox

As a newcomer to React Bootstrap, I've been encountering an irritating problem with checkboxes in a form I'm developing. After updating the state, the checkboxes don't remain checked when selected. However, if I check them again, they stay ...

Is it possible to have the cells in a grid adjust and fill the available space when the size of the grid changes?

Hey there! I'm currently working on creating a dynamic grid but I'm facing an issue with making the cells fill the space between them when the size of the grid changes. Within my function that generates the grid, I take in the size as a paramete ...

Customizing the image border at the top of the navigation bar

I'm in need of assistance with my navigation bar design. I have a specific vision for it, and I even have an image of the "rough texture" that I want to incorporate into the navigation menu. However, I've been struggling to figure out how to use ...

What is the best way to implement ajax loading on scroll?

I need to implement lazy loading for multiple ajax pages on my website. I want the ajax content to load dynamically as the user scrolls down the page, similar to infinite scroll or lazy image loading. $('.lazy_content').on('load', fu ...

Repeatedly tapping the Bootstrap Dropdown option

I'm having some trouble with my dropdown menu. When I click on the button to open it, everything works fine. But when I try to close it by clicking again, the menu glitches and sometimes won't close at all. Here's the HTML code I'm usin ...

JavaScript prohibiting input prior to execution

Having trouble with executing this javascript before the user input, can anyone assist me in resolving this issue. I just want to create a simple html page with a textbox and a button. When the button is clicked, it should open a new window with a modifie ...

Inverting the hierarchy of a tree structure

I am currently working with a tree structure and utilizing the jstree jQuery plugin. My main objective is to reverse the structure. https://i.sstatic.net/PG1Ha.png The desired structure should resemble the one shown in this image. I have made modificatio ...

Display only the selected index and conceal the rest

I am facing an issue where I have added a label and input in ng-repeat. The functionality works fine when the user clicks edit to show the input and hide the label. However, the problem arises when the user clicks on the new button as it displays the new i ...

Having trouble implementing button functionality in a web app using JS, jQuery, HTML, and CSS along with an API integration

I am currently developing a web search engine that utilizes the Giphy API... However, I've encountered difficulties with the button function. I have created buttons to modify the page format and adjust the number of search results displayed.... The We ...

Duplicate text content from a mirrored textarea and save to clipboard

I came across some code snippets here that are perfect for a tool I'm currently developing. The codes help in copying the value of the previous textarea to the clipboard, but it doesn't work as expected when dealing with cloned textareas. Any sug ...

Arrangement containing 4 separate div elements - 2 divs are fixed in size, 1 div adjusts dynamically (without scroll), and the remaining div fills the

I am trying to implement a specific layout using HTML and CSS on a webpage. So far, I have successfully created the black, red, and blue areas, but I am facing challenges with the scrollable green content. It currently has a static height, but I need it to ...

Managing the Fullscreen Feature of HTML5 Video

Is there a way to customize the fullscreen attribute for HTML5 video? For instance, can I make it so that fullscreen mode only displays 30% of the video? My goal is to have three browser tabs open, with each tab showing a different portion (33%) of the vi ...

What is the best way to display a single instance of a React component that is declared in multiple locations within the app?

Imagine I have 2 main components, A and B. Now, component C needs to be created inside both A and B. How can I guarantee that the same exact instance of C is generated in both places? Essentially, I want them to stay synchronized - any changes made to one ...

"Utilizing the appendTo attribute in primeNG within a ContextMenu component - A step-by-step guide

Currently, I am attempting to utilize the appendTo attribute within the ContextMenu component. My intention is to associate this behavior with a specific element, such as a div. ...

The carousel feature in Angular JS is malfunctioning

My angular carousel is not working properly and I am facing issues. You can view the carousel here. The controller in my Angular code does not seem to be calling out the images. To see the source code, you can click here. I have followed the coding demon ...

Adjusting the size of an image and its accompanying span within the container div

I have a fixed width and height div where I want to include an image with a caption (using img and figurecaption) without exceeding the parent's dimensions. Here is what I attempted: ` parent-> height: 100px; width: 100px; margin: 0; img-> d ...

Is it possible to modify the default name from "choose file" to "upload file" while maintaining the same functionality in AngularJS?

After implementing the code below, everything was working smoothly. However, I noticed that when I selected a file for the first time, the upload file button appeared to shrink in size. Upon inspecting my code, I came across: Code Inspection : (from cons ...

Conceal hyperlink repeatedly using jQuery

I am struggling with implementing a feature where I have two links, one displayed and the other hidden. Upon clicking the first link, it should disappear and the second link should be displayed. Clicking the second link should then hide itself and show the ...