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

@rkh/responsive

v0.0.2

Published

Responsive CSS with dynamic breakpoints

Downloads

4

Readme

Responsive and accessible Sass

responsive is a SCSS library for building responsive and accessible websites.

It does so by defining CSS layers, breakpoints, and variables, with a library of mixins, functions, and variables to help you use and configure them from within your Sass code.

With this library, you can use absolute units (e.g. px, pt) and have them converted into relative units (e.g. rem) based on the user's font size preference and the current breakpoint (which is also scaled based on the user's font size). This way you can in theory translate your designs pixel-perfectly into code (set your viewport size to the size used by your designer), and have these designs scale automatically, making them both responsive and accessible.

A few principles:

  • Accessible designs – Breakpoints and calculated sizes are scaled based on the user's font size preference.
  • Responsive designs – Breakpoints and variables play nice with media queries and container queries.
  • Pixel-perfect designs – Functions and mixins are built to allow use of absolute units (e.g. px, pt) and have them converted into relative units (e.g. rem) automatically. Setting your viewport size to the size used by your designer should result in a pixel-perfect design (in theory).
  • Configurable – The library is built to be configurable, so you can use your own breakpoints, enable/disable features, only use function or mixins, etc.
  • Gapless media-queries - Media queries are generated without gaps, so no unexpected behaviors at the breakpoints.
  • Order independent - The library uses CSS layers to predefine specificity. You don't risk having your media queries in the wrong order. No more !important hacks.

Basic usage

The most important parts are the breakpoint mixin, which applies CSS rules only to a specific breakpoint, and the size function, which converts absolute units (e.g. px, pt) into relative units (e.g. rem).

Standard breakpoints

These are the default breakpoints (can be configured):

Breakpoint | Assumed width | Actual breakpoint | Font size ------------|---------------|---------------------------------------|------------------------ xs | $350px$ | $350px * \frac{1em}{16px} = 21.875em$ | $16px * \frac{100vw}{350px} \approx 4.571vw$ sm | $576px$ | $576px * \frac{1em}{16px} = 36em$ | $16px * \frac{100vw}{576px} = 2.7\overline{77}vw$ md | $768px$ | $768px * \frac{1em}{16px} = 48em$ | $16px * \frac{100vw}{768px} = 2.08\overline{3}vw$ lg | $992px$ | $992px * \frac{1em}{16px} = 62em$ | $16px * \frac{100vw}{992px} \approx 1.613vw$ xl | $1200px$ | $1200px * \frac{1em}{16px} = 75em$ | $16px * \frac{100vw}{1200px} = 1.3\overline{33}vw$

Note that for media queries, you should use 0em for the xs breakpoint, to allow scaling down beyond the assumed width.

Example

Input:

@use '@rkh/responsive' with (
  $breakpoints: (
    'small': 0,
    'medium': 768px,
    'large': 1024px,
  )
);

/* scale the font-size for headlines */
h1 {
  font-size: responsive.size(24px);
}

/* changes color based on breakpoint */
.example {
  @include responsive.breakpoint('small') {
    color: green;
  }
  @include responsive.breakpoint('medium') {
    color: red;
  }
  @include responsive.breakpoint('large') {
    color: white;
  }
}

Will result in an output similar to:

@layer responsive {
  @layer base, from-bp-small, from-bp-medium, from-bp-large, bp-small, bp-medium, bp-large;
}

/* ... property definitions ... */

:root {
  --breakpoint-names: small, medium, large;
  /* ... */
}

@media (min-width: 0em) {
  @media not all and (min-width: 48em) {
    :root {
      --breakpoint: small;
      --assumed-width: 350px;
      --responsive-width: 21.875rem;
      --responsive-min-width: 306.25px;
      --responsive-base-font-size: 4.5714285714vw;
      --responsive-pixel-width: 0.2857142857vw;
    }
  }
}
@media (min-width: 48em) {
  @media not all and (min-width: 64em) {
    :root {
      --breakpoint: medium;
      --assumed-width: 768px;
      --responsive-width: 48rem;
      --responsive-min-width: 768px;
      --responsive-base-font-size: 2.0833333333vw;
      --responsive-pixel-width: 0.1302083333vw;
    }
  }
}
@media (min-width: 64em) {
  :root {
    --breakpoint: large;
    --assumed-width: 1024px;
    --responsive-width: 64rem;
    --responsive-min-width: 1024px;
    --responsive-base-font-size: 1.5625vw;
    --responsive-pixel-width: 0.09765625vw;
  }
}

/* scale the font-size for headlines */
h1 {
  font-size: calc(var(--responsive-pixel-width) * 24);
}

/* changes color based on breakpoint */
@media (min-width: 0em) {
  @media not all and (min-width: 48em) {
    @layer responsive.bp-small {
      .example {
        color: green;
      }
    }
  }
}
@media (min-width: 48em) {
  @media not all and (min-width: 64em) {
    @layer responsive.bp-medium {
      .example {
        color: red;
      }
    }
  }
}
@media (min-width: 64em) {
  @layer responsive.bp-large {
    .example {
      color: white;
    }
  }
}

Further reading

General topics:

Using the library without Sass: