Using the DOM (Document Object Model) to control CSS

The two major browser options -- Microsoft's Internet Explorer and the Mozilla browsers (Firefox, etc.) have slightly different syntax to access and change CSS style values from scripts. In addition the specification of the CSS properties on the the HTML page and inside scripts is also different.

  1. Spelling CSS Properties

    If you are specifying CSS style in a style sheet or locally on an HTML page, the property names have hyphens to separate the property name parts. For example:
            z-index; border-color; font-family; font-size; background-color; ...
    However, when these same properties are referenced inside the script code, the property names are spelled: (1) WITHOUT the hyphens; (2) the first letter of the property is lower case; and (3) the first letter of subsequent words of the property are capitalized. For example:
            zIndex; borderColor; fontFamily; fontSize; backgroundColor;...
    This is because in the script code the hyphen would be interpreted as a "subtraction" operation.

  2. DOM CSS References
  3. Cross Browser References

    In order to allow your script to work across various browsers you can employ the following test:

    if (document.all)
          document.all.some_id.style.some_css_property = some_value
    else
          document.getElementById(some_id).style.some_css_property = some_value


      Click a topic in the list to the left to see examples