[Demo] Get Width of Element in Percentage

[Demo] Get Width of Element in Percentage

by 20yco

HTML

<div class="xyz">Hi</div>

CSS

.xyz {
  width: 3%;
  background-color: red;
}

JavaScript

var width = $('.xyz').width();
var parentWidth = $('.xyz').offsetParent().width();
var percent = Math.round(100 * width / parentWidth);

alert(percent);