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 🙏

© 2024 – Pkg Stats / Ryan Hefner

cssuseragent

v2.1.31

Published

Automatically adds UserAgent-specific CSS classes to the document allowing for browser variations without resorting to CSS hacks.

Downloads

1,455

Readme

.css{user:agent;}

Distributed under the terms of The MIT license.

Special CSS Classes...

Adding cssua.js to the page preps the document with special CSS classes which enable targeting of browsers with varying degrees of precision.

<!-- example CSS classes applied to the page -->
<html class="ua-chrome ua-chrome-25 ua-chrome-25-0 ua-chrome-25-0-1364 ua-chrome-25-0-1364-172
	ua-desktop ua-desktop-macintosh
	ua-mac_os_x ua-mac_os_x-10 ua-mac_os_x-10-8 ua-mac_os_x-10-8-3
	ua-webkit ua-webkit-537 ua-webkit-537-22">
	...
</html>

UserAgent map...

UserAgent-sniffing is regarded as something to be used sparingly, but pragmatic developers know it is sometimes the simplest approach to fixing a particular browser's quirk. When it is, cssua.js produces a helper object as a side-effect which makes user-agent-siffing easier and more consistent.

An object map is also built which allows you to test the user agent from your script in a simplified manner that doesn't require string parsing. For example, this object is effectively produced:

cssua.userAgent = cssua.ua = {
	chrome: "25.0.1364.172",
	desktop: "macintosh",
	mac_os_x: "10.8.3",
	webkit: "537.22"
};

Examples

This makes applying slight layout differences a snap:

/* CssUserAgent lets you target specific browsers without resorting CSS hacks */
.foo
{
	background-image: url(foo.png);
	background-repeat: no-repeat;
	background-position: left top;
}

.ua-ie-5 .foo,
.ua-ie-6 .foo
{
	/* IE versions < 7.0 don't fully support transparent 24-bit PNGs */
	background-image: url(foo.gif);
}

Testing for older Internet Explorer has never been easier than

if (cssua.userAgent.ie < 8) { /* proof of Pareto principle here */ }

Or testing if is a mobile browser:

if (cssua.userAgent.mobile) { /* consider your audience */ }

Remember to convert a version String to a Number with parseFloat if the value isn't a simple number, e.g., it has multiple decimal points or trailing letters. Otherwise, JavaScript may interpret the value as NaN (not-a-number):

if (parseFloat(cssua.ua.chrome) > 101) { /* enter the Matrix */ }

Mobile browser detection

CssUserAgent helps detect the increasingly ambiguous category of mobile browsers. cssua.js adds specific classes when it detects mobile browsers:

<html class="... ua-mobile ua-mobile-iphone ...">...<html>

No Browser Hacks...

This technique also avoids all CSS hacks. It allows you to target the browser rendering engine (e.g. "ua-gecko"), or a specific browser (e.g. "ua-firefox"). The version can be targeted at the major version number (e.g. "ua-ie-5" includes 5.0, 5.5) or minor (e.g. "ua-ie-5-0" includes only 5.0) all the way down to a very specific case (e.g. "ua-chrome-8-0-552-224").

Future UserAgents...

This script understands the common structures of user agent strings enabling future user agent strings to simply work without changes. For example, when the early beta builds of Google Chrome were first released, it just worked.


Tested with
BrowserStack


Copyright (c) 2006-2013 Stephen M. McKamey