Skip to content

Instantly share code, notes, and snippets.

@celsowhite
Last active January 22, 2019 20:58
Show Gist options
  • Save celsowhite/005e04ac488edeed9f91a30820c2a78e to your computer and use it in GitHub Desktop.
Save celsowhite/005e04ac488edeed9f91a30820c2a78e to your computer and use it in GitHub Desktop.
Example of a proportionally sized div. Div automatically keeps proportions regardless of container width.
<div class="product_card">
<div class="product_image" style="background-image: url('https://celsowhite.com/static/img/about/me.jpg')">
<!-- Important: Any content in this proportional div must be positioned absolute so it adapts to the container proportions. -->
</div>
<div class="product_info">
<h2>Celso</h2>
</div>
</div>
body {
box-sizing: border-box;
}
// Structure
.product_card {
width: 50%;
}
// Image Portion (Stays proportional because of the pseudo after element. That can be replaced for a before element.)
.product_image {
background-size: cover;
background-position: center;
}
// Magic that makes it proportional.
.product_image:after {
content: "";
display: block;
padding-bottom: 60%; // Use this https://calculateaspectratio.com/ to calculate a percentage based on the desired ratio.
}
// Relative info container below image. Not proportional.
.product_info {
background: white;
width: 100%;
}
.product_info h2 {
margin: 0;
padding: 20px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment