New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-aspect-ratio

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-aspect-ratio

React Aspect Ratio Component

  • 1.0.46
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.7K
increased by6.77%
Maintainers
1
Weekly downloads
 
Created
Source

React Aspect Ratio

reac aspect ratio
npm gzip size downloads circleci


This is a React implementation for aspect ratio placeholder preventing browser reflow before browser downloads and renders your component.

Demo

Inspired by Thierry

Original idea from Sérgio Gomes

You can also read a detail post by Chris Coyier

Why

Most common use case is image loading. If you are not define dimensions for your image tag, browser will assume its a square size of image before image loaded. Hence you will see browser reflow your layout after image loaded.

If you define a hard dimensions, it might not fit a responsive design.

* Chrome is developing Intrinsic size attribute which potentially will act the same as what this library does. However, its very early stage and no other vendors showing signals of development yet. (Spec deprecated)

How

This component using what people call "Padding trick" - creating a wrapper html tag with zero height and a percentage of padding-bottom to perserve space. (padding-bottom will be percentage of your component width).

This library also utilizes CSS variable for modern browser as well as CSS calc API to minimized the style needed for different padding value.

Installation

via yarn

$ yarn add react-aspect-ratio

or via npm

$ npm install react-aspect-ratio

Usage

Props

PropsTypeDefaultDescription
ratiostring/number1Aspect ratio of your component, could be number or string like width/height
other propsObject{style: {--aspect-ratio: ${ratio}} }Any props to your React component, the library will add --aspect-ratio to your style object
childrenReact ElementSingle DOM element

You will need to import 'react-aspect-ratio/aspect-ratio.css'

import AspectRatio from 'react-aspect-ratio';

const RatioImage = () => (
  <AspectRatio ratio="3/4" style={{ maxWidth: '400px' }}>
    <img src="https://c1.staticflickr.com/4/3896/14550191836_cc0675d906.jpg" />
  </AspectRatio>
);
import AspectRatio from 'react-aspect-ratio';

const RatioIframe = () => (
  <AspectRatio ratio="560/315" style={{ maxWidth: '560px' }}>
    <iframe src="https://www.youtube.com/embed/Bku71V5f66g" frameBorder="0" allowFullScreen />
  </AspectRatio>
);

Can also use for background image

import AspectRatio from 'react-aspect-ratio';

<AspectRatio
  ratio={0.75}
  style={{
    maxWidth: '300px',
    backgroundImage: 'url(https://c1.staticflickr.com/4/3896/14550191836_cc0675d906.jpg)',
    backgroundSize: 'cover'
  }}
/>;

CSS (By Thierry)

[style*="--aspect-ratio"] > :first-child {
  width: 100%;
}
[style*="--aspect-ratio"] > img {  
  height: auto;
}
@supports (--custom:property) {
  [style*="--aspect-ratio"] {
    position: relative;
  }
  [style*="--aspect-ratio"]::before {
    height: 0;
    content: "";
    display: block;
    padding-bottom: calc(100% / (var(--aspect-ratio)));
  }  
  [style*="--aspect-ratio"] > :first-child {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
  }  
}
  • We use [style*="--aspect-ratio"] as a hook to target the appropriate boxes
  • We stretch the inner box regardless of support for custom property
  • We make sure the height of images comes from their intrinsic ratio rather than their height attribute
  • We style the container as a containing block (so the inner box references that ancestor for its positioning)
  • We create a pseudo-element to be used with the “padding hack” (it is that element that creates the aspect ratio)
  • We use calc() and var() to calculate padding based on the value of the custom property
  • We style the inner box so it matches the dimensions of its containing block

Keywords

FAQs

Package last updated on 23 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc