GM addStyle: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m Text replace - "</pre>}}" to "</pre>"
simplify
Line 3: Line 3:
== Description ==
== Description ==


This [[API_reference|API]] method adds a string of CSS to the document.
This method adds a string of CSS to the document.
It creates a new <code><style></code> element, adds the given CSS to it, and inserts it into the <code><head></code>.


It creates a new <code><style></code> element, adds the given CSS to it, and inserts it into the <code><head></code>.
Compatibility: [[Version_history#0.6.1|Greasemonkey 0.6.1+]]


== Syntax ==
== Syntax ==


'''GM_addStyle(''' ''css'' ''')'''
{{Function|GM_addStyle|css}}


:Value: Function
=== Arguments ===
:Returns: undefined
:Compatibility: [[Version_history#0.6.1|Greasemonkey 0.6.1+]]


:{| cellpadding="5" style="border-style:solid; background:#FFFFE0;"
; <code>css</code>
|+ Parameters
: <code>String</code> A string of CSS.
!style="background:#CC9900;"|'''Properties'''
|-
| <code><span style="background:#FFFFE0;">[[#css |css]]</span></code>
|}
:* All properties are optional except [[#css|css]].


=== Properties ===
== Examples ==
----
==== <code>css</code> ====
:Value: String
:Usage: <code>'''css''' = "body { color:red }";</code>


== Examples ==
<pre class='sample'>
<pre class='sample'>
GM_addStyle("body { color: white; background-color: black } img { border: 0 }");
GM_addStyle("body { color: white; background-color: black; } img { border: 0; }");
</pre>
</pre>


Spanned over multiple lines using E4X:
== Notes ==
<pre class='sample-good'>
GM_addStyle(<><![CDATA[
  body { color: white; background-color: black }
  img { border: 0 }
  .footer { width: 875px; }
]]></>.toString());
</pre>


Spanned over multiple lines using line continuation character.
* Add <code>!important</code> at the end of a CSS declaration to override an existing value (i.e. values the page specifies).
<pre class='sample-good'>
* Using [[Multi Line Strings]] may be helpful.
GM_addStyle("body { color: white; background-color: black }\
  img { border: 0 }\
  .footer { width: 875px; }");
</pre>
 
== Notes ==
Add <code>!important</code> at the end of the code to override an existing value.


[[Category:API_Reference|A]]
[[Category:API_Reference|A]]

Revision as of 22:04, 8 February 2010


Description

This method adds a string of CSS to the document. It creates a new <style> element, adds the given CSS to it, and inserts it into the <head>.

Compatibility: Greasemonkey 0.6.1+

Syntax

function GM_addStyle( css )

Arguments

css
String A string of CSS.

Examples

GM_addStyle("body { color: white; background-color: black; } img { border: 0; }");

Notes

  • Add !important at the end of a CSS declaration to override an existing value (i.e. values the page specifies).
  • Using Multi Line Strings may be helpful.