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

@pixleight/tailwindcss-aspect-ratio

v1.0.0

Published

An aspect ratio plugin for Tailwind CSS

Downloads

122

Readme

An Aspect Ratio Plugin for Tailwind CSS

This plugin generates utility classes to help create responsive elements that will maintain a specific aspect ratio. Useful for embedded content like videos from YouTube.

Installation

Install plugin via npm

# Using npm
npm install @pixleight/tailwindcss-aspect-ratio

# Using Yarn
yarn add @pixleight/tailwindcss-aspect-ratio

Add plugin to Tailwind

// tailwind.config.js

module.exports = {
  plugins: [
    // ...
    require('@pixleight/tailwindcss-aspect-ratio'),
    // ...
  ]
}

Usage

.aspect-ratio generates the initial styling, with additional classes for setting the width and height ratios.

The .aspect-ratio-item class also styles the child element to fit the container (this could also be achieved with .absolute.inset-0)

This plugin comes with ratios from 1–16 for both width and height. Using both of these classes along with the .aspect-ratio allows you customize the aspect ratio however you like without needing to configure new classes.

Example:

<div class="aspect-ratio aspect-ratio-w-16 aspect-ratio-h-9">
  <div class="aspect-ratio-item">
    <iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ"></iframe>
  </div>
</div>

Generates:

.aspect-ratio: {
  --aspect-ratio-w: 1;
  --aspect-ratio-h: 1;
  position: relative;
  height: 0;
  overflow: hidden;
  padding-bottom: calc(var(--aspect-ratio-h) / var(--aspect-ratio-w) * 100%);
}

.aspect-ratio-item: {
  position: absolute;
  width: 100%;
  height: 100%;
}

.aspect-ratio-w-16 {
  --aspect-ratio-w: 16;
}

.aspect-ratio-h-9 {
  --aspect-ratio-h: 9;
}

We also include some helpful defaults for common sizes:

  • .aspect-ratio-16/9
    • or .aspect-ratio-video
  • .aspect-ratio-1/1
    • or .aspect-ratio-square
  • .aspect-ratio-4/3
  • .aspect-ratio-3/2

Customization

Sizes can be configured in the theme section of your Tailwind config file:

// tailwind.config.js

module.exports = {
  // ...
  theme: {
    // ...
    extend: {
      // ...
      aspectRatio: {
        '21/9': [21, 9], // Generates `.aspect-ratio-21/9` for a 21:9 ratio

        /**
         * Integers in the `w` and `h` arrays generate classes for that dimension and ratio:
         * .aspect-ratio-{d}-{r}
         */
        w: [
          21,
          30,
        ],
        h: [
          19,
          22,
        ]
      },
      // ...
    },
  },
  // ...
}

Because values in the theme.extend section are only merged shallowly, you will need to include the values from the plugin's default theme if you wish to add more ratios to w or h instead of replacing them:

// tailwind.config.js

const aspectRatioTheme = require('@pixleight/tailwindcss-aspect-ratio/defaultTheme')

module.exports = {
  // ...
  theme: {
    // ...
    extend: {
      // ...
      aspectRatio: {
        w: [
          ...aspectRatioTheme.w, // include ratios from 1–16
          21,
        ],
      },
      // ...
    },
  },
  // ...
}