Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get CSS rule's percentage value in jQuery

Tags:

jquery

css

Let's say the rule is as follows:

.largeField {     width: 65%; } 

Is there a way to get '65%' back somehow, and not the pixel value?

Thanks.

EDIT: Unfortunately using DOM methods is unreliable in my case, as I have a stylesheet which imports other stylesheets, and as a result the cssRules parameter ends up with either null or undefined value.

This approach, however, would work in most straightforward cases (one stylesheet, multiple separate stylesheet declarations inside the head tag of the document).

like image 990
montrealist Avatar asked Apr 13 '09 15:04

montrealist


People also ask

How do you show percentages in CSS?

The <percentage> data type consists of a <number> followed by the percentage sign ( % ). Optionally, it may be preceded by a single + or - sign, although negative values are not valid for all properties. As with all CSS dimensions, there is no space between the symbol and the number.

Why do we use percentage in HTML?

It represents by <percentage > takes a number as a parameter. It is used to define the relative size to the parent size. With the help of this, you can adjust all the HTML-CSS elements. In CSS, many properties that take a percentage as parameters such as padding, width, height, margin, and font-size.


1 Answers

Most easy way

$('.largeField')[0].style.width  // >>> "65%" 
like image 177
redexp Avatar answered Oct 24 '22 17:10

redexp