CSS - @document

The @document CSS at-rule restricts the style rules contained within it based on the URL of the document. It is designed primarily for user style sheets. A @document rule can specify one or more matching functions. If any of the functions apply to a URL, the rule will take effect on that URL.

Example

 

CSS
@document url(http://www.w3.org/),
               url-prefix(http://www.w3.org/Style/),
               domain(mozilla.org),
               regexp("https:.*")
{
  /* CSS rules here apply to:
     - The page "http://www.w3.org/".
     - Any page whose URL begins with "http://www.w3.org/Style/"
     - Any page whose URL's host is "mozilla.org" or ends with
       ".mozilla.org"
     - Any page whose URL starts with "https:" */

  /* make the above-mentioned pages really ugly */
  body {
    color: purple;
    background: yellow;
  }
}

Syntax  

The values provided to the url(), url-prefix(), and domain() functions can optionally be enclosed by single or double quotes. The values provided to the regexp() function must be enclosed in quotes.

Escaped values provided to the regexp() function must additionally be escaped from the CSS. For example, a . (period) matches any character in regular expressions. To match a literal period, you would first need to escape it using regular expression rules (to \.), then escape that string using CSS rules (to \\.).

Formal syntax

CSS
@document <a href="css/value_definition_syntax#brackets" title="Brackets">[</a> <a href="css/url" title=""><url></a> <a href="css/value_definition_syntax#single_bar" title="Single bar">|</a> url-prefix(<a href="css/string" title=""><string></a>) <a href="css/value_definition_syntax#single_bar" title="Single bar">|</a> domain(<a href="css/string" title=""><string></a>) <a href="css/value_definition_syntax#single_bar" title="Single bar">|</a> regexp(<a href="css/string" title=""><string></a>) <a href="css/value_definition_syntax#brackets" title="Brackets">]</a><a href="css/value_definition_syntax#hash_mark_(.23)" title="Hash mark">#</a> {
  <group-rule-body>
}

Description  

The @document CSS at-rule restricts the style rules contained within it based on the URL of the document. It is designed primarily for user style sheets. A @document rule can specify one or more matching functions. If any of the functions apply to a URL, the rule will take effect on that URL.

The main use case is for user-defined stylesheets, though this at-rule can be used on author-defined stylesheets too.

The functions available are:

  • url(), which matches an exact URL
  • url-prefix(), which matches if the document URL starts with the value provided
  • domain(), which matches if the document URL is on the domain provided (or a subdomain of it)
  • regexp(), which matches if the document URL is matched by the regular expression provided. The expression must match the entire URL.

Browser Compatibility  

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support Not supported 1.5 (1.8)-moz Not supported Not supported Not supported
regexp() Not supported 6.0 (6.0)-moz Not supported Not supported Not supported
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? ? ? ? ?
regexp() Not supported Not supported ? Not supported Not supported Not supported

See Also  

Specifications  

Initially in CSS Conditional Rules Module Level 3, @document has been postponed to the level 4.

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/css/@document

At-rule CSS Reference