Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8061453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:20:42+00:00 2026-06-05T10:20:42+00:00

The following JavaScript code works perfectly in WebKit-based browsers, but it doesn’t work in

  • 0

The following JavaScript code works perfectly in WebKit-based browsers, but it doesn’t work in Firefox:

canvas.style['left'] = "50%";
canvas.style['margin-left'] = "-" + (width / 2) + "px";

Inspecting the element in Firefox, I can see that the left property was successfully set by the above code, but the element has no margin-left property for some reason.

It seems I must be doing something wrong, which isn’t surprising, because I haven’t been able to figure out what the correct way to access (read or set) CSS style properties from JavaScript is. I’ve just found some examples using this notation and tried to follow them, with these mixed results.

What is the correct / standards-compliant way to access an element’s CSS style properties from JavaScript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:20:43+00:00Added an answer on June 5, 2026 at 10:20 am

    After much hair-pulling, I unearthed the following basic facts that are sure to be no-brainers to most JavaScript folks, but I imagine my confusion is not uncommon among others very new to JavaScript.

    • All objects in JavaScript are associative arrays.

    Object properties can be accessed in either of two ways:

    var o = new Object();
    o.prop = "A string";
    o['prop']; // => "A string"
    

    While it is possible to use an object as a “normal” associate array and use keys with arbitrary strings:

    o.['another-prop'] = 4 // Perfectly valid
    

    in doing so, you would create a property that could not be accessed via the dot notation:

    o.another-prop // Parsed as `o.another` minus `prop`
    

    This is presumably the reason for:

    • DOM style properites use camelCase when accessed from JavaScript.

    I was using the hyphenated property names used in CSS. These worked in all the WebKit-based browsers I tested, but it seems that is only because WebKit is forgiving.


    So as far as I can tell, the correct way to access CSS style properties from JavaScript is to convert the property name to camel case, and then use either dot notation or bracketed associative array notation. The line that didn’t work in Firefox becomes

    canvas.style['marginLeft'] = "-" + (width / 2) + "px";
    

    or

    canvas.style.marginLeft = "-" + (width / 2) + "px";
    

    That works for me in all browsers I’ve tested, but if something I’ve said isn’t correct or consistent with the standards, I’d love to hear it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following javascript code works perfectly in Opera(v10.5x), but fails miserably in Firefox(v3.6) and
The following code works perfectly in Firefox, but not in IE or Chrome... Any
The following code works perfectly in Chrome and Safari, but bonks in Firefox. First
the following code works perfectly in FireFox 7, but when pulling up the same
I use the following code which works perfectly in all browsers except Chrome and
I have the following Javascript code that works perfectly: $(document).ready(function() { $(#Select1).setDefault(); $(#Select2).setDefault(); $(#Select3).setDefault();
I have the following JavaScript code. var myCellCollection = document.getElementById('myTbl').cells; This works well in
I have some JavaScript code that works in IE containing the following: myElement.innerText =
I'm using the following code for star rating. The code works fine in firefox
The following script below is not running in IE7 but works perfectly fine in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.