I have following responsive pattern:
In demo, all blocks have same height but width could be either 25% or 50% (it changes on lower resolutions but that is not relevant in this case). Since width is fluid, to keep aspect ratio, I used padding hack. My problem is setting correct padding size for double width elements.
To calculate padding for regular element I use percentage from formula x/y where x is height and y is width based on design mockup.
To calculate percentage for double sized element I use formula x/(2*y + 2*z) where z is size of gutter between elements (6px in demo example).
This code seems to work fine until I start resizing window and on some points layout breaks because normal and double sized elements have different heights. I suspect this happens because of how browser calculates percentage values. So my question is: is it possible to adjust calculation in my code to always get correct aspect ratio regardless of element widh?
relevant code:
HTML
<ul class="u-cf">
<li class="double"></li>
<li></li>
<li></li>
<li class="double"></li>
<li></li>
<li></li>
</ul>
CSS (Sass)
ul {
margin: 0;
padding: 6px;
list-style: none;
clear: both;
}
li {
position: relative;
padding: 6px;
width: 25%;
float: left;
&:before {
content: "";
padding-top: percentage(280/405);
display: block;
height: 0;
}
}
.double {
width: 50%;
&:before {
padding-top: percentage(280/822); // 2*405px + 2*6px
}
}