Created
January 17, 2021 03:16
-
-
Save ilomon10/fa533fd9ec1032872c118e95799e3946 to your computer and use it in GitHub Desktop.
Aspect ratio react component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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