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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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>Container Queries using JS-in-CSS</h1>

<h2>Width Queries</h2>

<h3>min-width</h3>
<div class=minwidth data-drag=horizontal>class="minwidth"</div>

<h3>max-width</h3>
<div class=maxwidth data-drag=horizontal>class="maxwidth"</div>

<h2>Height Queries</h2>

<h3>min-height</h3>
<div class="minheight" data-drag=vertical>class="minheight"</div>

<h3>max-height</h3>
<div class="maxheight" data-drag=vertical>class="maxheight"</div>

<h2>Quantity Queries</h2>

<h3>min-characters on block elements</h3>
<p>(Use keyboard)</p>
<div class="mincharacters" contenteditable>class="mincharacters"</div>

<h3>min-characters on form inputs</h3>
<p>(Use keyboard)</p>
<input type=text class="mincharacters-input" value='class="mincharacters-input"'>
<textarea class="mincharacters-textarea">class="mincharacters-textarea"</textarea>

<h3>max-characters on block elements</h3>
<p>(Use keyboard)</p>
<div class="maxcharacters" contenteditable>class="maxcharacters"</div>

<h3>max-characters on form inputs</h3>
<p>(Use keyboard)</p>
<input type=text class="maxcharacters-input" value='class="maxcharacters-input"'>
<textarea class="maxcharacters-textarea">class="maxcharacters-textarea"</textarea>


<h3>min-children</h3>
<button data-button="semiflat" onclick="var img=document.createElement('img');img.src='http://staticresource.s3.amazonaws.com/user.png';document.querySelector('.minchildren').appendChild(img);EQCSS.apply()">Add Child</button>
<div class="minchildren">
<img src=http://staticresource.s3.amazonaws.com/user.png style=width:50px>
</div>

<h3>max-children</h3>
<button data-button="semiflat" onclick="var img=document.createElement('img');img.src='http://staticresource.s3.amazonaws.com/user.png';document.querySelector('.maxchildren').appendChild(img);EQCSS.apply()">Add Child</button>
<div class="maxchildren">
<img src=http://staticresource.s3.amazonaws.com/user.png style=width:50px>
</div>

<h2>Scroll queries</h2>

<h3>min-scroll-y</h3>
<div class="min-scroll-y" onscroll=JSinCSS()>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>

<h3>max-scroll-y</h3>
<div class="max-scroll-y" onscroll=JSinCSS()>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>

<h3>min-scroll-x</h3>
<div class="min-scroll-x" onscroll=JSinCSS()>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>

<h3>max-scroll-x</h3>
<div class="max-scroll-x" onscroll=JSinCSS()>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>

<h2>Aspect queries</h2>

<h3>orientation</h3>
<div class="square" style="width: 100px; height: 100px;">100 &times; 100</div>
<div class="portrait" style="width: 100px; height: 200px;">100 &times; 200</div>
<div class="landscape" style="width: 200px; height: 100px;">200 &times; 100</div>

<h3>min-aspect-ratio</h3>
<div class="minaspectratio" data-drag=both>class="minaspectratio"</div>

<h3>max-aspect-ratio</h3>
<div class="maxaspectratio" data-drag=both>class="maxaspectratio"</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                window.addEventListener('load', JSinCSS)
window.addEventListener('resize', JSinCSS)
window.addEventListener('input', JSinCSS)
window.addEventListener('click', JSinCSS)

function JSinCSS() {

  var tag = document.querySelector('#JSinCSS')

  if (!tag) {

    tag = document.createElement('style')
    tag.id = 'JSinCSS'
    document.head.appendChild(tag)

  }

  tag.innerHTML = `

      /* Min-width */
      ${containerQuery('.minwidth', el => el.offsetWidth >= 300, `
        .minwidth {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Max-width in px */
      ${containerQuery('.maxwidth', el => el.offsetWidth <= 300, `
        .maxwidth {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Min-height in px */
      ${containerQuery('.minheight', el => el.offsetHeight >= 200, `
        .minheight {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Max-height in px */
      ${containerQuery('.maxheight', el => el.offsetHeight <= 200, `
        .maxheight {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Min-characters on block elements */
      ${containerQuery('.mincharacters', el => el.textContent.length >= 35, `
        .mincharacters {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Min-characters on input */
      ${containerQuery('.mincharacters-input', el => el.value.length >= 35, `
        .mincharacters-input {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Min-characters on textarea */
      ${containerQuery('.mincharacters-textarea', el => el.value.length >= 35, `
        .mincharacters-textarea {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Max-characters */
      ${containerQuery('.maxcharacters', el => el.textContent.length <= 35, `
        .maxcharacters {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Max-characters on input */
      ${containerQuery('.maxcharacters-input', el => el.value.length <= 35, `
        .maxcharacters-input {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Max-characters on textarea */
      ${containerQuery('.maxcharacters-textarea', el => el.value.length <= 35, `
        .maxcharacters-textarea {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Min-children */
      ${containerQuery('.minchildren', el => el.children.length >= 5, `
        .minchildren {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Max-children */
      ${containerQuery('.maxchildren', el => el.children.length <= 5, `
        .maxchildren {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Min-scroll-y */
      ${containerQuery('.min-scroll-y', el => el.scrollTop >= 50, `
        .min-scroll-y {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Max-scroll-y */
      ${containerQuery('.max-scroll-y', el => el.scrollTop <= 50, `
        .max-scroll-y {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Min-scroll-x */
      ${containerQuery('.min-scroll-x', el => el.scrollLeft >= 50, `
        .min-scroll-x {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Max-scroll-x */
      ${containerQuery('.max-scroll-x', el => el.scrollLeft <= 50, `
        .max-scroll-x {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}


      /* Square Orientation */
      ${containerQuery('.square', el => el.offsetWidth == el.offsetHeight, `
        .square {
          background: orchid;
        }
      `)}

      /* Portrait Orientation */
      ${containerQuery('.portrait', el => el.offsetWidth < el.offsetHeight, `
        .portrait {
          background: darkturquoise;
        }
      `)}

      /* Landscape Orientation */
      ${containerQuery('.landscape', el => el.offsetWidth > el.offsetHeight, `
        .landscape {
          background: greenyellow;
        }
      `)}


      /* Min-aspect ratio */
      ${containerQuery('.minaspectratio', el => el.offsetWidth / el.offsetHeight > 16/9, `
        .minaspectratio {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

      /* Max-aspect-ratio */
      ${containerQuery('.maxaspectratio', el => el.offsetWidth / el.offsetHeight < 16/9, `
        .maxaspectratio {
          background: greenyellow;
          border-color: limegreen;
        }
      `)}

  `

}

function containerQuery(selector, test, stylesheet) {

  var tag = document.querySelectorAll(selector)
  var style = ''
  var count = 0

  for (var i=0; i<tag.length; i++) {

    var attr = (selector+test).replace(/\W+/g, '')

    if (test(tag[i])) {

      tag[i].setAttribute(`data-${attr}`, count)

      var css = stylesheet.replace(/\$this/g, `[data-${attr}="${count}"]`)

      style += css

      count++

    } else {

      tag[i].setAttribute(`data-${attr}`, '')

    }

  }

  return style

}
              
            
!
999px

Console