Table of Contents
Using aspect-ratio on elements alone will calculate the height based on the automatic width.
If the content exceeds the aspect ratio, the element will still expand.
If an element has one of height or width , the other is calculated based on the aspect ratio.
If the element has both height and width , then aspect-ratio is ignored.
Use value functions
Maybe it would be helpful to think of aspect-ratio as the weakest way to determine the size of an element?
Home Web Front-end CSS Tutorial A First Look at `aspect-ratio`

A First Look at `aspect-ratio`

Apr 05, 2025 am 09:57 AM

A First Look at `aspect-ratio`

CSS ushers in a new attribute that affects the size of the box - aspect-ratio ! This is a big event. While there are already many ways to create boxes with specified aspect ratios (I think a solution based on custom properties is the best), none is particularly intuitive and certainly not as simple as declaring a single property.

With aspect-ratio attribute (MDN, not to be confused with media query versions) coming, I want to look into how it works and try to understand it.

Thanks to Una for letting me see this property first, it really attracted a lot of people. Here are some of my attempts.

Using aspect-ratio on elements alone will calculate the height based on the automatic width.

Without setting the width, the element still has a natural automatic width. Therefore, the height can be calculated based on the aspect ratio and render width.

 .el {
  aspect-ratio: 16 / 9;
}
Copy after login

If the content exceeds the aspect ratio, the element will still expand.

In this case, the aspect ratio will be ignored, which is actually good. This is why pseudo-element strategy is popular for aspect ratios, as it does not lead to dangerous data loss or awkward overlap when there is too much content.

However, if you want to limit the height to the aspect ratio, you can do it by adding min-height: 0;

If an element has one of height or width , the other is calculated based on the aspect ratio.

So aspect-ratio is basically a way to set another dimension when you have only one dimension.

If the element has both height and width , then aspect-ratio is ignored.

An explicit combination of height and width is "stronger" than aspect ratio.

Consider min-* and max-*

There is always some tension between width, minimum width, and maximum width (or height version). One of them always "wins". Usually this is intuitive.

If you set width: 100px; and min-width: 200px; , min-width will win. So min-width is either ignored (because you've already surpassed it) or won. The same is true for max-width : if you set width: 100px; and max-width: 50px; , max-width will win. So max-width is either ignored (because you are already below it) or won.

It seems that this common intuition applies here too: min-* and max-* attributes either win or don't matter. If they win, the aspect ratio will be destroyed.

 .el {
  aspect-ratio: 1 / 4;
  height: 500px;

  /* is ignored because the width is calculated as 125px */
  /* min-width: 100px; */

  /* Win, making aspect ratio 1 / 2 */
  /* min-width: 250px; */
}
Copy after login

Use value functions

In fluid situations or at any time when you basically don't know a dimension in advance, aspect ratios are always the most useful. But even if you don't know, you often impose constraints on things. For example, 50% width is good, but you just want to shrink it down to 200px. You can use width: max(50%, 200px); . Or use clamp(200px, 50%, 400px); to impose constraints on both sides.

This looks intuitive:

 .el {
  aspect-ratio: 4 / 3;
  width: clamp(200px, 50%, 400px);
}
Copy after login

But, suppose you encounter a minimum value of 200px and then apply min-width of 300px? min-width wins. It's still intuitive, but it can be confusing because of the many properties, functions, and values ​​involved.

Maybe it would be helpful to think of aspect-ratio as the weakest way to determine the size of an element?

It never outweighs any other size information, but if there is no other available information in the dimension, it will always perform its resizing.

The above is the detailed content of A First Look at `aspect-ratio`. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

See all articles