• Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A PostCSS plugin and Javascript runtime combination, which allows you to write container queries in your CSS the same way you would write media queries.


Container Query

npm version build Coverage Status Greenkeeper badge

SauceLabsTestMatrix

A PostCSS plugin and Javascript runtime combination, which allows you to write container queries in your CSS the same way you would write media queries.

πŸ•Ά Uses Resize Observer: Uses the native ResizeObserver implentation when available, and falls back to a polyfill to detect size changes. If you use Chrome, you can test how performant the plugin is with the native implementation. (Shipped in v64.)

πŸ“¦ Bundler Support: Built with bundlers in mind, like Parcel, webpack and Browserify.

🎲 Component Support: Built with component-based libraries in mind, like React, Vue, Ember and Angular.

πŸ“„ Intuitive Syntax: Uses at-rules, the same way @media queries do: @container (...) { ... }

πŸŽ‰ Container Units: rh, rw, rmin, rmax, which are relative to the container element's width and / or height. (Same way viewport units are relative to the viewport's size.)

⚑ Cross-Browser Testing: Sponsored with ❀️ by SauceLabs.

Introduction

Container queries are very similar to media queries; they allow you to apply styles to elements when certain conditions are met.

The key difference is that while media queries are relative to the viewport's size, container queries are relative to a container element's size.

Thanks to this trait, you can have multiple instances of the same container element, all of which changes its own- and its childrens' styles based on the containing element's size.

What is a Container?

A container is just an HTML element, which may (or may not) contain other elements.

You may want to think of them as "Components" (React) or "Blocks" (BEM).

Demos

The best way to understand the concept, is if you try it for yourself.

Note that because these demos are hosted on CodeSandbox, where webpack or PostCSS cannot (currently) be configured, styles are simply imported as strings and processed in the browser. (Using @zeecoder/cq-demo-utils.)

While this works for demo purposes, in a real application it is strongly recommended to process styles build-time, as later described in this documentation.

Documentation

What's Next?

Ideas for Enhancement

Support

Please consider supporting me if you like what I do on my Patreon page.

Thoughts on Design

In case you're wondering about the tool's design, here is a list of goals I had in mind with the initial implementation:

  • Should be tested,
  • Should use containers instead of elements (As opposed to other "element-query" libraries.),
  • Should use a media query-like syntax so that it's familiar and easy to use,
  • Should uses PostCSS for preprocessing, instead of having a runtime parser,
  • Should be easy enough to use, but a transpiling step would be assumed (Due to the reason above.),
  • Should use ES modules, so it plays nicely with bundlers (Parcel, webpack Browserify, etc.) and with Component-oriented UI libraries (React, Vue, etc.),
  • Should work best with component naming methodologies, like BEM or SUIT, but should also work without them.

WICG

The WICG dived into 2018 with renewed effort to make native Container Queries a reality in browsers.

If you're interested in how things are progressing, please feel free to visit the following links, where the disussions are happening:

Related

Alternatives

If you like the idea of container queries, but are not particularly convinced by this solution, then I encourage you to look at these alternatives:

License

MIT

More Repositories

1

use-resize-observer

A React hook that allows you to use a ResizeObserver to measure an element's size.
TypeScript
639
star
2

react-resize-observer

A React component that allows you to use a ResizeObserver to measure an element's size.
JavaScript
3
star
3

vk-elozetes

An RPGMaker2k3 game I made back in the day. It's not in a playable state ATM, but I saved it anyway. :)
2
star
4

z-modifier

Handles BEM-inspired modifier classes on a DOM element.
JavaScript
1
star
5

z-file-validator

Validates a file input based on the provided configurations.
JavaScript
1
star
6

postcss-equal-height

PostCSS plugin + JS runtime combination to ensure equal height of certain elements.
1
star
7

cq-demo-utils

Utilities for online container query demos.
JavaScript
1
star
8

z-image-preloader

Image preloading with promise support.
JavaScript
1
star
9

common-js-plus-legacy

POC of a way to use CommonJS with legacy applications.
JavaScript
1
star
10

react-london-container-queries

JavaScript
1
star
11

vk-elso-epizod

An RPGMakerXP game I never finished. :( Added the RVPacker tool in case I ever started it again with other programmers.
Ruby
1
star
12

z-trans

A simple translator that works similar to the Symfony Translator Component.
JavaScript
1
star
13

exporting-css-loaders

An experiment with webpack loaders that operate on css and contribute to a final exported module.
JavaScript
1
star
14

clam

[ABANDONED] Clam is a frontend toolset. It provides CommonJS-style modules to help develop and organise frontend code. Modules developed with the use of Clam will be maintainable and extendable.
HTML
1
star