Created
March 26, 2015 12:55
-
-
Save JoeKeikun/b078035d0ad0ec1e72e1 to your computer and use it in GitHub Desktop.
Use CSS to Specify the Aspect Ratio DIV
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
[HTML] | |
<div class='box'> | |
<div class='content'>Aspect ratio of 1:1</div> | |
</div> | |
[CSS] | |
.box{ | |
position: relative; | |
width: 50%; /* desired width */ | |
} | |
.box:before{ | |
content: ""; | |
display: block; | |
padding-top: 100%; /* initial ratio of 1:1*/ | |
} | |
.content{ | |
position: absolute; | |
top: 0; | |
left: 0; | |
bottom: 0; | |
right: 0; | |
} | |
---------------------------------- | |
OTHER ASPECT RATIOS | |
If you want to create other ratios just change the padding-top value of the pseudo element (see example): | |
/* Other ratios */ | |
.ratio2_1:before{ | |
padding-top: 50%; | |
} | |
.ratio1_2:before{ | |
padding-top: 200%; | |
} | |
.ratio4_3:before{ | |
padding-top: 75%; | |
} | |
.ratio16_9:before{ | |
padding-top: 56.25%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment