papiro
papiro

Reputation: 2365

How many significant figures can a number have in CSS?

My question is, simply, how many (non-zero) decimal places can I include in a value I use in a CSS stylesheet before the browser rounds the number when interpreting it?

NOTE: I am aware that any decimal pixels are rounded (differently by different browsers) because the screens cannot display sub-pixel units. What I am asking is before that rounding takes place, what number of decimal places will be retained to begin performing the final browser rendering calculations/roundings.

Upvotes: 5

Views: 1035

Answers (1)

BoltClock
BoltClock

Reputation: 723618

Be it truncation or rounding, in an ideal world, neither of these things should happen. The spec simply says that a numeric value may either consist of

  • one or more digits, or
  • any number of digits, followed by a period for the decimal point, followed by one or more digits.

The spec even accounts for the fact that the leading zero before the decimal point in a value that's less than 1 is not significant and can thus be omitted, e.g. opacity: .5. But there is quite simply no theoretical upper limit.

But, due to implementation limitations, browsers will often "round" values for the purposes of rendering. This is not something you can control other than by changing the precision of your values, and even so, this behavior can vary between browsers, for obvious reasons, and is therefore something you cannot rely on.

Upvotes: 5

Related Questions