|
| 1 | +<p align="center"> |
| 2 | + <a href="https://tailwindcss.com/" target="_blank"><img width="200" src="https://tailwindcss.com/img/tailwind.svg"></a><br> |
| 3 | + A utility-first CSS framework for rapidly building custom user interfaces. |
| 4 | +</p> |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +<br> |
| 9 | + |
| 10 | +# Tailwind CSS aspect ratio utilities |
| 11 | + |
| 12 | +This is a Tailwind CSS plugin that adds an utilities for restrict an element with a set [aspect ratio](https://css-tricks.com/aspect-ratio-boxes/) **based on the parent's element's width**. |
| 13 | + |
| 14 | +Maintained by: [Joan Piedra](https://joanpiedra.com) → [@neojp](https://twitter.com/neojp) |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +Install as a node module with either `npm` or `yarn` on your command line |
| 19 | + |
| 20 | +```bash |
| 21 | +# Install via npm |
| 22 | +npm install --save-dev @neojp/tailwindcss-aspect-ratio-utilities |
| 23 | + |
| 24 | +# Install via yarn |
| 25 | +yarn add --dev @neojp/tailwindcss-aspect-ratio-utilities |
| 26 | +``` |
| 27 | + |
| 28 | +Add this module as a plugin on your [Tailwind configuration file](https://tailwindcss.com/docs/configuration#plugins) (`tailwind.config.js`). |
| 29 | + |
| 30 | +```js |
| 31 | +module.exports = { |
| 32 | + plugins: [ |
| 33 | + require('@neojp/tailwindcss-aspect-ratio-utilities') |
| 34 | + ] |
| 35 | +}; |
| 36 | +``` |
| 37 | + |
| 38 | +### Variants |
| 39 | + |
| 40 | +Note that this plugin allows the usage of variants through the key `aspectRatio` and will default to your global variant setting. |
| 41 | + |
| 42 | +```js |
| 43 | +module.exports = { |
| 44 | + variants: { |
| 45 | + aspectRatio: ['responsive'] |
| 46 | + } |
| 47 | +}; |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +## Usage |
| 52 | + |
| 53 | +Use the Tailwind utility classes provided by this plugin. |
| 54 | + |
| 55 | +```html |
| 56 | + |
| 57 | +<div class="aspect-ratio-square"></div> |
| 58 | +<div class="aspect-ratio-3/2"></div> |
| 59 | +<div class="aspect-ratio-2/3"></div> |
| 60 | +<div class="aspect-ratio-4/3"></div> |
| 61 | +<div class="aspect-ratio-5/6"></div> |
| 62 | +<div class="aspect-ratio-16/9"></div> |
| 63 | +<div class="aspect-ratio-21/9"></div> |
| 64 | + |
| 65 | +``` |
| 66 | + |
| 67 | +### Examples |
| 68 | + |
| 69 | +Draw a gray 256px square box: |
| 70 | + |
| 71 | +```html |
| 72 | +<div class="bg-gray-500 w-64"> |
| 73 | + <div class="aspect-ratio-square"></div> |
| 74 | +</div> |
| 75 | +``` |
| 76 | + |
| 77 | +Restrict an image to a 4/3 aspect ratio: |
| 78 | + |
| 79 | +```html |
| 80 | +<div class="bg-gray-500 relative w-64"> |
| 81 | + <div class="aspect-ratio-4/3"></div> |
| 82 | + <img class="absolute h-full left-0 object-cover top-0 w-full" src="https://images.unsplash.com/photo-1575470066032-42f6bce0c7fb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80" alt="Unsplash stock image by Josh Hild"> |
| 83 | +</div> |
| 84 | +``` |
| 85 | + |
| 86 | +Embed a YouTube video and restrict it to an aspect ratio of 16/9: |
| 87 | + |
| 88 | +```html |
| 89 | +<div class="bg-gray-400 mx-auto relative w-1/2"> |
| 90 | + <div class="aspect-ratio-16/9"></div> |
| 91 | + <iframe class="absolute h-full left-0 top-0 w-full" width="560" height="315" src="https://www.youtube.com/embed/21HuwjmuS7A" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> |
| 92 | +</div> |
| 93 | +``` |
| 94 | + |
| 95 | +## Output |
| 96 | + |
| 97 | +Tailwind will be outputed as follows. |
| 98 | + |
| 99 | +```css |
| 100 | +.aspect-ratio-square { padding-top: 100%; } |
| 101 | +.aspect-ratio-3\/2 { padding-top: 66.66666666666667%; } |
| 102 | +.aspect-ratio-2\/3 { padding-top: 150%; } |
| 103 | +.aspect-ratio-4\/3 { padding-top: 75%; } |
| 104 | +.aspect-ratio-5\/6 { padding-top: 120%; } |
| 105 | +.aspect-ratio-16\/9 { padding-top: 56.25%; } |
| 106 | +.aspect-ratio-21\/9 { padding-top: 42.86%; } |
| 107 | +``` |
| 108 | + |
| 109 | +## Related plugins |
| 110 | + |
| 111 | +Shout out to [tailwindcss-aspect-ratio](https://github.com/webdna/tailwindcss-aspect-ratio) another plugin that attemps to handle aspect ratios on Tailwind CSS. |
| 112 | + |
| 113 | +## Contributing |
| 114 | + |
| 115 | +Feel free to submit a PR request, and send me a message on Twitter ([@neojp](https://twitter.com/neojp)) about it. |
| 116 | + |
| 117 | +## License |
| 118 | +This project has been licensed under [the Hippocratic License](https://firstdonoharm.dev/). |
0 commit comments