takamoso / postcss-aspect-ratio-polyfill Goto Github PK
View Code? Open in Web Editor NEWA PostCSS plugin to support aspect-ratio property.
License: MIT License
A PostCSS plugin to support aspect-ratio property.
License: MIT License
This polyfill only work with width based elements
example:
<div class="width-based aspect-ratio-box"></div>
<div class="height-based aspect-ratio-box"></div>
.width-based {
display: inline-block;
border: 1px solid blue;
width: 80px;
}
.height-based {
display: inline-block;
border: 1px solid red;
height: 80px;
}
.aspect-ratio-box::before {
float: left;
padding-top: 56.25%;
content: '';
}
.aspect-ratio-box::after {
display: block;
content: '';
clear: both;
}
the 2nd element have a 0px width
This gets polyfilled correctly:
.image-container-1 {
aspect-ratio: 1 / 1
}
.image-container-2 {
aspect-ratio: 1 / 1;
}
This does not:
.image-container-1, .image-container-2 {
aspect-ratio: 1 / 1;
}
This polyfill unnecessarily removes and replaces aspect-ratio
even in browsers where it is supported.
I suggest that the original aspect-ratio
property is left intact, and the polyfill-generated code is wrapped in a @supports not
instruction.
https://developer.mozilla.org/en-US/docs/Web/CSS/@supports
Input:
.aspect-ratio-box {
aspect-ratio: 16 / 9;
}
Expected output:
.card-wide {
aspect-ratio: 16 / 9;
}
@supports not (aspect-ratio: 1/1) {
.aspect-ratio-box::before {
float: left;
padding-top: 56.25%;
content: '';
}
.aspect-ratio-box::after {
display: block;
content: '';
clear: both;
}
}
Note: 1/1
is used only as a test value to check whether aspect-ratio
is supported at all, it does not affect the resulting layout.
When using the CSS calc()
function and variables to calculate aspect-ratio
in my source CSS, the rule fails to compile properly
Input source:
.card-cell {
--card-width: 1;
--card-height: 1;
aspect-ratio: calc(var(--card-width) / var(--card-height));
}
Output:
.card-cell::before {
float: left;
padding-top: calc(var(--card-width)NaN%var(--card-height));
content: '';
}
Seems that this library doesn't work well when passing a single value to aspect-ratio
which should be possible according to the docs:
If height and the preceding slash character are omitted, height defaults to 1
Since padding-top
requires a percentage value, this can be fixed easily by multiplying the value by 100%
inside of another calc()
, which results in working code.
Expected output:
.card-cell::before {
float: left;
padding-top: calc( calc(var(--card-width) / var(--card-height) ) * 100%);
content: '';
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.