Yes the order is what specifies what object will be in front of the other. To manipulate the order you will need to move things about the DOM. There is a good example of this on the SVG wiki at https://www.w3.org/TR/SVG11/render.html#RenderingOrder
An alternative to moving elements in the tree is to use <use> elements where you change the xlink:href attribute so that it gives you the z ordering you want.
Here's an old thread on svg-developers mailinglist discussing this topic in context of wanting to animate some shapes.
In this example the <use> element is the last one, which makes it the frontmost element. We can choose any of the other elements to act as frontmost simply by changing the xlink:href attribute. In the above example we picked the circle with id="c1", which makes it appear as the topmost element.
On FireFox (7+) and Chrome (14+) you can pull svg_element to the top. This does not give you freedom of full z axis control, but it's better than nothing ;)
Just append that element again.
var svg = doc.createElemNS('svg');
var circle = doc.createElemNS('circle');
var line = doc.createElemNS('line');
svg.appendChild(circle); // appends it
svg.appendChild(line); // appends it over circle
svg.appendChild(circle); // redraws it over line now
I thought it was going to throw en error or something.
SVG uses a "painters model" of rendering. Paint is applied in successive operations to the output device such that each operation paints over some area of the output device. When the area overlaps a previously painted area the new paint partially or completely obscures the old.- link to this
...Press on the buttons to 'push' that element to the front. (vs repainting the whole set and pushing 1 element to front, but keeping the original order as in the accepted solution)
It would be very nice to have relative z indices...
stackOverflow wants me to put the code if it's a link from jsfiddle?...ook
var orderArray=[0,1,2];
var divArray = document.querySelectorAll('.shape');
var buttonArray = document.querySelectorAll('.button');
for(var i=0;i<buttonArray.length;i++){
buttonArray[i].onclick=function(){
var localI = i;
return function(){clickHandler(orderArray.indexOf(localI));};
}();
}
function clickHandler(divIndex) {
orderArray.push(orderArray.splice(divIndex, 1)[0]);
orderDivs();
}
function orderDivs(){
for(var i=0;i<orderArray.length;i++){
divArray[orderArray[i]].style.zIndex=i;
}
}
Adam Bradley's comment to Sam's answer was exactly on point. It uses jQuery rather than CSS only, but he said this:
$('#thing-i-want-on-top').appendTo('#my-svg');
However, for what I was creating, I needed my SVG Path to be above any other path on hover, but still be below my SVG Text. So here's what I came up with:
In SVG, to get a higher larger Z index you should move the element down in the DOM tree. You can do this with jQuery, selecting the SVG element, removing it and appending it again at the position you want: