@namespace

Term side of the Card.
Topic: Directives
Unit title: @namespace
Categories: Directive
Explanation side of the Card

Explanation: The directive that declares a namespace for CSS selectors, useful in XML-based documents to differentiate between elements from different XML namespaces. It helps avoid conflicts by specifying which namespace the selectors apply to, ensuring proper styling in complex documents.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 1.
Availability: 

Example

Wrongcss
a { color: blue; }
@namespace svg url(http://www.w3.org/2000/svg);
svg|a { fill: blue; }
Rightcss
@namespace svg url(http://www.w3.org/2000/svg);

a { color: blue; }
svg|a { fill: blue; }

@namespace must come before style rules, otherwise it is ignored and svg|a never matches.

Common mistakes

  • Adding @namespace to plain HTML pages where it has no practical effect.
  • Placing @namespace after other style rules, so the browser ignores it.

Key points

  • @namespace rules come after @charset and @import and before all other rules.
  • Prefixed selectors use the prefix|element syntax, like svg|a, with a declared prefix.

Interview questions

Do I need @namespace for inline SVG in HTML?

Usually no. Type selectors like svg and circle already match inline SVG elements in HTML documents.

What does the | character mean in a CSS selector?

It separates a namespace prefix from an element or attribute name, as in svg|a or *|a for any namespace.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy