EQCSS

A CSS Extension for Element Queries & More

What are Element Queries?

Element queries are a new way of thinking about responsive web design where the responsive conditions apply to elements on the page instead of the width or height of the browser.

Unlike CSS @media queries, @element Queries are aware of more than just the width and height of the browser, you can write responsive conditions for a number of different situations like how many characters of text or child elements an element contains.

Another concept that element queries brings to CSS is the idea of ‘scoping’ your styles to one element in the same way that JavaScript functions define a new scope for the variables they contain.

How to use EQCSS

EQCSS is a JavaScript plugin that lets you write element queries inside CSS today. With performance in mind, this plugin is written in pure JavaScript, so it doesn't require jQuery or any other libraries on your page in order to function - add EQCSS.js to you HTML and you’re ready to get started!

View EQCSS on Github View EQCSS on NPM

Once you have downloaded a copy of EQCSS you will need to add it to every HTML page where you will be using element queries. It’s best to add a <script> after your content, before the end of your <body> tag.

<script src=EQCSS.js></script>

If you need to support IE8 there is an optional polyfill available that adds @media queries and @element queries to IE8. Add this file to your HTML before where you added EQCSS:

<!--[if lt IE 9]><script src="EQCSS-polyfills.js"></script><![endif]-->

CDN Hosted Links

EQCSS is also hosted on CDNjs and you can use the version hosted there for your projects: https://cdnjs.com/libraries/eqcss:

https://cdnjs.cloudflare.com/ajax/libs/eqcss/1.2.1/EQCSS-polyfills.min.js
https://cdnjs.cloudflare.com/ajax/libs/eqcss/1.2.1/EQCSS.min.js

Writing Element Queries

Now that you have EQCSS added to your HTML you’re ready to write element queries. There are two ways you can add EQCSS to your site: the easiest way is by writing them inside your CSS either in a <style> or <link> in your HTML, or by isolating your EQCSS styles in a custom script type.

You can also link to EQCSS hosted in external files with either .css or .eqcss extensions.

Using the EQCSS script type

While not necessary, if you choose to isolate your EQCSS from your CSS you can store it in external .eqcss files and link them using a <script> tag like this:

<script type="text/eqcss" src=styles.eqcss></script>

And you can include EQCSS scripts inline in your page like this as well:

<script type="text/eqcss">

  /* EQCSS goes here */

</script>

Running EQCSS

By default the plugin execute once when the content loads, and also whenever it detects browser resize (similar to @media queries). The EQCSS.apply() function can be called manually on other events too, like keyup or clicks, or even when specific elements are interacted with.

Designing with Element Queries

Element Queries have the following format:

@element {selector} and {condition} [ and {condition} ]* { {css} }

Element Query Conditions

Use your mouse (or drag on the handles) to resize the blocks.

min-width in px

class="minwidthpixels"
@element ".minwidthpixels" and (min-width: 500px) {
  .minwidthpixels {
    background: gold;
  }
}

min-width in %

class="minwidthpercents"
@element ".minwidthpercents" and (min-width: 50%) {
  .minwidthpercents {
    background: gold;
  }
}

max-width in px

class="maxwidthpixels"
@element ".maxwidthpixels" and (max-width: 500px) {
  .maxwidthpixels {
    background: gold;
  }
}

max-width in %

class="maxwidthpercents"
@element ".maxwidthpercents" and (max-width: 50%) {
  .maxwidthpercents {
    background: gold;
  }
}

min-height in px

class="minheightpixels"
@element ".minheightpixels" and (min-height: 200px) {
  .minheightpixels {
    background: gold;
  }
}

min-height in %

class="minheightpercents"
@element ".minheightpercents" and (min-height: 50%) {
        .minheightpercents {
          background: gold;
        }
      }

max-height in px

class="maxheightpixels"
@element ".maxheightpixels" and (max-height: 200px) {
  .maxheightpixels {
    background: gold;
  }
}

max-height in %

class="maxheightpercents"
@element ".maxheightpercents" and (max-height: 50%) {
  .maxheightpercents {
    background: gold;
  }
}

min-characters on block elements

(Use keyboard)

class="mincharacters"
@element ".mincharacters" and (min-characters: 30) {
  .mincharacters {
    background: gold;
  }
}

max-characters on block elements

class="maxcharacters"
@element ".maxcharacters" and (max-characters: 30) {
  .maxcharacters {
    background: gold;
  }
}

min-characters on form inputs

@element ".mincharacters-input" and (min-characters: 30) {
  .mincharacters-input {
    background: gold;
  }
}
@element ".mincharacters-textarea" and (min-characters: 30) {
  .mincharacters-textarea {
    background: gold;
  }
}

max-characters on form inputs

@element ".maxcharacters-input" and (max-characters: 30) {
  .maxcharacters-input {
    background: gold;
  }
}
@element ".maxcharacters-textarea" and (max-characters: 30) {
  .maxcharacters-textarea {
    background: gold;
  }
}

min-children

class="minchildren"

@element ".minchildren" and (min-children: 5) {
  .minchildren {
    background: gold;
  }
}

max-children

class="maxchildren"

@element ".maxchildren" and (max-children: 5) {
  .maxchildren {
    background: gold;
  }
}

min-lines

class="minlines"
@element ".minlines" and (min-lines: 5) {
  .minlines {
    background: gold;
  }
}

max-lines

class="maxlines"
@element ".maxlines" and (max-lines: 5) {
  .maxlines {
    background: gold;
  }
}

min-scroll-x

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.

@element ".min-scroll-x" and (min-scroll-x: 50%) {
  .min-scroll-x {
    background: gold;
  }
}

max-scroll-x

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.

@element ".max-scroll-x" and (max-scroll-x: 50%) {
  .max-scroll-x {
    background: gold;
  }
}

min-scroll-y

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.

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.

@element ".min-scroll-y" and (min-scroll-y: 50%) {
  .min-scroll-y {
    background: gold;
  }
}

max-scroll-y

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.

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.

@element ".max-scroll-y" and (max-scroll-y: 50%) {
  .max-scroll-y {
    background: gold;
  }
}

Scoped queries ($this)

class="scoped"
class="scoped"
@element ".scoped" and (min-width: 300px) {

  /* Only set a gold background to the elements matching the query */
  $this {
    background: gold;
  }
}

Scoped queries + multiple conditions

class="scoped-multiple"
class="scoped-multiple"
@element ".scoped-multiple" and (min-width: 350px) and (max-width: 450px) {
  $this {
    background: gold;
  }
  $this > b {
    color: red
  }
  $this:hover {
    background: orange;
  }
}

Multiple conditions

class="multiple-conditions"
@element ".multiple-conditions" and (min-width: 50%) and (min-height: 150px) {
  .multiple-conditions {
    background: gold;
  }
}

Comments

class="comments"
/* comment before */
@element ".comments" and (max-width: 400px) {
  /* comment at the beginning */
  .comments {
    background: gold;
  }
  /* comment at the end */
}
/* comment after */

Meta-Selectors

The following selectors can be used inside element queries. The $this meta-selector selects the same element(s) on the page that meet the query condition. The $parent selector refers to the parent of the element(s) on the page that meet the repsonsive condition. There are selectors for $prev and $next as well, and $root which always refers to the HTML document.

Alternatively, for SCSS compatibility instead of using $ you can also format meta-selectors prefixed by eq_. These are eq_this, eq_parent, eq_root, eq_prev, and eq_next

$this selector

class="this-selector"
@element ".this-selector input" and (min-characters: 5) {
  $this,
  $this:focus {
    border-color: red;
  }
}

$parent selector

class="parent-selector"
@element ".parent-selector input" and (min-characters: 5) {
  $this {
    border-color: red;
  }
  $parent {
    background: pink;
  }
}

$root selector

class="root-selector"
@element ".root-selector input" and (min-characters: 5) {
  $this {
    border-color: red;
  }
  $root {
    background: pink;
  }
}

$prev selector

class="prev-selector"
@element ".prev-selector [type=email]" {
  $prev {
    border-color: red;
  }
}

$next selector

class="next-selector"
@element ".next-selector [type=text]" {
  $next {
    border-color: red;
  }
}

CSS Functions

eval("")

Current Year:

Viewport Size:

@element "strong" {
  $this:after {
    content: ' eval("new Date().getFullYear()")';
  }
}
@element "em" {
  $this:after {
    content: ' eval("window.innerWidth+' x '+window.innerHeight")';
  }
}

Element Query Demos

Documentation & Tutorials

Further Reading

Similar Projects

EQCSS in the News

Licence

EQCSS.js is copyright © 2014– Tommy Hodgins & Maxime Euzière, and provided under the MIT license. The full license text can be found on Github