style object
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.
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:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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?
|
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.
|
|||||
<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?
|
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?
|
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?
|
Example HTML code 5:
This example shows how to change a style rule in an external style sheet:
|
|||||
<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?
|
Related pages:
External links:
User Contributed Comments