1

I'm trying to set the li's height based its width. I used JQuery for that.

var width = $('li').width();
$('li').css('height', width + 'px');

The problem is, it's not working. It's not the same exact size.

JSFiddle

$(document).ready(function () {
 "use strict";
 
 var width = $('li').width();
 
 $('li').css({
  'height': width + 'px',
        'background-color': 'green',
 });
    
    $(window).resize(function(){
     $('li').height(width);
 });
});
html, body {margin: 0; height: 100%}

div {
 height: 100%;
 display: flex;
 justify-content: center;
 align-items: center;
 overflow: auto;
}

ul {
 padding: 0;
 margin: auto;
 width: 70%;
 
 display: inline-flex;
 flex-flow: row wrap;
 justify-content: flex-start;
 align-items: flex-start;
 align-items: center;
 align-content: center;
}

li {
 list-style-type: none;
 border: 1px solid tomato;
 box-sizing: border-box;
 flex-basis: calc(100%/3);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <ul>
     <li>1</li>
        <li>2</li>
        <li>3</li>
     <li>4</li>
    </ul>
</div>
Jessica
  • 9,379
  • 14
  • 65
  • 136

2 Answers2

1

May be, this is a hack, but try this:

$(document).ready(function () {
    "use strict";

    var width = $('li').width();

    $('li').css({
        'height': width,
        'width': width,
        'background-color': 'green'
    });
    $("li").height($("li").width());
    $("li").height($("li").width());

    $(window).resize(function () {
        var width = $('li').width();        
        $('li').css({
            'height': width,
            'width': width,
            'background-color': 'green'
        });
        $("li").height($("li").width());
        $("li").height($("li").width());
    });
});

Fiddle: http://jsfiddle.net/pu5xomhc/1/

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

jquery width() rounds the value to an integer. To avoid that i'm using getBoundingClientRect() which support decimals.

Try this:

$(document).ready(function () {
    "use strict";

    var elem = $("li")
    var width = elem[0].getBoundingClientRect().width

    $('li').outerHeight(width);



});

Since you asked to resize when window size changes:

   $(window).resize(function(){
        var elem = $("li")
        var width = elem[0].getBoundingClientRect().width

        $('li').outerHeight(width);
    });
gidim
  • 2,314
  • 20
  • 23
  • What's the difference in doing `$('li').outerHeight(width)` and setting it with css? – Jessica Oct 26 '15 at 23:37
  • outerHeight includes padding and border, height doesn't. the value returned from getBoundingClientRect() includes your border – gidim Oct 26 '15 at 23:45
  • In css, things are work dynamically. (Like percentage.) Is there a way to do the same thing when editing css through JQuery? Meaning, when the `li's` width changes, is there a way to make the height also change, dynamically? – Jessica Oct 27 '15 at 01:33
  • yes, i've added an example to the post. Please accept if my answer is correct. – gidim Oct 27 '15 at 01:51
  • That's only when the actual window resizes, not when the `li`resizes – Jessica Oct 27 '15 at 01:57
  • that's a new question - see this http://stackoverflow.com/questions/9628450/jquery-how-to-determine-if-a-div-changes-its-height-or-any-css-attribute – gidim Oct 27 '15 at 02:21