search
HomeWeb Front-endCSS TutorialA First Look at `aspect-ratio`

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

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; */
}

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);
}

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
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
State of Devs: A Survey for Every DeveloperState of Devs: A Survey for Every DeveloperMay 07, 2025 am 09:30 AM

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more. 

What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

See all articles

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software