@namespace CSS At-Rule

Description

The CSS @namespace at-rule is used to declare XML namespaces in a CSS stylesheet. It can be used to define a default namespace or a prefixed namespace.

A namespace is a way to group related elements and attributes together. This can be useful for avoiding collisions between elements and attributes with the same name from different namespaces. For example, the <a> element is used in both HTML and SVG. To avoid collisions, you can use the @namespace at-rule to declare a default namespace for your CSS stylesheet. This will ensure that your CSS selectors only match elements in the specified namespace.

Syntax

namespace <namespace-prefix>? [ <string> | <url> ];
where 
<namespace-prefix> = <ident>

Values

  • none

Example

@namespace url(http://www.w3.org/1999/xhtml);
@namespace svg url(http://www.w3.org/2000/svg);

/* This matches all XHTML elements, as XHTML is the default unprefixed namespace */
a {}

/* This matches all SVG
elements */
svg|a {}

/* This matches both XHTML and SVG
elements */
*|a {}

Browser Support

The following table will show you the current browser support for the at-rule @namespace.

Desktop
Edge Chrome Firefox Opera Safari
121181
Tablets / Mobile
Chrome Firefox Opera Safari Samsung Webview
184141137

Last updated by CSSPortal on: 30th September 2023