Platform Optimization

Load WordPress gallery styles only where they are needed

Use a gallery, a text page and a template part to test the theme’s actual CSS delivery.

Two page outlines share a base stylesheet, while a second stylesheet connects only to the page with a grid of gallery images.

A WordPress gallery can have its own stylesheet, delivered on the public site when WordPress renders a Gallery block. Move one group of gallery rules out of the shared theme bundle, then test a page with a gallery and a page without one. Whether the second page avoids those rules depends on the active theme’s block asset policy and on what the final HTML contains.

Find a rule worth moving

Suppose your theme adds numbered captions, custom gaps and a wide layout to photo essays. The selectors for those features sit in the main theme stylesheet alongside navigation and typography. A visitor opening a text interview receives the gallery rules even though nothing on that page can use them. This is a reasonable candidate for a split. One line that sets the color of all captions is a poor starting point if image blocks, tables and galleries all depend on it.

Start with a rendered gallery on a staging copy. Inspect a caption and a gallery item in the browser’s Styles panel. Identify the exact selector, the winning declaration, its source file and any custom properties it consumes. Compare with a standalone image caption. A selector that happens to match an element inside a gallery can still be essential outside it. Move rules whose ownership you can demonstrate. Keep shared variables, resets and base typography in their current place.

Read the build inputs as well as the CSS sent to the browser. Editing an emitted file directly can produce a tempting result that disappears on the next build. If a Sass entry imports the gallery partial, change that entry and create a separate output for the partial. Keep the old configuration in version control. Capture the original two responses before changing either the theme or the optimizer. The WordPress template performance guide gives a broader method for selecting comparable visitor states.

Attach the file to the right block

A WordPress block has a registered name. The core Gallery block uses core/gallery. An image inside it can itself be a core/image block. Attaching gallery-specific styles to the image name would broaden their reach to pages containing ordinary images. Conversely, a plugin’s slider can generate gallery-like markup without rendering the core Gallery block. Inspect the editor’s block structure and the template or pattern that places it. Use the name of the block that owns the rules.

For a theme you maintain, register the stylesheet during setup. The following example assumes that assets/css/gallery.css exists in the active theme or its parent. Replace the example handle with a unique name for your theme. Treat the version as a release identifier. Change it when you change the CSS, subject to how your deployment caches versioned URLs.

php
add_action( 'after_setup_theme', function () {
    wp_enqueue_block_style( 'core/gallery', array(
        'handle' => 'journal-gallery-layout',
        'src'    => get_theme_file_uri( 'assets/css/gallery.css' ),
        'path'   => get_theme_file_path( 'assets/css/gallery.css' ),
        'ver'    => '1.0.0',
    ) );
} );

The URL identifies a browser-accessible file. The path identifies a file on the server and gives WordPress the option of inlining the stylesheet. These are distinct values even when the filename at the end looks identical. A server path in the src field will not magically become a public URL. Preserve the registration in the theme that owns the CSS, including a child theme when that is where your customization lives. The WordPress reference for per-block stylesheets describes the arguments and when WordPress enqueues the file.

Check what reaches three different pages

Use a gallery page that shows your custom design. For a negative control, create or choose a page using the same template and normal images but no Gallery block. Add a third page where a gallery comes from a template part or synced pattern. A check against the post’s raw text could miss that third case. A header or footer can also put a block on more pages than the editor of a single post would suggest.

On each page, inspect the delivered HTML and the network log with the browser cache disabled. Search for the distinctive CSS selector as well as for the filename. A style element in the document can contain the exact rules even when there is no request for gallery.css. If an optimizer combines styles, inspect the returned bundle or temporarily disable combining on staging to separate WordPress behavior from the optimizer’s output. Test the normal configuration again before drawing a production conclusion.

Page Expected evidence when loading on demand works
Gallery in the article Its scoped rules are present and the captions render correctly
Ordinary images only Shared image styles remain and the gallery rules are absent
Gallery in a template part Its rules arrive although the article itself has no Gallery block

An asset registered for a block is not necessarily conditional on every site. WordPress can enqueue block styles on render when the active configuration enables on-demand loading. With a different policy the styles can be enqueued on pages without that block. If the selector is present on the negative control, inspect the old theme bundle first. Then check for another registration under a different handle, a rendered Gallery elsewhere in the template and the active block asset policy. Changing the policy for every block would be a wider experiment than moving this stylesheet.

Catch cascade and cache surprises

A design can look right on first load while still depending on the old copy of its CSS. Remove the original imports in the same test build. Inspect the winning rule once more in the browser. If the spacing looks right only with both files present, compare the order and specificity of the competing rules. Avoid adding !important just to make the screenshot match. A style dependency might need an explicit handle, or a shared rule might belong in the base stylesheet after all.

Check the narrow layout, wide layouts, galleries with and without captions, and keyboard focus if items are links. Switch to a right-to-left locale if the theme supports one. Check the editor preview, then revisit a public permalink as a signed-out visitor. The editor and the public page are different contexts. A successful preview establishes that a person can edit the gallery; it does not establish what a cached public response sends to readers.

Now test a warm visit after navigating from the gallery page to the text page and in the opposite direction. External CSS can be reused from a browser cache, whereas inlined CSS is part of each HTML response that carries it. On a CDN, an old HTML document can continue referring to the previous asset after a release. Keep that asset available through the deployment transition. If the file is missing, repair the release or revert the change before attributing a layout regression to the browser.

Decide whether the split earns its place

Record the transferred CSS and HTML bytes on both templates under the same test conditions. Include the inline contribution to the document and identify whether an extra request appeared on the gallery page. Repeat the before-and-after runs on the same network profile and viewport. If a different image becomes the largest visible element, record that change; it makes an isolated LCP comparison harder to interpret. A smaller stylesheet on the text page is a measurable delivery change even if a timing difference is too small to distinguish from normal variation.

Keep the split when readers of unrelated pages avoid a meaningful body of rules and the gallery retains its correct first render, accessibility and cache behavior. If every landing page includes the gallery in a global template part, the extra separation may deliver little. If the file is tiny and changes alongside the main stylesheet on every release, maintaining a second asset and reviewing its ordering can cost more than it saves. Write down the pages tested, their delivered rules and the release that produced them. That gives the next person modifying the gallery a concrete way to repeat the decision.

Make the two control pages part of the theme’s release checks. A later designer might insert a gallery in the shared footer or replace the core block with a custom one. In the first case the negative control now legitimately needs the styles; in the second, the positive control can lose them. Updating the control pages alongside that design change keeps future tests attached to the structure visitors actually receive.

Related guides