I love using EQCSS, a unique tool that allows me to extract data from JavaScript and incorporate it into my CSS styling. Take a look at this example featuring a column that is 33% wide with a resizable square inside:
<section>
<div></div>
</section>
<style>
section {width: 33%;}
@element 'div' {
$this {
width: auto;
height: eval("clientWidth")px;
background: red;
}
}
</style>
<script src=http://elementqueries.com/EQCSS.js></script>
The use of width: auto
ensures the square fills its container. The eval('clientWidth')
statement within the element query specifically refers to the width of the element itself, resulting in a square shape where the height matches the width.
Explore more at:
I apply a similar approach to dynamically adjust the size of Youtube and Vimeo iframes based on their aspect ratio without requiring additional wrapping elements:
@element 'iframe' {
$this {
margin: 0 auto;
width: 100%;
height: eval("scrollWidth/(width/height)")px;
}
}
Witness responsive video scaling in action:
This method has been incredibly useful for me, and I hope you find it valuable too!