2

Lets say I have:

.item {
    width: 33%;
}

The thing is that if with jQuery I have to retrieve width:

$('.item').first().css('width')

It returns the actual width in pixels,

How can I get the % ? Do I have to calculate based on the actual parent's width?

Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

1 Answers1

1

you have to do calculations

100 * (element width / parent element width)

var el = $('.item').first(),
    width = (100 * parseFloat(el.css('width')) / parseFloat(el.parent().css('width'))) + '%';

or

var el = $('.item').first(),
    width = (100 * parseFloat(el.width()) / parseFloat(el.parent().width())) + '%';
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107