donle
donle

Reputation: 61

Get css value with pixels

How can I get element width or height value which would be translated as a pixel number? All I got by using .css('width') are the expressions, not even a percentage number, like calc(-25px + 50%).

Edit

my code here. (Chrome)

var bars_width = element.css('width');
$('.histgram-content').css('width', bars_width);
bars_width = parseInt(bars_width.replace('px', '')) * 0.85;
$('.histgram-bar').css('width', (bars_width/data.length).toFixed(0).toString() + 'px');
/*** average Y ***/
var graph_height = $('.histgram-graph').css('height');
graph_height = parseInt(graph_height.replace('px', ''));

var average_height = $('.average-line').css('top');
average_height = graph_height - parseInt(average_height.replace('px', ''));

The average_height returns the expression I said. The last line got the result of 'NaN'.

Upvotes: 2

Views: 1656

Answers (3)

Fei Sun
Fei Sun

Reputation: 518

Use window.getComputedStyle(),example: window.getComputedStyle($('div'))['width']

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can use any of jQuery's dimension related methods:

Working example

Upvotes: 2

Suyash
Suyash

Reputation: 176

You can height

$('#element-id').height();

You can width

$('#element-id').width();

Upvotes: 0

Related Questions