Search code examples
javascriptjqueryinline-code

jQuery Get Image Dimensions, Apply To Div


I want a div to get an inline style of the image it wraps around.

Pen here: http://codepen.io/stevenmorgan94/pen/xuEwm

HTML

<div class="box">
  <img src="http://placehold.it/250x150" />
</div>

JS

var img = $(".box > img");
$(".box").css({width:img.width(), height:img.height()});

So jquery get image dimensions, add inline style to .box with the width and height


Solution

  • jonathan didn't get it right. His solution not about what was asked. Correct one is

    var img = $(".box > img");
    $(".box").css({width:img.width(), height:img.height()});