The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Best practice to declare namespace for svg in HTML

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
  • #197768

    Hi all, been reading up on this but am utterly confused about it. Since I’m using several embedded svgs in my document, I thought to look for the most effective way to declare the namespace rather than specifying it for each separate element like this :

    <svg xmlns="">
    // some yummie svg stuff

    First thing I tried was to declaring it inside the &lt;html&gt; tag :

    <html xmlns="" 

    The moz doc states you can do this… but it doesn’t validate somehow.
    Next up – using @namespace. And my question – is this a correct
    approach? If I define the namespace with CSS for the svgs, can I then leave them out of each &lt;svg&gt; tag?

    @namespace svg url(;

    Thanks in advance for any advice.


    Editing one’s post when there’s a few code blocks in it is quite a drag these days, if I may so so. Someone please fix this! ;-)
    But that’s merely a cry of frustration…


    Editing one’s post when there’s a few code blocks in it is quite a drag these days.

    I seem to recall that there used to be a “preview” option..but perhaps that’s not possible under the current software.



    I’ll use a workaround in the meantime anyway – to copy and paste the text to an editor before I post it (when there’s a few code blocks). Just so the ticks don’t need to be all redone if I missed one character. Not too sure what’s happening by the way – sometimes the prefixes seem to parse fine but often they get posted as plain text.

    Thanks in any case. It’s a side note of course…


    I looked at the SVG spec –

    ..which seems pretty comprehensive.

    As to what the best method is…I guess whatever works.


    Yeah, it’s a whole field of study on it’s own. Besides that, it’s not very compatible with jQuery either. :-S
    Mighty interesting though (for general use, not so much the read-through).

    I’ll mess around with it a bit, see what works or not. I even read HTML5 should automatically know what namespace to use for svg. But I think it can’t hurt to declare it with @namespace anyway – a subject that has surprisingly few Google results by the way. I’m also interested in how it would be possible to declare the xlink namespace by default. This turns up basically nothing when I run a search on it.

    Using several svg elements, it would clean up the HTML nicely when the namespace ‘links’ can be taken out of the &lt;svg&gt; tags.

    So any expertise that would be shared is very welcome. :-)


    Besides that, it’s not very compatible with jQuery eithe

    Offhand, I would say it’s the other way round at the moment.

    Jquery hasn’t caught up with SVG…partly because the spec doesn’t allow adding classes etc to sub-elements / fragments.

    That said, there are some exciting SVG-JS libraries out there such as Snap, Raphael and GSAP.


    You are correct of course, jQuery is the incompatible part. But you can fake manipulating CSS by adding (or removing) classes as attributes. So it sorta works after all. Vanilla JS can fill in some other bits as well.



    Well, seems to work fine without declaring any namespaces whatsoever.
    So why not stick with that…

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Other’ is closed to new topics and replies.