takamoso / postcss-aspect-ratio-polyfill

A PostCSS plugin to support aspect-ratio property.
MIT License
28 stars 8 forks source link

PostCSS Aspect Ratio Polyfill

A PostCSS plugin to support aspect-ratio property.

Install

npm i -D postcss-aspect-ratio-polyfill

Usage

const postcss = require('postcss')

const output = postcss()
  .use(require('postcss-aspect-ratio-polyfill'))
  .process(require('fs').readFileSync('input.css', 'utf8'))
  .css

Basis

input:

.aspect-ratio-box {
  aspect-ratio: 16 / 9;
}

output:

.aspect-ratio-box::before {
  float: left;
  padding-top: 56.25%;
  content: '';
}
.aspect-ratio-box::after {
  display: block;
  content: '';
  clear: both;
}

With object-fit

<div class="aspect-ratio-box">
  <img src="https://picsum.photos/1280/720">
</div>

input:

.aspect-ratio-box {
  position: relative;
  max-width: 500px;
  aspect-ratio: 16 / 9;
}
.aspect-ratio-box > img {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

output:

.aspect-ratio-box {
  position: relative;
  max-width: 500px;
}
.aspect-ratio-box::before {
  float: left;
  padding-top: 56.25%;
  content: '';
}
.aspect-ratio-box::after {
  display: block;
  content: '';
  clear: both;
}
.aspect-ratio-box > img {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}