Question changed for clarity: I know how to get the value for a css property of an element. However, when the unit used is vw
, the string returned by .css()
is converted to px
. My question is: how could I make sure jQuery returns the actual value and unit of the property without converting them to px
?

- 181
- 1
- 3
- 11
-
Excuse me, but you are wrong. I have researched the subject and tried many different options, but I haven't found a way to select DOM elements according to the unit used to define a css property for such element. I do not post code because I don't yet have anything useful as a starting point. – cVergel Oct 23 '17 at 04:10
1 Answers
If you know the value of the property is in vw
units, you can search for this in the string.
Consider:
var prop = $('.selector').css('width');
prop
will contain a string value with the calculated width. This is not the case for .width()
which will return an integer value that represents the number of pixels.
Now we check for your condition. If the width is '20vw', we just examine the string and test for 'vw':
if($('.selector').css('width').slice(-2) == "vw"){
// Do a thing
}
If you have a larger group, you may want to make use of .each()
. Hope that helps.
Update
Review: Select all elements that have a specific CSS, using jQuery
You cannot (using a CSS selector) select elements based on the CSS properties that have been applied to them.
If you need to check elements, could try a few things:
var elemList = [];
$("*:not(html, head, meta, title, script, link, style, body)").each(function(ind, elem) {
/* Perform test
if(){
elemList.push($(elem));
}
*/
});
If you can add to your code, you could add a class, .vw
to each of the elements that uses 'vw'. Then you can easily select them all at once: $(".vw")
.
Using jQuery you can only get the calculated value.
Get actual value specified in CSS using jQuery
This means the values, regardless if they were specified using px
, em
, or vw
will be returned to pixel values.

- 30,304
- 2
- 26
- 45
-
Thanks, this might be a useful start, but it's the selector part which I need. What I mean is that I need to be able to operate on all css properties using vw units, site-wide, with a single loop or operation. If I have to define the selector for each element and property, I might as well define the changes in a separate stylesheet and not use jQuery. This is because many different types of elements use vw, and the properties range from widths and heights to font-sizes and line-heights. – cVergel Oct 23 '17 at 04:05
-
What I am trying to achieve is a scaling function that, when the page reaches certain breakpoints, will operate on all vw units to change the absolute sizes of elements in relation to the viewport, while keeping the proportion of all elements in relation to each other – cVergel Oct 23 '17 at 04:07
-
@cVergel See update. No good way to do this. I would either have separate stylesheet or loop each element and check it. – Twisty Oct 23 '17 at 04:24
-
So I got pretty close now, although I think the script might be looping through too many elements and might affect loading times (will link to working example when ready). However, debugging it I found out that the values returned by .css() are converted from vw to px, so your filter doesn't find any elements to work on unless I change the string from "vw" to "px"!... I think that I will have to go the manual css route because of the deadline, but I would like to crack this, it would be very useful in the future, for vw-based layouts. – cVergel Oct 23 '17 at 06:44
-
@cVergel You're right, I reread the doc: *Get the computed style properties for the first element in the set of matched elements.* So this is a Sting value being returned, yet of the computed value, not the natural value. There might be a quick fix. – Twisty Oct 23 '17 at 14:54
-
@cVergel I will have to withdraw my answer. *Using jQuery you can only get the calculated value.* See: https://stackoverflow.com/questions/19404747/get-actual-value-specified-in-css-using-jquery Sorry. If you mark the elements with a class, you can calculate the specified `vw` value, yet there is no way to gather this detail from the DOM or Style. – Twisty Oct 23 '17 at 15:40