Skip to content Skip to sidebar Skip to footer

Get Css Width Of An Element And Convert It To Percentage

i have this code: HTML
CSS .bar-value{ width: 50%; height: 28px; background: #8BBDE8; } JQUERY $(document).ready(function(){

Solution 1:

To get a percentage, you need the width of it's parent. Try this :

$(document).ready(function(){
    var element = $('.bar-value');
    var percent = element.css('width')/element.parent().width()*100+'%';
});

Solution 2:

This question has already been answered here

You have to implement it yourself :

var width = $('.bar-value').css('width');
var parentWidth = $('.bar-value').offsetParent().css('width');
var percent = 100*width/parentWidth;

Post a Comment for "Get Css Width Of An Element And Convert It To Percentage"