container-query-polyfill
Version:
A tiny polyfill for [CSS Container Queries][mdn], weighing about 1.6kB brotli’d. It transpiles CSS code on the client-side and implements Container Query functionality using [ResizeObserver] and [MutationObserver].
44 lines (28 loc) • 2.05 kB
Markdown
A tiny polyfill for [CSS Container Queries][mdn], weighing about 1.6kB brotli’d. It transpiles CSS code on the client-side and implements Container Query functionality using [ResizeObserver] and [MutationObserver].
Ideally, the polyfill is only loaded if the browser doesn’t support Container Queries natively. In a modern setup with a bundler that uses ES modules, the following snippet should work:
```js
const supportsContainerQueries = "container" in document.documentElement.style;
if (!supportsContainerQueries) {
import("container-query-polyfill");
}
```
If you are in a legacy setup (or just want to prototype quickly), there’s also an IIFE version that you can include using a `<script>` tag:
```html
<script src="https://unpkg.com/container-query-polyfill/cqfill.iffe.min.js"></script>
```
The polyfull should work in all modern browsers. Chrome 88+, Firefox 78+ and Safari 14+.
To keep the polyfill performant, small and maintainable, I have make certain tradeoffs with full feature parity of the Container Query spec. I have listed these tradeoffs below.
(These decisions _can_ be revisited if they pose a significant hurdle and there is a good way to implement them. Please open an issue!)
- Container Queries will not work when nested inside a Media Query. For now, the polyfill only supports top-level CQs.
- Container query thresholds can only be specified using pixels.
- Due to the nature of CORS, the polyfill only attempts to handle same-origin and inline stylesheets. Cross-origin stylesheets are ignored, regardless of CORS headers.
- Don’t do weird interspersed comments, okay? Like `@container /* here’s a comment! */ (min-width: 1px) { ... }`. Just don’t.
---
License Apache-2.0
[]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries
[]: https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver
[]: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver