npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

document-query-style

v1.1.0

Published

Select element(s) in a document with a computed style that matches the supplied query.

Readme

documentQueryStyle

Select element(s) in a document with a computed style that matches the supplied query

Usage

var documentQueryStyle = require('document-query-style');

var whiteElements = documentQueryStyle('background-color', 'rgb(255, 255, 255)');

var firstAbsoluteTopElement = documentQueryStyle({
    position: 'absolute',
    top: 0
}, true);

API

Query a single CSS property

documentQueryStyle( property, value [, shouldReturnFirstResult] )
  • property is a CSS property such as 'background-color' or 'position'.
  • value is the value you expect that property to hold.
  • shouldReturnFirstResult (optional) should be true if you're only interested in the first matching element.

Returns an array of elements (which could be empty), or if asked for the first matching element, it will either return an element or null if there are no matches.

Query multiple CSS properties

documentQueryStyle( properties [, shouldReturnFirstResult] )
  • properties is an object where keys are CSS properties and values are the values you expect those properties to hold
  • shouldReturnFirstResult (optional) [as above]

Returns [as above]

Polyfill (well, prollyfill)

Assuming this would eventually become part of the document API, it would most likely expose the methods:

  • document.queryStyleAll - which returns all matching elements
  • document.queryStyle - which returns the first matching element

You can polyfill that API by calling:

documentQueryStyle.polyfill();

Then you'll be able to do this:

var whiteElements = document.queryStyleAll('background-color', 'rgb(255, 255, 255)');

var firstAbsoluteTopElement = document.queryStyle({
    position: 'absolute',
    top: 0
});

Note: rather than using a single funtion with the optional shouldReturnFirstResult argument, each method essentially sets that option for you.

.polyfill API

documentQueryStyle.polyfill([api]);
  • api (optional) the object you want to make queryStyleAll and queryStyle available on. This defaults to document, but you might not want to put it there.

Demo

There are examples in the examples directory. Have a look at the index.html file in each example for some typical markup and styling, then look at the corresponding index.js file to see how you'd use documentQueryStyle to query the DOM.

To try out some examples in the browser, run:

$ npm run example

...to get a list of examples, then run:

$ npm run example <example_name>

...and open http://127.0.0.1:9966 in your browser, and chech the browser's console output.

Building standalone version

This module is designed to be required into your project with a tool such as browserify, but if you want to build a standalone module, run:

$ npm install
$ npm run dist

You'll find two new files in the root directory (one of which is minified). Either drop one of the scripts into a page and access documentQueryStyle globally in your project, or use your AMD loader of choice.

Caveats

window.getComputedStyle is used to fetch the current style of each element in the DOM, which means that all units are standardised, i.e.:

  • Every size is in px (including fractions based on unit conversions)
  • Every colour is in rgb() or rgba()

This library will eventually perform conversions on the values you pass in, but for now, stick to the standard units.

Also, window.getComputedStyle will treat all vendor prefixed properties as separate properties, so you'll have to explicitly check them. documentQueryStyle knows nothing about which property will actually determine the style of an element, in cases where different values are applied.

If an element inherits style from a parent, rather than being defined explicitly, it will be included in the results.

Most importantly, the speed of documentQueryStyle is inversely proportional to the number of elements in the DOM when you call it, so it's probably best not to use it on very complex pages, unless you want to take a performance hit.