Skip to content

Breakpoints

A custom resize event based on the “breakpoints”, or media queries, in the CSS on the page.

  • It reads the min-width media queries from your stylesheets and fires a switch event on window when the window moves from one breakpoint to another.
  • event.detail is { name, size }: the name of the breakpoint and its width in pixels.
  • Each breakpoint is named bp-{width}, for example bp-768. bp-0 always exists: it is the width below the smallest query.
  • switch is also fired once when the page loads, so you know the current breakpoint without resizing.
  • The <script> build starts it for you, so there is nothing to call. With modules, create one Breakpoints and call init().

Resize the window. The current breakpoint is shown below and updates each time the window crosses one.

HTML
<p>Resize the window. The current breakpoint is shown below and updates each time the window crosses one.</p>
<div class="current-bp"></div>
<style>
/* The breakpoints are the min-width media queries in your CSS: here 576, 768, 992 and 1200. */
.current-bp { border-left: 4px solid var(--sl-color-accent); }
@media (min-width: 576px) { .current-bp { border-left-width: 8px; } }
@media (min-width: 768px) { .current-bp { border-left-width: 12px; } }
@media (min-width: 992px) { .current-bp { border-left-width: 16px; } }
@media (min-width: 1200px) { .current-bp { border-left-width: 20px; } }
</style>
JavaScript
// `switch` is fired on window once when the page loads, and again each time the window
// crosses a breakpoint. event.detail is { name, size }.
// The script build starts Breakpoints for you, so there is nothing else to set up.
var currentBp = document.querySelector('.current-bp');
window.addEventListener('switch', function (e) {
currentBp.textContent = JSON.stringify(e.detail);
});

The breakpoints here come from the <style> in the HTML above. The rest of this site’s CSS writes its media queries as (width >= 50em), which is not read (see the notes).

Both are fired on window.

Event Fired when event.detail
switch the page loads, and each time the breakpoint changes { name, size }
debounce.resize the window was resized, at most once per frame {}
window.addEventListener('switch', function (event) {
console.log(event.detail.name); // name of the breakpoint
console.log(event.detail.size); // width in px of the breakpoint
});
  • Only min-width queries are read, and the number in them is taken as pixels: min-width: 50em becomes bp-50. The range syntax, (width >= 768px), is not read at all.
  • Only the top-level @media rules of each stylesheet are looked at. A media query inside @layer, @supports or another @media is not seen.
  • The CSS is read once, when the DOM is ready, so styles in the <head> and the <body> are both seen, as long as an external stylesheet has loaded by then. Styles added later are not seen. To read them, call destroy() and then init().
  • A stylesheet from another origin that the browser does not let the script read is skipped.
  • Run only one. The <script> build starts one itself, and a second one makes every event fire twice.
  • destroy() removes the listeners and stops the events. init() starts it again.

See the generated Breakpoints class reference for every method.

A checklist. An unticked item is not built, and not promised: it says what is missing today. A ticked item has been built.

  • Sizes in JavaScript. Giving the breakpoints to Breakpoints instead of reading them from the CSS, which a stylesheet from another origin can block.
  • More queries. Only top-level min-width queries are read, as pixels. Not read: em, the range syntax ((width >= 768px)), max-width, and queries inside @layer, @supports or another @media.
  • matchMedia. Letting the browser say which query matches, instead of comparing the window width.
  • Stylesheets added later. The CSS is read once. Watching for new stylesheets would keep it up to date.
  • One instance. Two instances fire every event twice, and loading breakpoints.js together with ml.js starts two.
  • Asking. A current breakpoint to read, and a way to ask whether the window is at least a size.