0

Possible Duplicate:
Are the decimal places in a CSS width respected?

Normally in CSS when we set a size we use while values in points or pixels. However, sometimes it is not possible to know the exact widths, so I jQuery to calculate it with .width() function and then perform some math, like divide by the number of elements etc.

Sometimes the results has decimal points. Do browsers support such values or is it better to round-up to whole values, for example using something like Math.floor(), before using the values?

Community
  • 1
  • 1
santa
  • 12,234
  • 49
  • 155
  • 255
  • See this answer: http://stackoverflow.com/questions/4308989/are-the-decimal-places-in-a-css-width-respected – sellmeadog Sep 24 '12 at 18:28

1 Answers1

3

Browsers do support decimal points, for example you could have 20.5%, or .5% etc

However - this tends to be limited to percentages, as you can't really have a half of a pixel.

See, as this is basically a duplicate.

Are the decimal places in a CSS width respected?

Community
  • 1
  • 1
David
  • 2,053
  • 2
  • 16
  • 26
  • Very cool, thanks. Unfortunately in my case I already use some jQuery to calculate the remaining width left by a number of elements, so I'll just round-them up and then get a dif with subtraction. – santa Sep 24 '12 at 18:30
  • Rounding down might be wiser, otherwise 5 elements at 20.5 rounded up, is 102.5 (Which could be greater than a container of 100 for example) – David Sep 24 '12 at 18:31
  • Browsers **do** support decimals but they cannot render a half of pixel. They'll round somehow and try not to break the layout. http://ejohn.org/blog/sub-pixel-problems-in-css/ – Michał Miszczyszyn Sep 24 '12 at 18:32
  • Most browsers will round down (which will cause layouts in 99% of cases to not break), IE is basically the exception, as we all know IE likes to break layouts! – David Sep 24 '12 at 18:34
  • Sorry, I meant round-down, and then subtract the sum of rounded-down values from the total and the add to one of the elements, perhaps :last-child to keep it at 100% ;) – santa Sep 24 '12 at 18:37