• Stars
    star
    956
  • Rank 47,818 (Top 1.0 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 4 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

@tailwindcss/aspect-ratio

A plugin that provides a composable API for giving elements a fixed aspect ratio.

Installation

Install the plugin from npm:

npm install -D @tailwindcss/aspect-ratio

Then add the plugin to your tailwind.config.js file, and disable the aspectRatio core plugin to avoid conflicts with the native aspect-ratio utilities included in Tailwind CSS v3.0:

// tailwind.config.js
module.exports = {
  theme: {
    // ...
  },
  corePlugins: {
    aspectRatio: false,
  },
  plugins: [
    require('@tailwindcss/aspect-ratio'),
    // ...
  ],
}

Usage

Combine the aspect-w-{n} and aspect-h-{n} classes to specify the aspect ratio for an element:

<div class="aspect-w-16 aspect-h-9">
  <iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>

Use aspect-none to remove any aspect ratio behavior:

<div class="aspect-w-16 aspect-h-9 lg:aspect-none">
  <!-- ... -->
</div>

When removing aspect ratio behavior, if nested elements have w-{n} or h-{n} classes, ensure they are re-declared with a matching breakpoint prefix:

<div class="aspect-w-16 aspect-h-9 lg:aspect-none">
  <img src="..." alt="..." class="w-full h-full object-center object-cover lg:w-full lg:h-full" />
</div>

Note that due to the way this currently needs to be implemented (the old padding-bottom trick) you need to assign the aspect ratio to a parent element, and make the actual element you are trying to size the only child of that parent.

Once the aspect-ratio property is supported in modern browsers, we'll add official support to Tailwind CSS itself and deprecate this plugin.

Aspect ratio classes up to 16 are generated by default:

Width Height
aspect-w-1 aspect-h-1
aspect-w-2 aspect-h-2
aspect-w-3 aspect-h-3
aspect-w-4 aspect-h-4
aspect-w-5 aspect-h-5
aspect-w-6 aspect-h-6
aspect-w-7 aspect-h-7
aspect-w-8 aspect-h-8
aspect-w-9 aspect-h-9
aspect-w-10 aspect-h-10
aspect-w-11 aspect-h-11
aspect-w-12 aspect-h-12
aspect-w-13 aspect-h-13
aspect-w-14 aspect-h-14
aspect-w-15 aspect-h-15
aspect-w-16 aspect-h-16

Configuration

You can configure which values and variants are generated by this plugin under the aspectRatio key in your tailwind.config.js file:

// tailwind.config.js
module.exports = {
  theme: {
    aspectRatio: {
      1: '1',
      2: '2',
      3: '3',
      4: '4',
    }
  },
  variants: {
    aspectRatio: ['responsive', 'hover']
  }
}

Compatibility with default aspect-ratio utilities

Tailwind CSS v3.0 shipped with native aspect-ratio support, and while these new utilities are great, the aspect-ratio property isn't supported in Safari 14, which still has significant global usage. If you need to support Safari 14, this plugin is still the best way to do that.

While it's technically possible to use the new native aspect-ratio utilities as well as this plugin in the same project, it doesn't really make a lot of sense to do so. If you're able to use the new native aspect-ratio utilities, just use them instead of this plugin, as they are a lot simpler and work much better.

However, if you do want to use both approaches in your project, maybe as a way of transitioning slowly from the plugin approach to the new native utilities, you'll need to add the following values to your tailwind.config.js file:

module.exports = {
  // ...
  theme: {
    aspectRatio: {
      auto: 'auto',
      square: '1 / 1',
      video: '16 / 9',
      1: '1',
      2: '2',
      3: '3',
      4: '4',
      5: '5',
      6: '6',
      7: '7',
      8: '8',
      9: '9',
      10: '10',
      11: '11',
      12: '12',
      13: '13',
      14: '14',
      15: '15',
      16: '16',
    },
  },
}

This is necessary, as the default aspectRatio values are overwritten by this plugin's values.

More Repositories

1

tailwindcss

A utility-first CSS framework for rapid UI development.
TypeScript
81,562
star
2

headlessui

Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
TypeScript
25,564
star
3

heroicons

A set of free MIT-licensed high-quality SVG icons for UI development.
JavaScript
21,279
star
4

prettier-plugin-tailwindcss

A Prettier plugin for Tailwind CSS that automatically sorts classes based on our recommended class order.
TypeScript
5,382
star
5

tailwindcss-typography

Beautiful typographic defaults for HTML you don't control.
JavaScript
4,319
star
6

tailwindcss-forms

A plugin that provides a basic reset for form styles that makes form elements easy to override with utilities.
HTML
4,137
star
7

tailwindcss.com

The Tailwind CSS documentation website.
MDX
3,233
star
8

tailwindcss-intellisense

Intelligent Tailwind CSS tooling for Visual Studio Code
TypeScript
2,783
star
9

tailwindcss-jit

JavaScript
2,188
star
10

tailwindcss-custom-forms

A better base for styling form elements with Tailwind CSS.
JavaScript
1,553
star
11

tailwindcss-line-clamp

A plugin that provides utilities for visually truncating text after a fixed number of lines.
JavaScript
1,176
star
12

tailwindcss-container-queries

A plugin for Tailwind CSS v3.2+ that provides utilities for container queries.
TypeScript
1,131
star
13

designing-with-tailwindcss

Source code for the "Designing with Tailwind CSS" course.
CSS
712
star
14

heroicons.com

JavaScript
697
star
15

tailwindcss-setup-examples

675
star
16

tailwindui-vue

Deprecated, please use the Headless UI repo instead.
JavaScript
647
star
17

play.tailwindcss.com

JavaScript
638
star
18

tailwindcss-playground

HTML
516
star
19

tailwindui-react

Deprecated, please use the Headless UI repo instead.
TypeScript
433
star
20

tailwindcss-from-zero-to-production

Source code for the "Tailwind CSS Up and Running" screencast series.
JavaScript
379
star
21

blog.tailwindcss.com

JavaScript
342
star
22

tailwindui-issues

Bug fixes and feature request tracking for Tailwind UI.
233
star
23

webpack-starter

A quick and simple example of using Tailwind CSS with Webpack.
JavaScript
212
star
24

tailwindcss-plugin-examples

Examples to help you get started building your own Tailwind CSS plugins.
JavaScript
209
star
25

discuss

A place to ask questions, get help, or share what you've built with Tailwind CSS.
171
star
26

tailwind-play-api

PHP
44
star
27

minimal-tailwind-postcss-plugin

A simple PostCSS plugin designed to cover all of the tricky integration points a build-tool needs to support for Tailwind CSS.
JavaScript
11
star
28

reproduction-headlessui-vue

JavaScript
9
star
29

reproduction-headlessui-react

JavaScript
1
star