DOM CSS

Back to the index.

DOM methods and properties that are for all implementations, and not just for the JavaScript one. In theory almost all of them should work in any programming language that supports the DOM.

This is the desktop table. See also the mobile table.

Last major update on 3 September 2013.

style element https://developer.mozilla.org/en-US/docs/Web/API/HTMLStyleElement

mediaList: http://www.w3.org/TR/cssom/#medialist mediaText, item(), length, appendMedium, deleteMedium mediaQueryList https://msdn.microsoft.com/en-us/library/ie/hh772454%28v=vs.85%29.aspx

style sheets: media, location, parent, owner, disabled https://msdn.microsoft.com/en-us/library/ie/ms535871(v=vs.85).aspx

Element styles

Basic style manipulation starts with accessing styles of individual HTML elements. The important style property gives access to the inline styles of the element. Finding out which non-inline styles apply to an element is more difficult. See the Get styles page for a practical example.

Method or property Internet Explorer EdgeHTML Firefox Safari Chrome Opera Yandex
7 and lower 8 9 10 11 35 Win 35 Mac 35 Linux 7 40 Win 40 Mac 37 Linux 26 Win 26 Mac 14.12 Win 14.12 Mac
currentStyle
The current style of the element, however this style is set

Test page Microsoft
x.currentStyle

Returns the actual style of element x, even if this is not an inline style. It thus reports the result of all style declarations that work on element x.

getComputedStyle()
The current style of the element, however this style is set

Test page W3C
window.getComputedStyle(x,null).color

Read out the current color style of element x. All units are converted to pixels first.

style
The inline style of the element

Test page
x.style

Access the inline styles of element x. Styles defined in embedded, linked or imported style sheets are not reported.

You can also set it: x.style.color = '#0000cc'. This sets the inline style, which of course overrules all other styles.

  • Konqueror has trouble with reading out the border style that I use in my test. However, in general style works correctly in both browsers.
  • IE5.5 and 6 have problems with setting the new border rule, but I suspect that's caused by my template and not by the actual test. In general they support style changes well.
x.style = 'font-size: 150%'

Change the inline styles to only font-size: 150%.

document.styleSheets[1].cssRules[0].style

Access the styles in the first rule of the second style sheet. These, too, can be set:
document.styleSheets[1].cssRules[0].style.color = '#0000cc'

(IE uses rules instead of cssRules, obviously.)

Method or property Internet Explorer EdgeHTML Firefox Safari Chrome Opera Yandex
7 and lower 8 9 10 11 35 Win 35 Mac 35 Linux 7 40 Win 40 Mac 37 Linux 26 Win 26 Mac 14.12 Win 14.12 Mac

Changing style sheets

You can change entire style sheets, which means that the styles you change will apply to the whole HTML page, not just one element. There are some cute old-fashioned browser compatibility problems here.

Method or property Internet Explorer EdgeHTML Firefox Safari Chrome Opera Yandex
7 and lower 8 9 10 11 35 Win 35 Mac 35 Linux 7 40 Win 40 Mac 37 Linux 26 Win 26 Mac 14.12 Win 14.12 Mac
addRule()
Add a rule into a stylesheet the Microsoft way

Test page
document.styleSheets[1].addRule('pre', 'font: 14px verdana')

Add the rule pre {font: 14px verdana} to the second style sheet in the document.

deleteRule()
Delete a rule from a stylesheet the W3C way

Test page
document.styleSheets[1].deleteRule(1)

Delete the second rule of the second style sheet in the document.

insertRule()
Insert a rule into a stylesheet the W3C way

Test page
var x = document.styleSheets[1];
x.insertRule('pre {font: 14px verdana}',x.cssRules.length)

Insert the rule pre {font: 14px verdana} into the second style sheet in the document at the last position (length of the cssRules array).

Unfortunately the second argument is required. It should default to cssRules.length.

removeRule()
Remove a rule from a stylesheet the Microsoft way

Test page
document.styleSheets[1].removeRule(1)

Remove the second rule from the second style sheet in the document.

Method or property Internet Explorer EdgeHTML Firefox Safari Chrome Opera Yandex
7 and lower 8 9 10 11 35 Win 35 Mac 35 Linux 7 40 Win 40 Mac 37 Linux 26 Win 26 Mac 14.12 Win 14.12 Mac

Style sheet properties

Style sheets and their rules have several properties. They aren't very interesting, and browser incompatibilities make sure that they won't be used much.

Method or property Internet Explorer EdgeHTML Firefox Safari Chrome Opera Yandex
7 and lower 8 9 10 11 35 Win 35 Mac 35 Linux 7 40 Win 40 Mac 37 Linux 26 Win 26 Mac 14.12 Win 14.12 Mac
cssText
The CSS of a rule as a string.

Test page
x.style.cssText

Returns the entire inline style of element x.

document.styleSheets[1].cssText

Returns the entire content of the style sheet.

document.styleSheets[1].cssRules[1].cssText

Returns the entire text of the rule (for instance pre {font: 14px verdana;}).

(Obviously, IE needs rules instead of cssRules.)

document.styleSheets[1].cssRules[1].style.cssText

Returns all declarations of the rule (for instance font: 14px verdana;).

(Obviously, IE needs rules instead of cssRules.)

disabled
Whether the stylesheet is disabled

Test page
document.styleSheets[1].disabled = true

Disable the second style sheet of the document. (Setting it to false enables the style sheet again).

href
The href of a stylesheet

Test page
document.styleSheet[0].href

Get the href of the style sheet. This is a relative path in IE; a complete URL in all other browsers.

  • IE up to 8 allows you to change the href.
selectorText
The selector of a rule as a string

Test page
document.styleSheets[1].cssRules[1].selectorText

Get the selector text of the second rule in the second style sheet in the document.

Note that some browsers return UPPER CASE selector texts even when the style sheet contains lower case selectors. Use selectorText.toLowerCase() for all your comparisons.

  • Opera allows you to set the selectorText.
  • IE splits all comma selectors into several rules (p#test, li becomes two rules, one for p#test, one for li). This is not counted as a bug in this test; IE shows the correct selectorText according to its own rules.
  • IE9, however, persists in this behaviour even though it support for cssRules is supposed to be standards compliant and thus return p#test, ul. What it returns, however, is p#test. I assume this is a bug in the selectorText implementation, and not in the actual style sheet (i.e. the rule does apply to both p#test and ul.)
Method or property Internet Explorer EdgeHTML Firefox Safari Chrome Opera Yandex
7 and lower 8 9 10 11 35 Win 35 Mac 35 Linux 7 40 Win 40 Mac 37 Linux 26 Win 26 Mac 14.12 Win 14.12 Mac

Miscellaneous

Miscellaneous stuff. Generally not important.

Method or property Internet Explorer EdgeHTML Firefox Safari Chrome Opera Yandex
7 and lower 8 9 10 11 35 Win 35 Mac 35 Linux 7 40 Win 40 Mac 37 Linux 26 Win 26 Mac 14.12 Win 14.12 Mac
createStyleSheet()
Create a style sheet

Test page
To be tested
document.createStyleSheet('extrastyle.css')

Create an extra style (sheet) and append it to the styleSheets[] array.

getPropertyPriority()
Get the property priority (!important or not)

Test page
document.styleSheets[1].cssRules[1].style.getPropertyPriority('color')

Returns important when the style is marked !important. Returns an empty string otherwise.

(Obviously, IE needs rules instead of cssRules.)

getPropertyValue()
Get the value of a style property

Test page
document.getElementById('test').style.getPropertyValue('color')
document.styleSheets[1].cssRules[1].style.getPropertyValue('color')

Get the color declaration of the style object. This works on all style objects. Of course, reading out style.color is much easier.

ownerNode
The owner of a style sheet

Test page
document.styleSheets[1].ownerNode

Access the owner node of the style sheet, usually a <link> or a <style> tag.

parentStyleSheet
The stylesheet a declaration is part of

Test page
document.styleSheets[1].cssRules[1].parentStyleSheet

Access the parent style sheet of the rule, which is once again document.styleSheets[1].

removeProperty()
Remove a style declaration entirely

Test page
document.styleSheets[1].cssRules[1].style.removeProperty('color')
document.getElementById('test').style.removeProperty('color')

Remove the color declaration from the second rule of the second style sheet on the page or the element with ID="test".

  • Opera refuses to remove the last remaining property of a rule.
setProperty()
Set a style property

Test page
x.style.setProperty('color','#00cc00',null)
document.styleSheets[1].cssRules[1].style.setProperty('color','#00cc00',null)

Set the color of x or the second rule in the second style sheet to green.

As usual, the null is required. (Default values, anyone?) You can also use important instead.

Method or property Internet Explorer EdgeHTML Firefox Safari Chrome Opera Yandex
7 and lower 8 9 10 11 35 Win 35 Mac 35 Linux 7 40 Win 40 Mac 37 Linux 26 Win 26 Mac 14.12 Win 14.12 Mac

Tested browsers

Desktop browser test array 2.1; January 2015

IE7 and lower
I removed them, so they’re not tested for newer methods and properties that they don’t support anyway. However, I copy all information from older versions of the Tables.
If IE8 supports a method or property I never tested before I have to guess if IE7 and lower also support it. In general I assume they support the Microsoft-invented properties, but for others I will occasionally have to add a "Don’t know" entry. If IE8 does not support something I never tested before, I assume IE7 and lower also don’t support it.
IE 8, 9, and 10
Trident
On separate Windows 7 virtualizations as downloaded from Modern IE.
IE11
Trident
On Windows 8.1 virtualization as downloaded from Modern IE.
On Surface
EdgeHTML
EdgeHTML
On Microsoft Remote Desktop, as indicated at Remote IE. This is not Spartan, but an IE that runs EdgeHTML, the new rendering engine.
Firefox
Gecko 35
35 on Win7, Mac and Linux
Safari
WebKit
7.0.5 on Mac
Chrome
Chromium 40/37
40 on Win7 and Mac
37 on Linux (can’t update)
Opera 26
Chromium 38
26.0 on Win7 and Mac
Yandex 14.12
Chromium 38
14.12 on Win7
14.12 on Mac

Operating systems

Mac
MacBook Pro 17'' with OS 10.9.4
This is my main test station. It also runs all virtual Windows systems.
Windows
All downloaded from modern.ie. I use VirtualBox, and downloded the Windows 7 systems for all browsers but IE11, which runs on Windows 8.1.
The non-IE Windows browsers all run on the IE9/Win7 virtualization.
Surface
Microsoft Surface with Windows RT 8.1
Linux
Ubuntu 12.04 on pretty old hardware. Not fair for performance comparisons.