Git Product home page Git Product logo

element-query's Introduction

Element Query

Element query is a little JS library designed to enable CSS "element queries," or queries nested inside CSS selectors. While traditional media queries always relate to the viewport or window, element queries relate to the state of particular elements on the page. This allows for modular media queries.

Just include the element-query.js script on your page and you can start using some basic element queries right away.

Syntax

At current, only two queries are supported:

  • min-available-width
  • max-available-width

These are based on the width of the element containing the selected element.

In addition, px are the only units currently supported (this will change).

You can try it out with some CSS like:

.test-element:media(max-available-width: 400px) {
    background: purple;
}

In the above CSS, .test-element will have a purple background if it is inside an element that is 400px wide or smaller.

Test

You can also poke around in the HTML and CSS in the /test directory. You'll need to run a web server to test loading an external .css file since that sadly requires XHR to work (so you'll hit CORS when loading).

On OS X, an easy way to do this on the command line is:

$ cd path/to/element-query
$ python -m SimpleHTTPServer

Righteously Ugly Code

This is super-rough; the product of an afternoon's work and a conversation with @beep, inspired by @ianstormtaylor's blog post: http://ianstormtaylor.com/media-queries-are-a-hack/ and @jonathantneal's blog post: http://www.jonathantneal.com/blog/thoughts-on-media-queries-for-elements/ and @necolas's tweet: https://twitter.com/necolas/status/299573744307941376

There will still be some refinement to come.

License

This code copyright 2013 Rob Brackett and is licensed under an MIT-style license, the full text of which can be found in the LICENSE file.

element-query's People

Contributors

mr0grog avatar unruthless avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

element-query's Issues

Support parsing class names instead of loading CSS via XHR

Right now, we have to load the CSS via XHR because the parsed stylesheet info doesn't contain unrecognizable selectors (which would include any selectors with :media() in them).

If we could parse the class names we replace media queries with (e.g. query_min-available-width_200px), we could do everything without needing to reload via XHR. Not as nice for an author, though. Maybe a useful optimization when using a build tool?

Probably not worthwhile unless someone really wants to use this in production as more than a proof-of-concept.

Support html:media() by synthesizing actual @media(){} rules

Right now, we don't actually support element queries on the html element because it has not parentNode that we can measure. If we encounter an html:media() query, we should synthesize an appropriate @media rule for it and be done with it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.