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>
    <li style="background-image: url(./img/giraffe.jpg);">
    </li>
    <li style="background-image: url(./img/neptune.jpg);">
    </li>
    <li style="background-image: url(./img/sea.jpg);">
    </li>
    <li style="background-image: url(./img/snail.jpg);">
    </li>
    <li style="background-image: url(./img/space.jpg);">
    </li>
    <li style="background-image: url(./img/superhero.jpg);">
    </li>
    <li style="background-image: url(./img/surfer.jpg);">
    </li>
    <li style="background-image: url(./img/triangle.jpg);">
    </li>
    <li style="background-image: url(./img/psychedelic.jpg);">
    </li>
    <li style="background-image: url(./img/planet.jpg);">
    </li>
    <li style="background-image: url(./img/presents.jpg);">
    </li>
  </ul>

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.


ul,
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 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
MIN, MAX, COUNT, AVG and SUM in SQLSQL/MySQLrandom function in SCSS (Sass)SCSSHow to Use SQL MAX() Function with Dates?SQL/MySQLHow to use PHPMailer to send an Email via Gmail SMTP Server?PHPWhat’s a Web Storage API in JavaScript?JavaScriptThe Complete Basic Explanation of PHP SessionsPHP