Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Minimalist Container Queries</h1>

<p>By <a href="https://scottjehl.com">Scott Jehl</a></p>

<p>This <code>c-q</code> custom element enables very simple "min-width" container queries, allowing you to adjust its CSS based on its own rendered width instead of viewport width.</p>

<h2>Example:</h2>


<c-q>
  <p>resize me!!</p>
</c-q>

<h2>Usage</h2>

<p>To use, set a given <code>c-q</code>'s <code>--breakpoints;</code> custom CSS property to one or more pixel-based breakpoints that the JS should be aware of, like this:</p>
<pre><code>c-q { --breakpoints: "400 600 800"; }</code></pre>
<p>One or more of these values will appear in the element's <code>data-min-width</code> attribute when they are greater or equal to the element's rendered width. You can style the element based on their presence using the <code>~=</code> attribute selector, like this: </p>
<pre><code>c-q[data-min-width~="400"] { background: green; }</code>. </p></pre>




<style>
/* demo styles */
  body {
  font-family: sans-serif;
  margin: 3vw;
}
code {
  padding: .3em .5em;
  font-size: 1em;
  background: #333;
  color: #fff;
  border-radius: .2em;
}
p {
  max-width: 45em;
  line-height: 1.5;
}
c-q {
  resize: both;
  display: block;
  overflow: auto;
  height: 50px;
  width: 300px;
  padding: 10px;
}
</style>
              
            
!

CSS

              
                c-q {
  --breakpoints: "400 600 800";
  background: tan;
}
c-q[data-min-width~="400"] {
  background: green;
}
c-q[data-min-width~="600"] {
  background: blue;
}
c-q[data-min-width~="800"]  {
  background: red;
}
              
            
!

JS

              
                // Minimalist Container Queries
// 2020 @scottjehl, @filamentgroup / MIT License
class CQ extends HTMLElement {
  constructor() {
    super();
    this._init = this._init.bind(this);
    this._observer = new MutationObserver(this._init);
  }
  connectedCallback() {
    if (this.children.length) {
      this._init();
    }
    this._observer.observe(this, { childList: true });
  }
  _init() {
    var self = this;
    this._rObserver = new ResizeObserver((entries) => {
      for (const entry of entries) {
        self.setActiveBPs(entry);
      }
    });
    this._rObserver.observe(this);
  }

  setActiveBPs(entry) {
    var bps = window.getComputedStyle(entry.target).getPropertyValue("--breakpoints");
    var activeBPs = [];
    if (bps) {
      bps = bps.replace('"', "");
      bps.split(" ").forEach((bp) => {
        var bpnum = parseFloat(bp);
        if (entry.contentRect.width >= bpnum) {
          activeBPs.push(bpnum);
        }
      });
    }
    entry.target.setAttribute("data-min-width", activeBPs.join(" "));
  }
}
customElements.define("c-q", CQ);

              
            
!
999px

Console