,

How to Use Custom CSS in WordPress Image Galleries (Step by Step)

Browser window with tilted Polaroid style gallery images and CSS code lines

Galleryberg and the WordPress block editor give you a lot of design controls out of the box: columns, spacing, rounded corners, shadows, captions, and hover effects. But sometimes you want a look that no settings panel offers, like photos that sit on the page as tilted Polaroid prints, or images that stay black and white until a visitor hovers over them. That is where a few lines of custom CSS come in.

In this guide, you will learn how to add custom CSS to WordPress image galleries the safe way. We will cover where to put your CSS, how to target just one gallery (or one image) without touching the rest of your site, and five ready to use CSS snippets. You will also see a live gallery on this page that is styled with the exact code from this tutorial.

What Is Custom CSS and When Should You Use It?

CSS (Cascading Style Sheets) is the code that controls how things look on a web page: colors, borders, sizes, spacing, and animations. Every WordPress theme and block plugin ships with its own CSS. “Custom CSS” simply means extra rules that you add yourself to change or extend that design.

Before you write any code, check the block settings first. Custom CSS is best for the things that settings cannot do. Here is a quick rule of thumb:

  • Use the block settings for columns, gaps between images, rounded corners, shadows, background color, padding, caption style, and hover effects. These are faster to change, work on every screen size, and will not break when you edit the gallery later.
  • Use custom CSS for creative extras, such as photo frames, tilted images, black and white filters, special hover animations, or hiding something on small screens only.

For example, you do not need CSS to round the corners of your images. Galleryberg has a setting for that, as shown in our guide on changing the image border radius of a gallery.

Galleryberg gallery block Styles tab with spacing, border, and shadow settings

Where to Add Custom CSS in WordPress

WordPress has a built-in place for site wide CSS, so you do not need an extra plugin. Where you find it depends on your theme.

Block Themes (Site Editor)

If you use a block theme such as Twenty Twenty-Five or Ollie, go to Appearance > Editor and open Styles. Click the three dot menu at the top of the Styles panel and choose Additional CSS. Paste your code into the box, then click Save.

Classic Themes (Customizer)

If your theme still uses the Customizer, go to Appearance > Customize > Additional CSS. Paste your code, check the live preview on the right, and click Publish.

Both options store your CSS in the database, so it is not lost when you update WordPress or your plugins. If you ever switch themes, remember to copy your CSS over to the new theme.

Method 1: Custom CSS for the Default WordPress Gallery Block

The Gallery block that comes with WordPress can be styled with CSS too. The trick is to give the gallery its own class name, so your code only affects that one gallery.

  1. Select your Gallery block in the editor. Click the outer gallery, not a single image (the block toolbar should say Gallery).
  2. In the block settings sidebar, open the Advanced panel.
  3. In Additional CSS class(es), type a name such as my-gallery. Use lowercase letters and dashes only, with no dot and no spaces.
  4. Save the post, then add your CSS in Additional CSS, starting each rule with .my-gallery.

For the default Gallery block, each image is a .wp-block-image figure inside the gallery. For example, this adds a thin white frame and a soft shadow to every image in that gallery:

.my-gallery .wp-block-image img {
  box-sizing: border-box;
  border: 6px solid #ffffff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

This works, but the default block has very few design settings, so you end up writing CSS even for basic things like borders and shadows. If you are not sure which block to use, our comparison of the Gutenberg Gallery block vs Galleryberg explains the differences.

Method 2: Custom CSS for Galleryberg Galleries

Galleryberg is a gallery block for the WordPress block editor. The core Galleryberg plugin includes four layouts (Tiles, Square, Masonry, and Justified), a lightbox, captions, hover effects, and full styling controls. Because it already handles the basics, your custom CSS can focus on the fun, creative parts.

Step 1: Build Your Gallery

Add the Galleryberg Gallery block to a post or page and pick your images. Choose a layout, set the number of columns, and adjust spacing, corners, and shadows in the sidebar. If you are new to the block, start with our guide on how to create an image gallery in WordPress.

Step 2: Give the Gallery a Class Name

Select the gallery block (click the outer gallery, or use the block breadcrumb at the bottom of the editor). In the settings sidebar, open Advanced and add a class name in Additional CSS class(es), for example my-gallery. Galleryberg prints this class on the gallery wrapper on the front end, so your CSS will only affect this gallery.

Step 3: Know Which Parts You Can Target

These are the main class names in a Galleryberg gallery. You can combine them with your own class name:

  • .my-gallery: the whole gallery wrapper.
  • .my-gallery .wp-block-galleryberg-image: each image card (a figure element).
  • .my-gallery img: the image itself.
  • .my-gallery figcaption: the caption of each image.
  • .layout-tiles, .layout-square, .layout-masonry, .layout-justified: added to the wrapper depending on the layout you picked.

Step 4: Add Your CSS

Open Additional CSS (see the section above), paste one of the snippets below, and replace my-gallery with your own class name if you picked a different one. Save, then view the page on the front end.

Step 5: Target a Single Image (Optional)

Want one photo to stand out? Click that image inside the gallery, open Advanced, and give it its own class, such as is-highlight. Galleryberg adds the class to that image card only, so you can style it separately (see snippet 5 below).

Live Example: A Polaroid Style Gallery Made With Custom CSS

The gallery below is a regular Galleryberg gallery using the Square layout, three columns, and the lightbox. The Polaroid frames, the slight tilt, and the hover animation all come from snippet 1 below. Hover over a photo to see it straighten up, and click it to open the lightbox.

5 Custom CSS Snippets for Your Galleryberg Galleries

Copy any of these into Additional CSS. Each one starts with .my-gallery, so it only changes the gallery that has that class name.

1. Polaroid Style Frames With a Playful Tilt

This is the code behind the live example above. It adds a white frame with a deeper bottom edge, tilts every other photo, and straightens the photo on hover. It looks best with the Square or Tiles layout and a gap of 24 pixels or more, so the frames do not touch.

.my-gallery .wp-block-galleryberg-image {
  background: #ffffff;
  padding: 10px 10px 30px;
  box-shadow: 0 4px 14px rgba(17, 41, 67, 0.18);
  transform: rotate(-2deg);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.my-gallery .wp-block-galleryberg-image:nth-child(even) {
  transform: rotate(2deg);
}

.my-gallery .wp-block-galleryberg-image:hover {
  transform: rotate(0) scale(1.04);
  box-shadow: 0 10px 24px rgba(17, 41, 67, 0.25);
  z-index: 3;
}

2. Black and White Until Hover

A classic look for portfolios and photography sites. All photos start in black and white and fade into full color when a visitor hovers over them.

.my-gallery img {
  filter: grayscale(100%);
  transition: filter 0.4s ease;
}

.my-gallery .wp-block-galleryberg-image:hover img {
  filter: grayscale(0);
}

Do not combine this snippet with the Duotone filter on the same images, because both use the CSS filter property and one will cancel the other.

3. A Colored Frame Around Every Image

Adds a solid color border inside the edge of each image. Change the color code to match your brand.

.my-gallery img {
  outline: 4px solid #112943;
  outline-offset: -4px;
}

Using outline with a negative offset draws the border inside the image, so your layout and spacing stay exactly the same.

4. Hide Captions on Small Screens

Captions are great on a desktop, but on a small phone screen they can cover most of a photo. This snippet hides them on screens narrower than 600 pixels, while the lightbox still shows the full image. Learn more about captions in our guide to adding captions to gallery images.

@media (max-width: 600px) {
  .my-gallery figcaption {
    display: none;
  }
}

5. Highlight One Special Image

First give one image the class is-highlight (Step 5 above). Then use this snippet to add a gold frame to that image only.

.my-gallery .is-highlight img {
  outline: 4px solid #e2b04a;
  outline-offset: -4px;
}

Tips for Writing Gallery CSS That Works

  • Always start with your class name. Writing .my-gallery img instead of just img keeps your changes inside one gallery and away from your logo, blog images, and other blocks.
  • Use the settings for columns and spacing. Galleryberg writes the column count and gap directly on the gallery, and adjusts columns per device. Change these in the block settings instead of with CSS, so your gallery stays responsive.
  • Check every screen size. Preview your page on desktop, tablet, and phone. Our guide to responsive galleries in WordPress has more tips.
  • Clear your cache. If you use a caching plugin or a CDN, clear the cache after you save your CSS, or you may still see the old design.
  • Keep animations light. Simple transforms and filters are smooth on most devices. Avoid heavy effects on very large galleries, and keep your images optimized. See our tips on making galleries load faster.
  • Respect reduced motion. Some visitors turn off animations in their device settings. You can wrap transitions in a prefers-reduced-motion media query, as we did in the live example.

Frequently Asked Questions

Will I lose my custom CSS when I update Galleryberg or WordPress?

No. CSS added in Additional CSS is saved in your WordPress database, not inside the plugin or theme files, so updates do not remove it. The only time you need to act is when you switch to a different theme. In that case, copy your CSS into the new theme’s Additional CSS box.

Why is my custom CSS not working?

Can I add custom CSS to just one gallery?

Do I need to know CSS to customize my galleries?

Conclusion

Custom CSS lets you give your WordPress galleries a look that is truly your own, from Polaroid frames to black and white hover effects. The key is to keep it simple: use the block settings for layout, columns, and spacing, give your gallery a class name, and add only the extra styling you need in Additional CSS.

With the core Galleryberg plugin handling layouts, the lightbox, captions, and styling, you will rarely need more than a few lines of code. Pick one of the snippets above, try it on your next gallery, and see how a small touch of CSS can make your photos stand out.