Simple Gallery Tutorial (CSS)

Square Image Gallery

jump to rectangular gallery

home

I've tried multiple ways of making galleries, but the easiest one I can center that also adjusts to screen size is flexbox.

Flexbox simply refers to a div styled with display: flex; and the elements inside of it.

.gallery {
  width: 500px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;}
  
.gallery img {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border: 2px solid darkolivegreen;
  background: yellowgreen;
  margin: 2px;}
  
.gallery img:hover {
  object-fit: contain;}

Here is the CSS that styles the gallery div and the images inside of it. Each selector will be discussed.


.gallery {
  width: 500px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;}

If you want your gallery left aligned you don't even have to bother with flexbox, a normal div will do just fine:

.gallery img {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border: 2px solid darkolivegreen;
  background: yellowgreen;
  margin: 2px;}

Without object-fit: cover; we get this: (stretched and squished pictures).

I still want the viewer to see the full image however, so I changed the object-fit: when the user hovers over the image.

.gallery img:hover {
  object-fit: contain;}

If all of your images have the same ratio, you don't need to worry about this, just set the width and height to be the same for each image (rectangles even work).

Rectangular Image Gallery

jump to square gallery

Rectangular galleries allow you to see every image in full, but they are a little more tricky to get looking nice.

four leaf clover four leaf clover four leaf clover four leaf clover four leaf clover four leaf clover two four leaf clovers with heart shaped leaves two four leaf clovers, each in a small bottle four leaf clover two four leaf clovers two four leaf clovers four leaf clover four leaf clover small four leaf clover
.gallery2 {
  height: 400px;
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-content: center;}
.gallery2 img {
  width: 100px;
  border: 1.5px solid darkolivegreen;
  margin: 2px;}

Here the images are organized into columns, all the same width so that the edges of the gallery are straight.

Column based rectangular galleries look the best in my opinion, but if you want your pictures organized in a particular sequence, they are tricky. Thus heres a row based rectangular gallery:

four leaf clover four leaf clover four leaf clover four leaf clover four leaf clover four leaf clover two four leaf clovers with heart shaped leaves two four leaf clovers, each in a small bottle four leaf clover two four leaf clovers two four leaf clovers four leaf clover four leaf clover small four leaf clover
.basic {
  width: 500px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;}  
.basic img {
  height: 100px;
  border: 1.5px solid darkolivegreen;
  margin: 2px;}

This is very similar to the square gallery, with the gallery div using the same properties (flex really only used to center the images), but of course the images are rectangles. The images are given a set height rather than width, because they are in rows rather than columns (contrasting to the first rectangular gallery).

This is not my favorite aesthetically, as I prefer to have the sides flat, but it is an option.


four leaf clover four leaf clover four leaf clover four leaf clover four leaf clover four leaf clover two four leaf clovers with heart shaped leaves two four leaf clovers, each in a small bottle four leaf clover two four leaf clovers two four leaf clovers four leaf clover four leaf clover small four leaf clover

This is about as basic as you can get without just dumping the images. They aren't in a flexbox, the images are just given the same height so they fit somewhat neater together .basic2 img { height: 100px;}. The previous example is based off of this, but it uses flexbox so the rows can be centered.