Aspect Ratios

@sgroup/tailwind-plugins/aspect-ratios

Use the ratio helpers to manage the aspect ratios images, background images and external content like <iframe>s and <video>s.

You can use any values to determine the width and height ratio, up to 21. Use the .aspect-2x4 representing the width by height value.

Images

Because <img> elements don't support the ::before pseudo element, we require a wrapper element.

Images - 21x6

You'll notice that the image has been squished to the aspect ratio. You can change this be adding object-* classes to the <img> element.

Images with object constraints

Background images

Background images should use the img-* helper classes.

Background Images - 21x4

Videos

Video - 16x9

Standard ratios

You can technically craft any ratio combination you like, but some industry-standard ones are:

21:9
16:9
4:3
1:1

Responsive

You might want to have a set ratio for images on desktop and tablet, but want to un-constrain media when at mobile. You can use aspect-none at breakpoints to cater for this.

Mobile no aspect ratio
Desktop no aspect ratio

Configuration

Configure the @sgroup/tailwind-plugins/aspect-ratios plugin using theme options.

theme: {
    // Completely override the aspect ratios
    aspectRatio: {
        1: '1',
        2: '2',
        3: '3',
        4: '4',
        6: '6',
        9: '9',
        21: '21',
    },

    // Add responsive and hover variants
    variants: {
        aspectRatio: ['responsive', 'hover'],
    },
},