How to use z-index in svg elements? How to use z-index in svg elements? jquery jquery

How to use z-index in svg elements?


Specification

In the SVG specification version 1.1 the rendering order is based on the document order:

first element -> "painted" first

Reference to the SVG 1.1. Specification

3.3 Rendering Order

Elements in an SVG document fragment have an implicit drawing order, with the first elements in the SVG document fragment getting "painted" first. Subsequent elements are painted on top of previously painted elements.


Solution (cleaner-faster)

You should put the green circle as the latest object to be drawn. So swap the two elements.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="30 70 160 120">    <!-- First draw the orange circle -->   <circle fill="orange" cx="100" cy="95" r="20"/>    <!-- Then draw the green circle over the current canvas -->   <circle fill="green" cx="100" cy="105" r="20"/> </svg>