Last active
December 6, 2015 03:51
-
-
Save ehgoodenough/7090b36433cbc700fdc7 to your computer and use it in GitHub Desktop.
I wrote this mixin to fix an element to an absolute aspect ratio. It resizes the element relative to the viewport, or ``vw`` and ``vh``.
This file contains hidden or 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
#frame { | |
top: 0rem; | |
left: 0rem; | |
right: 0rem; | |
bottom: 0rem; | |
margin: auto; | |
position: fixed; | |
overflow: hidden; | |
} | |
@media(min-aspect-ratio: 640 / 360) { | |
#frame { | |
font-size: 0.27778vh; | |
width: 177.77778vh; | |
height: 100vh; | |
} | |
} | |
@media(max-aspect-ratio: 640 / 360) { | |
#frame { | |
font-size: 0.15625vw; | |
height: 56.25vw; | |
width: 100vw; | |
} | |
} |
This file contains hidden or 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
@mixin aspect-ratio($width, $height) { | |
top: 0rem; | |
left: 0rem; | |
right: 0rem; | |
bottom: 0rem; | |
margin: auto; | |
position: fixed; | |
overflow: hidden; | |
@media(min-aspect-ratio: #{$width + "/" + $height}) { | |
font-size: (($width/$height)*100vh)/$width; | |
width: ($width/$height)*100vh; | |
height: 100vh; | |
} | |
@media(max-aspect-ratio: #{$width + "/" + $height}) { | |
font-size: (($height/$width)*100vw)/$height; | |
height: ($height/$width)*100vw; | |
width: 100vw; | |
} | |
} | |
#game-frame { | |
@include aspect-ratio(640, 360); | |
} |
Any children can be positioned in units of em
instead of px
, since the font-size
is likewise relative to the viewport.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want, you can support multiple aspect ratios by assigning each to a different class, and then dynamically switching between them.