If I wanted to use inline styles in React return{}, do numeric values need quotes?
For example, in width: 50%;
Does 50% need to be enclosed in quotes?
If so, why?
If I wanted to use inline styles in React return{}, do numeric values need quotes?
For example, in width: 50%;
Does 50% need to be enclosed in quotes?
If so, why?
2
Answers
In this case, the value ‘50%’ is a string and should be enclosed in quotes because it is a CSS value. The quotes are necessary to indicate that it is a string value representing a percentage.
In this case, 50 is interpreted as a numeric value and not a string.
This depends on the type of numeric value you use.
There are four types of numeric values in CSS:
Integer
: whole numberNumber
: can be a decimal or a fractionDimension
: anumber
with a unit attached to it e.g.20px
Percentage
: anumber
with a%
that represents a fractionIn Javascript, only the
integer
andnumber
types are considered numeric and as such can be written without quotes in a JSX inline style object, otherwise you need to wrap yourdimension
orpercentage
in quotes because they’re parsed as strings.So if this is the CSS you want:
Your JSX would look like this: