1

when i use element.style.left = 1.6px; this only sets property to 1. Not 1.6px. Which is not right. Is there a way to do this correctly.

Only i can think of is add new stylesheet to the whole body and delete the old one...seems like too much though.

8
  • 3
    write it as a string, '1.6px'; Commented May 11, 2014 at 7:46
  • There is no such thing as .6 pixels. Pixels are always an integer. Commented May 11, 2014 at 8:14
  • 1
    @Scott While your logic may seem right to you, that is not entirely true, check this fiddle and see computed styles for div.wrap - jsfiddle.net/easwee/bssnY - you will see that it computes to 301.5px and renders as 302px on screen (because of rounding). Decimals DO sum up AND rounding occurs at render. Commented May 11, 2014 at 9:51
  • the problem i was trying to solve doesn't exist...just writing what i have written would give error..i meant to put quotes but what i thought was that value gets converted from 1.6px to 1. Not because style.left is like that but because parseInt was messing it up. Commented May 11, 2014 at 17:11
  • 1
    @easwee -- "...and renders as 302px" because there's no such thing as a partial pixel :) I did not state you couldn't specify partial pixels. You can. They just don't actually exist. Commented May 11, 2014 at 18:37

2 Answers 2

2

Try using

element.style.left = "1.6px";

Because css properties are strings in Javascript.

Sign up to request clarification or add additional context in comments.

1 Comment

Consider adding a bit of explanation to your answer. For example why are the quote marks required...?
0

This is because we can't operate on anything other than multiples of 1 pixel in CSS. Think of the page as a grid of pixels - we can't write between the lines of the grid! Why is it that you need 1.6px specifically?

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.