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 🙏

© 2025 – Pkg Stats / Ryan Hefner

html-sizes

v1.0.0

Published

Html sizes generator with max DPR support

Downloads

7

Readme

Html Sizes

npm NPM npm

Generate html sizes attributes without painful for your responsive images.

Html sizes support maxDPR option to cap images dpr and speed up your website !

You can read this mozilla tutorial to understand responsive images.

Install

This library is available on npm.

npm install html-sizes

yarn add html-sizes

pnpm add html-sizes

Usage

Html sizes provide an unique function named generateSizes.

import generateSizes from 'html-sizes';

generateSizes({
  '(max-width: 340px)': '100vw',
  '(min-width: 400px)': '350px',
  default: '50vw',
});

// Return : (max-width: 340px) 100vw, (min-width: 400px) 350px, 50vw

Parameters

generateSizes(params: SizesParams, options?: SizesOptions);

params (required)

export interface SizesParams {
  [key: string]: string;
  default: string;
}

A map (js object) with all your needed breakpoints and the required default size.

  • Keys is your css breakpoints with parenthesis
  • Values is your image size (with vw, px, calc(), min(), max()...)

generateSizes respects params attributes order but default is always at the end of generated sizes.

options (optional)

export interface SizesOptions {
  maxDPR?: MaxDPR; // 1 | 2
  dpiCompatibility?: boolean;
}

maxDPR

You can pass only 1 or 2 to cap image DPR.

This option add dpr media queries on sizes to scale down your image on high dpr devices and reduce image size.

To understand how browser pick the best image with pixel density, you can read this article from imagekit.io.

Scaling is computed on all values and all breakpoints.

Supported media queries

  • (min-resolution: 3dppx)
  • (-webkit-min-device-pixel-ratio: 3) -> Safari

Examples

Simple example with a 100vw image on a 500px viewport width.

Without maxDPR

generateSizes({
  default: '100vw',
});

// Return : 100vw

| DPR | Sizes | Final image width | | --- | ----- | ----------------- | | 1 | 100vw | 500 x 1 = 500px | | 2 | 100vw | 500 x 2 = 1000px | | 3 | 100vw | 500 x 3 = 1500px |

With maxDPR=2

generateSizes(
  {
    default: '100vw',
  },
  {
    maxDPR: 2,
  },
);

// Return : (min-resolution: 3dppx) 67vw, (-webkit-min-device-pixel-ratio: 3) 67vw, 100vw

| DPR | Sizes | Final image width | | --- | ------------------- | ----------------------- | | 1 | 100vw | 500 x 1 = 500px | | 2 | 100vw | 500 x 2 = 1000px | | 3 | 67vw (100 * 2 / 3) | 500 x 3 x 0.67 = 1005px |

On 3x display, images is scale down to 2x.

With maxDPR=1

generateSizes(
  {
    default: '100vw',
  },
  {
    maxDPR: 1,
  },
);

// Return : (min-resolution: 3dppx) 34vw, (-webkit-min-device-pixel-ratio: 3) 34vw, (min-resolution: 2dppx) 50vw, (-webkit-min-device-pixel-ratio: 2) 50vw, 100vw

| DPR | Sizes | Final image width | | --- | ------------------ | ---------------------- | | 1 | 100vw | 500 x 1 = 500px | | 2 | 50vw (100 x 1 / 2) | 500 x 2 x 0.5 = 500px | | 3 | 34vw (100 x 1 / 3) | 500 x 3 x 0.34 = 510px |

On 2x and 3x display, images is scale down to 1x.

dpiCompatibility

This flag add a new supported media query with DPI for old browsers.

generateSizes(
{
  '(max-width: 500px)': '100vw',
  default: '500px',
},
{
  maxDPR: 2,
  dpiCompatibility: true,
},
);

// Return : (min-resolution: 3dppx) and (max-width: 500px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 500px) 67vw, (min-resolution: 288dpi) and (max-width: 500px) 67vw, (max-width: 500px) 100vw, (min-resolution: 3dppx) 334px, (-webkit-min-device-pixel-ratio: 3) 334px, (min-resolution: 288dpi) 334px, 500px