Breakpoint Classes for Bricks
Adds the active Bricks breakpoint to the body element as CSS classes and a data attribute, and keeps them up to date as the window is resized. For your own CSS and JavaScript.
Version: 0.1.0 · Requires: Bricks 2.4 or newer · See the live demo
Installation
- Upload and activate the plugin. Bricks must be active, and the theme must call
wp_body_open(). - There are no settings. The classes are on the body straight away.
What is added to the body
bpc-{key}— the active breakpoint only, for examplebpc-mobile_portrait.bpc-in-{key}— every breakpoint whose styles currently apply, following the Bricks cascade. On a desktop-first site,.bpc-in-tablet_portraitmeans tablet portrait and smaller.data-bpc="{key}"— the active breakpoint’s key.
{key} is the key Bricks gives each breakpoint: desktop, tablet_portrait, mobile_landscape, mobile_portrait, or your own custom ones. Paused breakpoints are skipped.
JavaScript event
bpc:breakpoint is fired on document when the active breakpoint changes. event.detail has from, to and key. It does not fire for the first breakpoint on page load, so read data-bpc on the body for that.
document.addEventListener('bpc:breakpoint', function (e) {
console.log(e.detail.from, '→', e.detail.to);
});
Quick start
- Activate the plugin.
- Add the class
decorto an element and add this to your custom CSS:.bpc-in-tablet_portrait .decor { display: none; } - Resize the window. The element disappears at tablet portrait and smaller.
How it works
- The media queries are built the way Bricks builds its own: breakpoints wider than the base use min-width, narrower ones use max-width. This covers desktop-first sites, mobile-first sites and a base in the middle.
- The active breakpoint is the matching one furthest from the base.
- A small inline script at the top of the body sets the classes before content paints.
Tips
- Use
bpc-in-*for “this size and smaller” rules, andbpc-{key}for rules that apply at exactly one breakpoint. - Prefer a normal Bricks responsive style when you only need to change one element’s own style. These classes are for rules that reach across elements, or for scripts.
Troubleshooting
- No classes on the body — the theme may not call
wp_body_open(), or an optimiser is delaying inline scripts. Exclude#nl-bpcin the optimiser’s settings. - The classes are not in the page source — expected. JavaScript sets them, so the HTML sent by the server does not contain them.
- The event did not fire on load — expected. Read
data-bpcfor the first value.
For developers
nl_bpc/configfilters the breakpoint list before it is printed.
Known limits
- Not run in the builder’s main window. The preview canvas keeps the classes up to date.
- The script carries attributes that ask Cloudflare Rocket Loader, WP Rocket, LiteSpeed Cache and SG Optimizer not to delay it. Other optimisers may need it excluded by hand.