Convert Figma logo to code with AI

tailwindlabs logotailwindcss-aspect-ratio

No description available

1,000
35
1,000
0

Top Related Projects

A utility-first CSS framework for rapid UI development.

A better base for styling form elements with Tailwind CSS.

Beautiful typographic defaults for HTML you don't control.

A plugin that provides a basic reset for form styles that makes form elements easy to override with utilities.

A plugin that provides utilities for visually truncating text after a fixed number of lines.

A plugin for Tailwind CSS v3.2+ that provides utilities for container queries.

Quick Overview

Error generating quick overview

Competitor Comparisons

A utility-first CSS framework for rapid UI development.

Pros of tailwindcss

  • Comprehensive utility-first CSS framework with a wide range of pre-built classes
  • Highly customizable and extensible through configuration
  • Active development and large community support

Cons of tailwindcss

  • Larger file size due to its extensive feature set
  • Steeper learning curve for developers new to utility-first CSS
  • May require additional setup and configuration for optimal use

Code Comparison

tailwindcss:

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

tailwindcss-aspect-ratio:

<div class="aspect-ratio-16/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>

Summary

tailwindcss is a full-featured CSS framework offering a wide range of utilities, while tailwindcss-aspect-ratio is a focused plugin specifically for handling aspect ratios. The main framework provides more flexibility and features but comes with increased complexity, while the aspect ratio plugin offers a simpler, targeted solution for maintaining consistent element proportions.

A better base for styling form elements with Tailwind CSS.

Pros of tailwindcss-custom-forms

  • Provides a comprehensive set of form element styles
  • Offers easy customization options for form elements
  • Integrates seamlessly with existing Tailwind CSS classes

Cons of tailwindcss-custom-forms

  • Larger file size due to more comprehensive styling options
  • May require more configuration to achieve desired form styles
  • Less focused on a single specific feature

Code Comparison

tailwindcss-custom-forms:

<input class="form-input mt-1 block w-full" type="text">
<select class="form-select mt-1 block w-full">
  <option>Option 1</option>
  <option>Option 2</option>
</select>

tailwindcss-aspect-ratio:

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

The tailwindcss-custom-forms plugin focuses on enhancing form elements with customizable styles, while tailwindcss-aspect-ratio provides a simple solution for maintaining aspect ratios of elements. The custom-forms plugin offers more versatility for form styling but may require more setup, whereas the aspect-ratio plugin solves a specific layout challenge with minimal configuration.

Beautiful typographic defaults for HTML you don't control.

Pros of tailwindcss-typography

  • Provides a comprehensive set of typography-related utility classes
  • Offers easy customization of text styles for different content types
  • Includes responsive typography options out of the box

Cons of tailwindcss-typography

  • Larger file size due to more extensive utility classes
  • May require more learning curve for developers new to typography concepts
  • Could potentially lead to more complex HTML markup

Code Comparison

tailwindcss-typography:

<article class="prose lg:prose-xl">
  <h1>Heading 1</h1>
  <p>This is a paragraph with styled typography.</p>
</article>

tailwindcss-aspect-ratio:

<div class="aspect-w-16 aspect-h-9">
  <img src="image.jpg" alt="16:9 aspect ratio image">
</div>

The tailwindcss-typography plugin provides a more comprehensive set of utility classes for styling text content, while tailwindcss-aspect-ratio focuses specifically on maintaining aspect ratios for elements like images and videos. The typography plugin offers more flexibility for text-heavy content, whereas the aspect-ratio plugin is more specialized for layout purposes.

A plugin that provides a basic reset for form styles that makes form elements easy to override with utilities.

Pros of tailwindcss-forms

  • Provides comprehensive styling for form elements, including inputs, selects, and checkboxes
  • Offers a consistent look across different browsers and platforms
  • Easily customizable through Tailwind's configuration system

Cons of tailwindcss-forms

  • Larger file size due to more comprehensive styling
  • May require more overrides for highly custom designs
  • Not focused on a single specific functionality like aspect ratio

Code Comparison

tailwindcss-forms:

@layer base {
  [type='text'],
  [type='email'],
  [type='url'],
  [type='password'],
  [type='number'],
  [type='date'],
  [type='datetime-local'],
  [type='month'],
  [type='search'],
  [type='tel'],
  [type='time'],
  [type='week'],
  [multiple],
  textarea,
  select {
    @apply w-full border-gray-300 rounded-md shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50;
  }
}

tailwindcss-aspect-ratio:

@responsive {
  .aspect-w-1,
  .aspect-w-2,
  .aspect-w-3,
  .aspect-w-4,
  .aspect-w-5,
  .aspect-w-6,
  .aspect-w-7,
  .aspect-w-8,
  .aspect-w-9,
  .aspect-w-10,
  .aspect-w-11,
  .aspect-w-12,
  .aspect-w-13,
  .aspect-w-14,
  .aspect-w-15,
  .aspect-w-16 {
    position: relative;
    padding-bottom: calc(var(--tw-aspect-h) / var(--tw-aspect-w) * 100%);
  }
}

The code snippets show that tailwindcss-forms focuses on styling form elements, while tailwindcss-aspect-ratio provides utility classes for maintaining aspect ratios of elements.

A plugin that provides utilities for visually truncating text after a fixed number of lines.

Pros of tailwindcss-line-clamp

  • Specifically designed for text truncation, offering a more focused solution for this common use case
  • Provides a simple and intuitive way to limit the number of lines of text displayed
  • Automatically handles ellipsis and cross-browser compatibility

Cons of tailwindcss-line-clamp

  • Limited to text-related styling, whereas tailwindcss-aspect-ratio has broader layout applications
  • May require additional CSS for more complex text truncation scenarios
  • Less flexibility in terms of responsive design compared to aspect ratio utilities

Code Comparison

tailwindcss-line-clamp:

<p class="line-clamp-3">
  This text will be truncated after three lines...
</p>

tailwindcss-aspect-ratio:

<div class="aspect-w-16 aspect-h-9">
  <img src="example.jpg" alt="Example image" />
</div>

Both plugins offer concise and easy-to-use utility classes, but they serve different purposes. tailwindcss-line-clamp focuses on text truncation, while tailwindcss-aspect-ratio provides a solution for maintaining consistent aspect ratios in layouts. The choice between the two depends on the specific design requirements of your project.

A plugin for Tailwind CSS v3.2+ that provides utilities for container queries.

Pros of tailwindcss-container-queries

  • Enables responsive design based on container size, not just viewport size
  • Allows for more flexible and modular component design
  • Supports modern CSS container queries specification

Cons of tailwindcss-container-queries

  • Requires more complex setup and configuration
  • May have limited browser support compared to aspect ratio utilities
  • Potentially steeper learning curve for developers new to container queries

Code Comparison

tailwindcss-container-queries:

<div class="@container">
  <div class="@lg:text-2xl @sm:text-base">
    Responsive text based on container size
  </div>
</div>

tailwindcss-aspect-ratio:

<div class="aspect-w-16 aspect-h-9">
  <img src="example.jpg" alt="16:9 aspect ratio image">
</div>

The tailwindcss-container-queries plugin provides a more dynamic approach to responsive design, allowing elements to adapt based on their container's size. This offers greater flexibility in creating modular components that can adjust their layout independently of the viewport.

On the other hand, tailwindcss-aspect-ratio focuses specifically on maintaining consistent aspect ratios for elements, which is particularly useful for images and video embeds. It's simpler to implement and has broader browser support.

While tailwindcss-container-queries offers more advanced responsive capabilities, it may require more setup and have a steeper learning curve. tailwindcss-aspect-ratio provides a straightforward solution for a specific layout need, making it easier to use for developers who primarily need to maintain aspect ratios in their designs.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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

WidthHeight
aspect-w-1aspect-h-1
aspect-w-2aspect-h-2
aspect-w-3aspect-h-3
aspect-w-4aspect-h-4
aspect-w-5aspect-h-5
aspect-w-6aspect-h-6
aspect-w-7aspect-h-7
aspect-w-8aspect-h-8
aspect-w-9aspect-h-9
aspect-w-10aspect-h-10
aspect-w-11aspect-h-11
aspect-w-12aspect-h-12
aspect-w-13aspect-h-13
aspect-w-14aspect-h-14
aspect-w-15aspect-h-15
aspect-w-16aspect-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.

NPM DownloadsLast 30 Days