Giter Club home page Giter Club logo

tailwindcss-aspect-ratio's Introduction

@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.

tailwindcss-aspect-ratio's People

Contributors

adamwathan avatar bradlc avatar dominicm00 avatar innocenzi avatar kripod avatar reinink avatar robinmalfait avatar saibotk avatar simonswiss avatar skdishansachin avatar thecrypticace avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tailwindcss-aspect-ratio's Issues

production issue

What version of @tailwindcss/aspect-ratio are you using?

^0.2.1

What version of Node.js are you using?

14.1.0

What browser are you using?

Brave

What operating system are you using?

macOS

Reproduction repository

https://merits-products-production.herokuapp.com/p/teams

Describe your issue

If I run npm run production the page layout for aspect ratio does not work however npm run dev this plugin works just fine.

the layout when working should look like the second grid layout on this page from tailwindui.com
https://tailwindui.com/components/marketing/sections/team-sections

Utilities not always getting compiled when using multiple config files

What version of @tailwindcss/aspect-ratio are you using?

v0.4.0

What version of Node.js are you using?

v17.1.0

What browser are you using?

Safari

What operating system are you using?

macOS

Reproduction repository

https://github.com/robdekort/aspect-plugin-issue

Describe your issue

The issue I have started out as a discussion here.

In TW v3 I'm having issues getting (plugin based) aspect utilities compiled.

Reproduce

  1. Clone repo
  2. Run npm i && npm run watch

Note that the aspect-w-, aspect-h- are not being compiled into site.css. They only get compiled when you require the Aspect Ratio plugin in tailwind.config.2.js

Funny thing is this is isn't an issue with the form plugin. That one works wherever I require it. This wasn't an issue for me in v2. Could this be a bug or was it never intended to work like this?

Needs h-0 class on the wrapping div regardless?

Watching https://www.youtube.com/watch?v=NX_NW6bt6_s it seemed we could remove also h-0 from the parent element, however, if I do that, then the aspect ratio is not always respected.

In short, I have a grid of 3 columns, where the 2nd column spans to 2:

<div class="grid grid-cols-3">
  <div class="aspect-w-1 aspect-h-1">
    <img
      class="object-cover rounded-lg shadow-lg"
      src="/assets/img/freud.jpeg"
      alt=""
    />
  </div>
  <p class="col-span-2 font-light">
    <span class="italic font-serif"
      >Some text</p>
</div>

So, ideally, I'd expect the image to appear as a square no matter what the viewport size is, however, check out this screen recording as the screen size gets narrower and narrower:

Screenshot 2021-03-16 at 14 46 33

As you can see, Freud's photo, at some point isn't square anymore!

If I add h-0 to the parent div however, so that the code above becomes:

<div class="grid grid-cols-3">
  <div class="h-0 aspect-w-1 aspect-h-1">
    <img
      class="object-cover rounded-lg shadow-lg"
      src="/assets/img/freud.jpeg"
      alt=""
    />
  </div>
  <p class="col-span-2 font-light">
    <span class="italic font-serif"
      >Some text</p>
</div>

Then it works as advertised:

Screenshot 2021-03-16 at 14 49 59

So, I wonder, is this a bug or am I missing something?

Thanks!

Flexbox issue

Does it work inside of flexbox? I couldn't be successful with this code:

<div className='flex h-screen bg-purple-900 items-center flex-col justify-center w-screen'> <div className='aspect-w-16 aspect-h-9'> <img src='https://picsum.photos/id/237/500/500' className='object-cover object-center' alt='' /> </div> </div>

deprecated - In favor of integrations in core Tailwind 3.0

What version of @tailwindcss/aspect-ratio are you using?

0.4.0

What version of Node.js are you using?

v14.17.2

What browser are you using?

Chrome

What operating system are you using?

Windows

Reproduction repository

Tailwind 3.0.0 now in core have this plugin! Check official docs

Describe your issue

By default this plugin is no longer needed for new version.
Pleas block usage of it or write message and in console.log warm about incompatibility.

Doc:
https://tailwindcss.com/docs/aspect-ratio

Right now Plugin remove default one options and replace with one provided in readme.md (not merge plugin and core options)
This isn't needed as can be used in syntax [] for any variants, new options can be defined in tailwindcss.config.js and no need separate plugin.

Using :after

Hey @adamwathan , thanks for your great work!

I just wanted to ask if you have considered using :after element for the padding and what key points made you decide against it?

I usually go with something like this:

[class*="aspect-ratio"] {
  position: relative;
  display: block;
}
[class*="aspect-ratio"]:after{
  content: '';
  display: block;
  padding-bottom: var(--aspect-ratio, 100%);
}
[class*="aspect-ratio"] > * {
  position: absolute;
}
.aspect-ratio-square {
  --aspect-ratio: 100%;
}

The pros of course are that we don't need an extra parent element.

I assume one con is that u cannot target the :after element with other utilities, also this way the selector is a bit stronger, but, while this is subjective, I cannot think of a common enough scenario that this would be a problem. Anyway you made it with purpose and I am just curious ๐Ÿ˜Š

An unexpected error occurred: "expected hoisted manifest for \"tailwindcss#color\"".

What version of @tailwindcss/aspect-ratio are you using?

v0.4.2

What version of Node.js are you using?

v14.19.1

What browser are you using?

Brave

What operating system are you using?

macOS

Reproduction repository

N/A

Describe your issue

$ yarn add -D @tailwindcss/aspect-ratio
yarn add v1.22.19
[1/4] ๐Ÿ” Resolving packages...
[2/4] ๐Ÿšš Fetching packages...
[3/4] ๐Ÿ”— Linking dependencies...
error An unexpected error occurred: "expected hoisted manifest for "tailwindcss#color"".
info If you think this is a bug, please open a bug report with the information provided in "/Users/sumeetadur/Projects/remote-tech/com.l8log.web/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

GatsbyImage specificity issue with Tailwind aspect-ratio

What version of @tailwindcss/aspect-ratio are you using?

v0.3.0

What version of Node.js are you using?

v16.9.1

What browser are you using?

Chrome

What operating system are you using?

macOS

Reproduction repository

https://github.com/hisnameisjimmy/gatsby-plugin-image-tailwind-specificity

Describe your issue

GatsbyImage css classes are overriding Tailwind aspect-ratio classes when deployed/built for prod. It works as expected in development. I'm not sure if this is the right place to post this, but it doesn't get fixed by setting important: true in tailwind config because aspect-ratio doesn't take on that directive. Would love some advice!

You can see the issue in the following screenshots or by visiting this example site: https://inspiring-hamilton-bd685d.netlify.app/

CleanShot 2021-11-12 at 11 55 25@2x

CleanShot 2021-11-12 at 11 53 43@2x

error An unexpected error occurred: "https://registry.yarnpkg.com/functional-purple-black-tree/-/functional-purple-black-tree-1.0.1.tgz: Request failed \"404 Not Found\"".

What version of @tailwindcss/aspect-ratio are you using?

v.0.2.0

What version of Node.js are you using?

v12.0.0

What browser are you using?

Chrome

What operating system are you using?

macOS

Reproduction repository

no repo

Describe your issue

yarn add v1.22.10
warning package.json: No license field
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
warning [email protected]: No license field
[1/4] ๐Ÿ” Resolving packages...
[2/4] ๐Ÿšš Fetching packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/functional-purple-black-tree/-/functional-purple-black-tree-1.0.1.tgz: Request failed "404 Not Found"".
info If you think this is a bug, please open a bug report with the information provided in ".../yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

Utilize `aspect-ratio` property with fallback

There is a new property coming out that is aspect-ratio which easily accomplishes this. https://web.dev/aspect-ratio/

I believe you can do this with a fallback for the majority of browsers.

div {
  background: lightblue;
  width: 100%;
  
  //   New aspect-ratio property
  aspect-ratio: 16 / 9;
}

// Fallback (current, using padding hack)
@supports not (aspect-ratio: 16 / 9) { 
  div::before {
    float: left;
    padding-top: 56.25%;
    content: '';
  }
  
  div::after {
    display: block;
    content: '';
    clear: both;
  }
}

Should this work with Tailwind 2.0

After upgrading to Tailwind 2.0 it seems that this plugin doesn't produce any classes anymore. Only me? I'm on the PostCSS compatibility version of tw2.0.

Dependency issue with TW v3.0.0-alpha.1

What version of @tailwindcss/aspect-ratio are you using?

v0.2.1

What version of Node.js are you using?

v14.16.1

What browser are you using?

Chrome

What operating system are you using?

macOS

Reproduction repository

https://github.com/sinyayadynya/melon

Describe your issue

I uninstalled tailwindcss-aspect-ratio before upgrading to TailwindCSS v3.0.0-alpha.1 (thought if was fully native), and then tried to reinstall it, but got this error messages:

npm ERR! Could not resolve dependency:
npm ERR! peer tailwindcss@">=2.0.0" from @tailwindcss/[email protected]

Add

What version of @tailwindcss/aspect-ratio are you using?

0.3.0

What version of Node.js are you using?

14.17.0

What browser are you using?

Chrome

What operating system are you using?

MacOS

Reproduction repository

N/A

Describe your issue

From what I can tell, the aspect-ratio property is fully supported in all of the most recent versions of major browsers.

https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio

Is it time to migrate this plugin into the main repo, and away from the padding hack?

Is it time to deprecate this plugin for good?

What version of @tailwindcss/aspect-ratio are you using?

0.2.1

What version of Node.js are you using?

16.x.x

What browser are you using?

Chrome

What operating system are you using?

MacOS

Reproduction repository

github.com/tailwindlabs/tailwindcss-aspect-ratio

Describe your issue

The Readme.md says:

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

Well, the aspect-ratio has made it to all the living browsers (I am not talking walking dead).
Isn't it time then to deliver on the promise ;)?

JIT mode

What version of @tailwindcss/aspect-ratio are you using?

Playground

What version of Node.js are you using?

Playground

What browser are you using?

Chrome

What operating system are you using?

macOS

Reproduction repository

https://play.tailwindcss.com/lja9H4c3N2

Describe your issue

Does the plugin work with JIT mode? I'm trying to use it with custom classes, but it doesn't seem to work

Reduce video size based on viewport height

Thanks for an awesome plugin! I wondered if there's a way to shrink the size of a video based on both the width and height of the viewport, rather than just the width?

So that if a user has a 'short' browser window, the video will shrink in size so that the full height of the video is displayed, rather than get cut off.

Here is a video example of the issue:
https://user-images.githubusercontent.com/67900507/111027123-1f8d4200-83e6-11eb-84ab-801b51e18a85.mov

You'll see at 0.08 the video is cut off when the browser window reduces in height.

An example of the exact functionality I'd like to replicate can be seen by watching a stream on Twitch.tv and reducing the height of the browser - the video shrinks all the way down and doesn't get cut off.

Here is my code:

  <div className="flex flex-col w-0 flex-1 overflow-hidden">
    <RoomTopbar />        
    <div className="flex items-center justify-center">        
      <div className="w-full xl:w-9/12">          
        <div className="aspect-w-16 aspect-h-9">            
          <video className="outline-none" controls>              
            Your browser does not support the video tag.                
          </video>           
        </div>            
      </div>          
    </div>        
  </div>     
</div>

Any help greatly appreciated ๐Ÿ™๐Ÿ˜€

Plugin generated wrong classes

What version of @tailwindcss/aspect-ratio are you using?

v0.3.0

What version of Node.js are you using?

v17.7.1

What browser are you using?

Safari 15.4

What operating system are you using?

macOS

Reproduction repository

https://play.tailwindcss.com/kFPe1ZjEBx

Describe your issue

With aspect ratio plugin Tailwind CSS can generate classes like aspect-1 to aspect-16, I think it's a mistake or I don't understand the use case? ๐Ÿค”

Thank you, Flo.

wrap parent in flex dont show its child with aspect-ratio

I'm trying to put an icon next to the image, to accommodate flex use but when using it with flex, the image disappears

<div class="flex items-center">
  <svg class="mr-0.5 w-6 h-6 text-gray-400 cursor-move">
  </svg>
  <div class="max-w-xl mx-auto shadow-xl">
    <div class="aspect-h-2 aspect-w-3 overflow-hidden rounded-lg"><img src="https://images.unsplash.com/photo-....." class="object-cover w-full h-full" /></div>
  </div>
</div>

showcase:
https://play.tailwindcss.com/17QXVqWIPR

@apply issue

What version of @tailwindcss/aspect-ratio are you using?

v0.2.1

What version of Node.js are you using?

v12.18.3

What browser are you using?

Edge

What operating system are you using?

Windows 10

Reproduction repository

https://github.com/zidandff/tailwind-aspectratio

Describe your issue

If I wrote the class of tailwindcss-aspect-ratio in element attribute is working normally
`


`

however when I want create a component using @apply it doesn't work
.card { @apply aspect-w-9 aspect-h-16; }

but when use aspect-w-1 aspect-h-1 it's working normally again
.card { @apply aspect-w-1 aspect-h-1; }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.