Yogesh Chauhan's Blog

Learn to make a responsive gallery using CSS Grid and without media queries

in CSS on September 18, 2020

Step 1: Add HTML

Let’s add some images first. We will add images as background images.

  <ul class="responsive_gallery">
    <li style="background-image: url(./img/cat.jpg);">
    <li style="background-image: url(./img/giraffe.jpg);">
    <li style="background-image: url(./img/neptune.jpg);">
    <li style="background-image: url(./img/sea.jpg);">
    <li style="background-image: url(./img/snail.jpg);">
    <li style="background-image: url(./img/space.jpg);">
    <li style="background-image: url(./img/superhero.jpg);">
    <li style="background-image: url(./img/surfer.jpg);">
    <li style="background-image: url(./img/triangle.jpg);">
    <li style="background-image: url(./img/psychedelic.jpg);">
    <li style="background-image: url(./img/planet.jpg);">
    <li style="background-image: url(./img/presents.jpg);">

Step 2: Remove margin from body. 

If you want some additional padding around the gallery then keep the margin.

body {
  margin: 0;

Step 3: Add style for ul and li elements.

We are removing list styles as we want to use those as image holders.

li {
  list-style: none;
  padding-left: 0;
  margin: 0;

Step 4: Add style for background images.

Keep the background-size the way you want. Cover will keep good proportion of the image with position on center.

li {
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;

Step 5: Define CSS Grid for gallery.

auto-fill will keep the full browser width occupied. minmax function will make sure that the image won’t go under 300px size and keep using more size if available.

For rows, we want at least 25vh but we don’t want the image more than 200px long.

.responsive_gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  grid-auto-rows: minmax(25vh, 200px);

Step 6: nth-of-type

What the below CSS does, is select every second list item inside unordered lists and make it take 2 spans in grid.

.responsive_gallery li:nth-of-type(2n + 2) {
  grid-row: span 2;

That should create a definitely great responsive image gallery without any media queries.

Download the files from my Github repo: css-grid-responsive-gallery by Yogesh

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to disable the Recovery Mode in WordPress?WordPressFilling a button background from left to right using CSSCSSSelect statement in Postgres with examplesPostgresHow to create a sticky menu pin using CSS?CSSHow different is Handling Events in React vs HTML DOM?ReactWordPress: How to print ACF array field values?WordPress