You are here: Reference > JavaScript > client-side > HTML DOM > objects > nodes and tags > style

style object

Browser support:
Specifies style sheet rules for a document.
The style element allows authors to include CSS code into the HTML document. The style tag should be placed in the head element.
To include an external CSS file into the HTML document, use the link element or the @import at-rule. If you want to import a CSS file into another CSS file, use the @import at-rule. See Example 2.
If you need to modify the contents of a style sheet dynamically, use the styleSheet object and the cssRules and rules collections. See Example 3-5.

Syntax:

Methods that return the object:
document.createElement ("style")
The base interface, through which you can add new functionalities to the style object, is the HTMLStyleElement interface.
If you want to see the HTML objects by categories, please visit this page.
HTML page for this element: style

Possible members:

Properties
Methods
Events
attributes
Represents a collection of attribute nodes that belong to an element.
baseURI
10
Returns the base URL for the object.
behaviorUrns
Represents a collection of the Uniform Resource Names for all behaviors attached to an element.
canHaveChildren
Retrieves a Boolean value that indicates whether the element can contain child elements.
canHaveHTML
Retrieves a Boolean value that indicates whether the element can contain HTML formatted text.
dir
Sets or retrieves the text direction as related to the lang property.
disabled
Sets or retrieves the state of an object for user interaction.
id
Sets or retrieves a unique identifier for the object.
isContentEditable
Returns a Boolean value that indicates whether the contents of the object are editable by the user.
isDisabled
Returns a Boolean value that indicates whether the object is disabled.
isMultiLine
Returns a Boolean value that indicates whether the contents of an element can be multiline or not.
isTextEdit
Returns a Boolean value that indicates whether the createTextRange method can be used for the element.
lang
Specifies or returns the language of the element.
localName
9
Returns the local part of the qualified name of the current node.
media
Specifies or returns the media types for style definition.
name
Sets or retrieves the name of an element.
namespaceURI
93.6
Sets or returns the namespace URI of the current node.
nextElementSibling
93.5
Returns a reference to the next child element of the current element's parent.
nextSibling
Returns a reference to the next child of the current element's parent.
nodeName
Returns the name of the current node.
nodeType
Returns an integer that indicates the type of the node.
nodeValue
Sets or returns the value of the current node.
outerHTML
Sets or retrieves the outer HTML content (the source code including the opening and closing tags) of an element.
outerText
Sets or returns the text content of an element including the text content of its descendants.
ownerDocument
Returns the document object that contains the current node.
parentElement
Returns the parent element of the object in the DOM hierarchy.
parentNode
Returns the parent element of the current node in the DOM hierarchy.
parentTextEdit
Returns the closest ancestor element of the current element in the DOM hierarchy that can be used to create a TextRange object.
previousElementSibling
93.5
Returns a reference to the previous child element of the current element's parent.
previousSibling
Returns a reference to the previous node of the current element's parent.
readyState
Returns a string value that represents the state of the object.
scopeName
Retrieves the local name of the namespace declared for the current element.
sheet
9
Represents a CSS style sheet that belongs to a style element or an imported style file.
sourceIndex
Returns the position of the current object in the all collection of the document.
styleSheet
Represents a CSS style sheet that belongs to a style element or an imported style file.
tagName
Returns the tag name of the current element.
tagUrn
Sets or retrieves the Uniform Resource Name (URN) of the namespace declared for the current element.
title
Specifies or returns a tooltip for an element.
type
Specifies or returns the type of style sheet language.
uniqueID
Returns the unique identifier generated by the browser for the object.

Example HTML code 1:

This example illustrates the use of the style element:
<head>
    <style>
        #sampleID {color:red;}
        .sampleClass {color: green;}
        b {color: blue;}
    </style>
</head>
<body>
    <div id="sampleID">The color of this text is red.</div>
    <div class="sampleClass">The color of this text is green.</div>
    <b>The color of this text is blue.</b>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example shows how to import an external style sheet into the document. For detailed description, please see the @import at-rule.
Code
red.css
<head>
    <style>
        @import "red.css";
    </style> 
</head>
<body>
    <div class="red">red division</div>
    <div>non-red division</div>
    <span class="red">red span</span>
</body>
Did you find this example helpful? yes no

Example HTML code 3:

This example shows how to change a style rule in an embedded style sheet:
<head>
    <style>
        .red {
            color: red;
        }
    </style> 
    <script type="text/javascript">
        function ChangeRedColor () {
                // the first style sheet in the document
            var sheet = document.styleSheets[0];
                // the first rule in the style sheet
            var rules = sheet.cssRules ? sheet.cssRules : sheet.rules;
            var firstRule = rules[0];

                // change the color
            firstRule.style.color = "#00ff00";
        }
    </script> 
</head>
<body>
    <button onclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button>
    <br /><br />
    <div class="red">red division</div>
    <div>non-red division</div>
    <span class="red">red span</span>
</body>
Did you find this example helpful? yes no

Example HTML code 4:

This example is the same as the previous one, but it uses the sheet and styleSheet properties of the style element to get the style sheet:
<head>
    <style id="myStyle">
        .red {
            color: red;
        }
    </style> 
    <script type="text/javascript">
        function ChangeRedColor () {
            var styleTag = document.getElementById ("myStyle");

                // the style sheet in the style tag
            var sheet = styleTag.sheet ? styleTag.sheet : styleTag.styleSheet;
            
                // the first rule in the style sheet
            var rules = sheet.cssRules ? sheet.cssRules : sheet.rules;
            var firstRule = rules[0];

                // change the color
            firstRule.style.color = "#00ff00";
        }
    </script> 
</head>
<body>
    <button onclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button>
    <br /><br />
    <div class="red">red division</div>
    <div>non-red division</div>
    <span class="red">red span</span>
</body>
Did you find this example helpful? yes no

Example HTML code 5:

This example shows how to change a style rule in an external style sheet:
Code
red.css
<head>
    <style id="myStyle">
        @import "red.css";
    </style> 
    <script type="text/javascript">
        function ChangeRedColor () {
            var styleTag = document.getElementById ("myStyle");

                // the style sheet in the style tag
            var sheet = styleTag.sheet ? styleTag.sheet : styleTag.styleSheet;

            if (sheet.cssRules) { // all browsers, except IE before version 9
                var rule = sheet.cssRules[0];
                    // we know that the rule is a CSSImportRule
                var importedSheet = rule.styleSheet;
            }
            else {  // Internet Explorer before version 9
                var importedSheet = sheet.imports[0];
            }
    
                // the first rule in the style sheet
            var rules = importedSheet.cssRules ? importedSheet.cssRules : importedSheet.rules;
            var firstRule = rules[0];

                // change the color
            firstRule.style.color = "#00ff00";
        }
    </script> 
</head>
<body>
    <button onclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button>
    <br /><br />
    <div class="red">red division</div>
    <div>non-red division</div>
    <span class="red">red span</span>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content