|
| 1 | +# @tailwindcss/aspect-ratio |
| 2 | + |
| 3 | +A plugin that provides a composable API for giving elements a fixed aspect ratio. |
| 4 | + |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +Install the plugin from npm: |
| 9 | + |
| 10 | +```sh |
| 11 | +# Using npm |
| 12 | +npm install @tailwindcss/aspect-ratio |
| 13 | + |
| 14 | +# Using Yarn |
| 15 | +yarn add @tailwindcss/aspect-ratio |
| 16 | +``` |
| 17 | + |
| 18 | +Then add the plugin to your `tailwind.config.js` file: |
| 19 | + |
| 20 | +```js |
| 21 | +// tailwind.config.js |
| 22 | +module.exports = { |
| 23 | + theme: { |
| 24 | + // ... |
| 25 | + }, |
| 26 | + plugins: [ |
| 27 | + require('@tailwindcss/aspect-ratio'), |
| 28 | + // ... |
| 29 | + ], |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +Combine the `aspect-w-{n}` and `aspect-h-{n}` classes to specify the aspect ratio for an element: |
| 36 | + |
| 37 | +```html |
| 38 | +<div class="aspect-w-16 aspect-h-9"> |
| 39 | + <iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> |
| 40 | +</div> |
| 41 | +``` |
| 42 | + |
| 43 | +Note that due to the way this currently needs to be implemented ([the old padding-bottom trick](https://css-tricks.com/aspect-ratio-boxes/)) 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. |
| 44 | + |
| 45 | +Once the [`aspect-ratio` property](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/aspect-ratio) is supported in modern browsers, we'll add official support to Tailwind CSS itself and deprecate this plugin. |
| 46 | + |
| 47 | +Aspect ratio classes up to 16 are generated by default: |
| 48 | + |
| 49 | +| Width | Height | |
| 50 | +| --- | --- | |
| 51 | +| `aspect-w-1` | `aspect-h-1` | |
| 52 | +| `aspect-w-2` | `aspect-h-2` | |
| 53 | +| `aspect-w-3` | `aspect-h-3` | |
| 54 | +| `aspect-w-4` | `aspect-h-4` | |
| 55 | +| `aspect-w-5` | `aspect-h-5` | |
| 56 | +| `aspect-w-6` | `aspect-h-6` | |
| 57 | +| `aspect-w-7` | `aspect-h-7` | |
| 58 | +| `aspect-w-8` | `aspect-h-8` | |
| 59 | +| `aspect-w-9` | `aspect-h-9` | |
| 60 | +| `aspect-w-10` | `aspect-h-10` | |
| 61 | +| `aspect-w-11` | `aspect-h-11` | |
| 62 | +| `aspect-w-12` | `aspect-h-12` | |
| 63 | +| `aspect-w-13` | `aspect-h-13` | |
| 64 | +| `aspect-w-14` | `aspect-h-14` | |
| 65 | +| `aspect-w-15` | `aspect-h-15` | |
| 66 | +| `aspect-w-16` | `aspect-h-16` | |
| 67 | + |
| 68 | +## Configuration |
| 69 | + |
| 70 | +You can configure which values and variants are generated by this plugin under the `aspectRatio` key in your `tailwind.config.js` file: |
| 71 | + |
| 72 | +```js |
| 73 | +// tailwind.config.js |
| 74 | +module.exports = { |
| 75 | + theme: { |
| 76 | + aspectRatio: { |
| 77 | + 1: '1', |
| 78 | + 2: '2', |
| 79 | + 3: '3', |
| 80 | + 4: '4', |
| 81 | + } |
| 82 | + }, |
| 83 | + variants: { |
| 84 | + aspectRatio: ['responsive', 'hover'] |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
0 commit comments