Skip to content Skip to sidebar Skip to footer

Width Equal To Dynamic Height Square

Is it possible with either css or js to set the width of an element to match it's height when the height is a % of the parent div. The child div also needs to change size if browse

Solution 1:

If you are using jQuery, you can set the width = height or vice-versa in the resize() function.

http://jsfiddle.net/Ev6Vj/173/

$(window).resize(function() {
  $('#main').height($('#main').width());
});
<div id="main">
    <img src="http://placehold.it/200x200" />
</div>
#main {
    position: absolute;
    bottom: 0;
    top: 0;
    border: #000 thin solid;
    background: #DDEEFF;
    width: 100%;
}

Solution 2:

$(document).ready(function()
{
    $(window).resize(function()
    {
        $("div").height($("div").width());
    });
    $(window).resize();
});

You can do something like this using jQuery to match the height and width of an element.


Solution 3:

Use jquery.

function resizeChild() {
var child = $(".child");
var parentSize = child.parent().height();
child.height(parentSize / 2);
child.width(parentSize / 2);
}
$(window).ready(resizeChild);
$(window).resize(resizeChild);

see fiddle


Post a Comment for "Width Equal To Dynamic Height Square"