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

html-attribute-sets

v1.0.0

Published

A library for managing and manipulating sets of HTML attributes. Useful for toggling CSS classes per breakpoint. For example, display an element in Bootstrap as Dropdown for desktop, and as Accordion for mobile.

Downloads

28

Readme

HTML Attribute Sets

A JS library to describe directly in HTML and swap HTML tag attributes (CSS styles, data attributes, etc), without managing custom JavaScript code for each case.

The problem

When you create a responsive design in HTML and CSS, pretty often, some components should look and behave differently depending on the device type. For example, several blocks with text should look like tabs on desktop and as an accordion on mobile.

The easiest and the most popular approach to implement this is to put this component twice into the HTML code and use the display: none CSS approach to hide one of the duplicates from displaying per device.

But is that approach good? No! You make the same HTML code present on the page twice! Search engines will not be happy with this! And page size and load time - too.

The solution

The right approach is just to reuse the same HTML structure for all the device types, and just toggle CSS classes in it to make it look different per device type.

To resolve this task, I created a JS library that automates this feature, which makes swapping HTML tag classes and attributes easy and automatic, and even without any custom JS code!

All that you need to do is to add an attribute data-attr-sets with the list of classes per breakpoint (device type).

Demo

You can see the functionality in action on the demo page »

Usage

  1. Add a library to the webpage using the CDN:
<script src="//cdn.jsdelivr.net/gh/MurzNN/[email protected]/dist/browser/min/index.js" type="module"></script>

or any other approach.

  1. And just describe needed class sets per breakpoint in the HTML attribute of the needed tag, here is an example:
<div data-attr-sets='{
  "sm-": "btn btn-primary",
  "md": "btn btn-secondary",
  "lg+": "btn btn-success"
}'>My button</div>

This is just a simplified example based on Bootstrap breakpoints and classes, which makes the button colored blue for mobile devices, gray for tablet, and green for desktop.

Actually, the library doesn't depend on Bootstrap, you can use it with any frontend framework and any breakpoints.

To automate triggering the attributes swap on the breakpoint change, you can use any approach that you want.

Example

Here is a more detailed example of a component that is represented as dropdowns for all breakpoints more than "md" and as accordion for "sm" and less, and uses the (breakpoints-js)[https://github.com/thecreation/breakpoints-js] library to trigger swapping when the breakpoint changes.

<div id="my-responsive-component" data-attr-sets='{"sm-":"accordion","md+":""}'>
  <div data-attr-sets='{"sm-":"accordion-item","md+":"dropdown"}'>
    <button data-attr-sets='{"sm-":{"class":"accordion-header accordion-button collapsed","data-bs-toggle":"collapse"},"md+":{"class":"btn btn-outline-primary dropdown-toggle","data-bs-toggle":"dropdown"}}' type="button" data-bs-parent="#my-responsive-component" data-bs-target="#my-item-1-body">Header 1</button>
      <div data-attr-sets='{"sm-":"accordion-body collapse","md+":"dropdown-menu"}' id="my-item-1-body" data-bs-parent="#my-responsive-component">Body 1</div>
  </div>
  <div data-attr-sets='{"sm-":"accordion-item","md+":"dropdown"}'>
    <button data-attr-sets='{"sm-":{"class":"accordion-header accordion-button collapsed","data-bs-toggle":"collapse"},"md+":{"class":"btn btn-outline-primary dropdown-toggle","data-bs-toggle":"dropdown"}}' type="button" data-bs-parent="#my-responsive-component" data-bs-target="#my-item-2-body">Header 2</button>
      <div data-attr-sets='{"sm-":"accordion-body collapse","md+":"dropdown-menu"}' id="my-item-2-body" data-bs-parent="#my-responsive-component">Body 2</div>
  </div>
</div>

and a JS library initialization:

<script
  src="//cdn.jsdelivr.net/npm/breakpoints-js@1/dist/breakpoints.min.js:"
  type="module"
></script>
<script>
// Initialize the Breakpoints library.
Breakpoints();

// Get the list of available breakpoints.
const breakpointsList = Breakpoints.all();

// Set initial classes on the element load.
AttributeSetApply({
  set: Breakpoints.current().name,
  setsList: breakpointsList
});

// Apply attributes on changing the breakpoint.
Breakpoints.on('change', function () {
  AttributeSetApply({
    set: this.current.name,
    setsList: breakpointsList
  });
});