Skip to content

CSS-Element-Queries Polyfill. proof-of-concept for high-speed element dimension/media queries in valid css.

License

Notifications You must be signed in to change notification settings

gjjones/css-element-queries

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 

Repository files navigation

CSS Element Queries

Element Queries is a polyfill adding support for element based media-queries to all new browsers (incl. IE8+). It allows not only to define media-queries based on window-size but also adds 'media-queries' functionality depending on element (any selector supported) size while not causing performance lags due to event based implementation.

It's a proof-of-concept event-based CSS element dimension query with valid CSS selector syntax.

Features:

  • no performance issues
  • no interval/timeout detection. Truly event-based
  • no CSS modifications. Valid CSS Syntax
  • all CSS selectors available. Uses regular attribute selector
  • supports and tested in webkit, gecko and IE(8/9/10).
  • min-width, min-height, max-width and max-height are supported so far
  • works with any layout modifications: HTML (innerHTML etc), inline styles, DOM mutation, CSS3 transitions, fluid layout changes (also percent changes), pseudo classes (:hover etc.), window resizes and more
  • no Javascript-Framework dependency (works with jQuery, Mootools, etc.)

More demos and information: http://marcj.github.io/css-element-queries/

Example

.widget-name {
    padding: 25px;
}
.widget-name[max-width="200px"] {
    padding: 0;
}
.widget-name[min-width="500px"] {
    padding: 55px;
}

/* responsive images /*
.responsive-image img {
    width: 100%;
}

.responsive-image[max-width^='400px'] img {
    content: url(demo/image-400px.jpg);
}

.responsive-image[max-width^='1000px'] img {
    content: url(demo/image-1000px.jpg);
}

.responsive-image[min-width='1000px'] img {
    content: url(demo/image-full.jpg);
}

Include the javascript files at the bottom and you're good to go. No custom javascript calls needed.

<script src="src/ResizeSensor.js"></script>
<script src="src/ElementQueries.js"></script>

Issues

  • So far does not work on img tags. Wrapping with a div works fine though (See demo).
  • [only non-IE]: Adds additional hidden element into selected target element and forces target element to be relative or absolute.

Event-Based resize detection inspired by backalleycoder.com <3

Bitdeli Badge

License

This code is copyright 2013 Marc J. Schmidt and is licensed under the MIT license. See the LICENSE file for the full text.

ElementQueries.convertToPx covered by the following license:

Copyright (c) 2013 Rob Brackett

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

CSS-Element-Queries Polyfill. proof-of-concept for high-speed element dimension/media queries in valid css.

Resources

License

Stars

Watchers

Forks

Packages

No packages published