• Stars
    star
    4,270
  • Rank 9,607 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CSS Element-Queries aka Container Queries. High-speed element dimension/media queries in valid css.

CSS Element Queries

Gitter

Element Queries is a polyfill adding support for element based media-queries to all new browsers (incl. IE7+). 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 since it listens only on size changes of elements that have element query rules defined through css. Other element query polifills only listen on window.onresize which causes performance issues and allows only to detect changes via window.resize event and not inside layout changes like css3 animation, :hover, DOM changes etc.
  • no interval/timeout detection. Truly event-based through integrated ResizeSensor class.
  • automatically discovers new DOM elements. No need to call javascript manually.
  • no CSS modifications. Valid CSS Syntax
  • all CSS selectors available. Uses regular attribute selector. No need to write rules in HTML/JS.
  • supports and tested in webkit, gecko and IE(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.)
  • Works beautiful for responsive images without FOUC

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

Examples

Element Query

.widget-name h2 {
    font-size: 12px;
}

.widget-name[min-width~="400px"] h2 {
    font-size: 18px;
}

.widget-name[min-width~="600px"] h2 {
    padding: 55px;
    text-align: center;
    font-size: 24px;
}

.widget-name[min-width~="700px"] h2 {
    font-size: 34px;
    color: red;
}

As you can see we use the ~= attribute selector. Since this css-element-queries polyfill adds new element attributes on the DOM element (<div class="widget-name" min-width="400px 700px"></div>) depending on your actual CSS and element's dimension, you should always use this attribute selector (especially if you have several element query rules on the same element).

<div class="widget-name">
   <h2>Element responsiveness FTW!</h2>
</div>

Responsive image

    <div data-responsive-image>
        <img data-src="http://placehold.it/350x150"/>
        <img min-width="350" data-src="http://placehold.it/700x300"/>
        <img min-width="700" data-src="http://placehold.it/1400x600"/>
    </div>

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>

See it in action:

Here live http://marcj.github.io/css-element-queries/.

Demo

Module Loader

If you're using a module loader you need to trigger the event listening or initialization yourself:

var ElementQueries = require('css-element-queries/src/ElementQueries');

 //attaches to DOMLoadContent
ElementQueries.listen();

//or if you want to trigger it yourself.
// Parse all available CSS and attach ResizeSensor to those elements which have rules attached
// (make sure this is called after 'load' event, because CSS files are not ready when domReady is fired.
ElementQueries.init();

Issues

  • So far does not work on img and other elements that can't contain other elements. Wrapping with a div works fine though (See demo).
  • Adds additional hidden elements into selected target element and forces target element to be relative or absolute.
  • Local stylesheets do not work (using file:// protocol).
  • If you have rules on an element that has a css animation, also add element-queries. E.g. .widget-name { animation: 2sec my-animation, 1s element-queries;}. We use this to detect new added DOM elements automatically.

License

MIT license. Copyright Marc J. Schmidt.

More Repositories

1

TypeRunner

High-performance TypeScript compiler
C++
2,569
star
2

jquery-selectBox

A jQuery plugin for replacing <select> elements.
JavaScript
548
star
3

angular2-localstorage

Angular 2+ decorator to save and restore variables/class properties to HTML5 LocalStorage automatically.
TypeScript
302
star
4

topsort.php

High-Performance Topological Sort / Dependency resolver in PHP
PHP
237
star
5

php-rest-service

Php-Rest-Service is a very simple and fast PHP class for server-side RESTful JSON APIs.
PHP
217
star
6

BetterQuitJobBundle

You should better quit your current job instead of searching a solution for *that* problem.
95
star
7

angular-es6-annotations

This little collection of annotations and registry functions allows you to register directives, controllers and filter with annotations at your angular module.
JavaScript
66
star
8

Pesto

Pesto is a high-performance GUI framework in C++ highly inspired by CSS and HTML, using Skia as rendering engine.
C++
64
star
9

angular-desktop-ui

Angular & Electron desktop UI framework. Angular components for native looking and behaving macOS desktop UI (Electron/Web)
TypeScript
51
star
10

pybridge

TypeScript library to access python functions in NodeJS, type-safe and easy to use.
TypeScript
44
star
11

typescript-react-dependency-injection

React + Vite + Deepkit's powerful dependency injection system
TypeScript
27
star
12

typedoc-plugin-lerna-packages

A plugin for Typedoc that groups all Lerna packages into own TS module
TypeScript
27
star
13

npm-local-development

Replacement for 'npm link' that actually works. Automatically overwrites installed packages with a locally available dev version.
JavaScript
24
star
14

glut.ts

The first reactive full-stack framework for Typescript specially tailored for admin interfaces and complex SPA.
TypeScript
23
star
15

bitcoin.ts

Typescript implementation of a Cryptocurrency inspired by the Bitcoin blockchain.
TypeScript
19
star
16

chrome-chatgpt-screenshot-extension

JavaScript
12
star
17

google-api-php-client

Google APIs Client Library for PHP
PHP
11
star
18

krycms-bundle-old

Don't use it. Don't Star it. Old project. New Project is https://github.com/jarves/jarves |
JavaScript
10
star
19

angular-typescript-decorators

Kinda hackish handy decorators for using Typescript with Angular v1.4+
TypeScript
9
star
20

katana-orm

Katana ORM, the new incredible sharp PHP ORM - maybe someday.
8
star
21

optimistic-locking-behavior

A behavior allowing you to use optimistic locking in Propel 2
PHP
5
star
22

twig-apply_filter-bundle

Symfony Bundle for Twig 'apply_filter' to call dynamic filters.
PHP
4
star
23

typescript-semantic-search

TypeScript
4
star
24

deepkit-bookstore

Example bookstore API with Deepkit API Console
TypeScript
3
star
25

angular-zoneless

Angular v16+ Zoneless implementation for SSR with Hydration
TypeScript
3
star
26

angular-unsub

Unsubscribes automatically your RXJS subscriptions when component is ngOnDestroy'ed
TypeScript
3
star
27

change-logger-behavior

Logs changes for defined columns in a extra logger table, for Propel2
PHP
3
star
28

glut-admin

The first real-time headless CMS/CMF with crud builder, based on Typescript/Angular. So efficient and fast it hurts.
TypeScript
2
star
29

mootools-sprintf

Mootools Javascript sprintf/printf function.
JavaScript
2
star
30

estdlib.ts

Engineer's strict stdlib for TypeScript.
TypeScript
2
star
31

mowla.js

A fast and super simple JavaScript Template Engine.
JavaScript
1
star
32

Propel2-old

PHP
1
star
33

shellfuncs

Some useful bash functions
Shell
1
star
34

deepkit-webpack

JavaScript
1
star
35

git-objects-sync

git push based on objects, instead of commits.
Python
1
star
36

angular-ivy-issue1

TypeScript
1
star
37

shut-the-lock

iOs Game "Shut the lock"
JavaScript
1
star
38

vscode-vertical-tabs

1
star
39

docker-macOS-container

An easy way to run commands in docker using macOS catalina as OS (via qemu) via ssh. Jenkins node possible
Shell
1
star
40

angular-desktop-ui-example

Angular v9 with angular-desktop-ui configured and ready to use
TypeScript
1
star