I’m trying to make a responsive square with the width size based on the (100%) height of the element. I believe it’s impossible using only CSS.
The square width should be equal to the height (100% of the large container. The large container is more than 100% of the screen). The ratio has to be width=height to keep the square.
Advertisement
Answer
Ok here the solution.
JavaScript
x
13
13
1
<div id="square" style="background-color:black;height:100%">test</div>
2
3
$(window).ready(updateWidth);
4
$(window).resize(updateWidth);
5
6
function updateWidth()
7
{
8
var square = $('#square');
9
var size = square.height();
10
11
square.css('width',size);
12
}
13