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;}
display: flex;
gives us some extra properties we can use to style the gallery withflex-wrap: wrap;
is very important for a gallery, otherwise it would look like this:justify-content: center;
for positioning the gallery in the center of the screen no matter the size of the screen
display: flex;
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;}
.gallery img {}
selects all image elements within an element with the gallery class - this is useful if you have images on your page that are not part of the gallery, that you want styled differentlyobject-fit: cover;
to crop the image without squishing their dimensionsWithout 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;}
img:hover
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 galleries allow you to see every image in full, but they are a little more tricky to get looking nice.
.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.
flex-direction: column;
puts the images into columnsalign-content: center;
is used to put the columns in the center of the gallery - this is used instead of justify-content: center;
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:
.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.
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.