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
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 | Edge |
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 |
|||||||||||||||||||
getComputed
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 |
|||||||||||||||||||
style
The inline style of the element
Test page |
|||||||||||||||||||
x.style Access the inline styles of element You can also set it:
|
|||||||||||||||||||
x.style = 'font-size: 150%' Change the inline styles to only |
|||||||||||||||||||
document.styleSheets[1].cssRules[0].style Access the styles in the first rule of the second style sheet. These, too, can be set: (IE uses |
|||||||||||||||||||
Method or property | Internet Explorer | Edge |
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 |
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 | Edge |
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 |
|||||||||||||||||||
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 Unfortunately the second argument is required. It should default to |
|||||||||||||||||||
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 | Edge |
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 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 | Edge |
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 |
|||||||||||||||||||
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 (Obviously, IE needs |
|||||||||||||||||||
document.styleSheets[1].cssRules[1].style.cssText Returns all declarations of the rule (for instance (Obviously, IE needs |
|||||||||||||||||||
disabled
Whether the stylesheet is disabled
Test page |
|||||||||||||||||||
document.styleSheets[1].disabled = true Disable the second style sheet of the document. (Setting it to |
|||||||||||||||||||
href
The href of a stylesheet
Test page |
|||||||||||||||||||
document.styleSheet[0].href Get the
|
|||||||||||||||||||
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
|
|||||||||||||||||||
Method or property | Internet Explorer | Edge |
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 stuff. Generally not important.
Method or property | Internet Explorer | Edge |
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 | ||||
create
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. |
|||||||||||||||||||
getProperty
Get the property priority (!important or not)
Test page |
|||||||||||||||||||
document.styleSheets[1].cssRules[1].style.getPropertyPriority('color') Returns (Obviously, IE needs |
|||||||||||||||||||
getProperty
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 |
|||||||||||||||||||
ownerNode
The owner of a style sheet
Test page |
|||||||||||||||||||
document.styleSheets[1].ownerNode Access the owner node of the style sheet, usually a |
|||||||||||||||||||
parent
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 |
|||||||||||||||||||
remove
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".
|
|||||||||||||||||||
set
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 As usual, the |
|||||||||||||||||||
Method or property | Internet Explorer | Edge |
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 |
Desktop browser test array 2.1; January 2015