Skip to content

Instantly share code, notes, and snippets.

@ilomon10
Created January 17, 2021 03:16
Show Gist options
  • Save ilomon10/fa533fd9ec1032872c118e95799e3946 to your computer and use it in GitHub Desktop.
Save ilomon10/fa533fd9ec1032872c118e95799e3946 to your computer and use it in GitHub Desktop.
Aspect ratio react component
import React from 'react';
const AspectRatio = ({ children, className = "", ratio, style = {} }) => {
const r = ratio.split(":");
const val = 100 / parseFloat(r[0]) * parseFloat(r[1]);
return (
<div className={className} style={{ position: 'relative', width: "100%", paddingTop: `${val}%`, ...style }}>
<div style={{ position: 'absolute', top: 0, left: 0, height: "100%", width: "100%" }}>{children}</div>
</div>
)
};
export default AspectRatio;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment