# @namespace

> The `@namespace` CSS directive is used to define XML namespaces for use in XML-based languages within CSS.

## What does @namespace do in CSS?

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.

- Topic: Directives
- Type: directive
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1+, Firefox Android 4+, Safari 1+, Safari iOS 1+

## Example

Wrong:

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

Right:

```css
@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.

## Related cards

- [Directive](https://wonderium.dev/css/directive.md)
- [Type Selector](https://wonderium.dev/css/type-selector.md)
- [`<svg>`](https://wonderium.dev/html/svg.md)
- [@import](https://wonderium.dev/css/import.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/namespace
- Last updated: 2026-11-25
