[javascript] How to use z-index in svg elements?

We have already 2019 and z-index is still not supported in SVG.

You can see on the site SVG2 support in Mozilla that the state for z-indexNot implemented.

You can also see on the site Bug 360148 "Support the 'z-index' property on SVG elements" (Reported: 12 years ago).

But you have 3 possibilities in SVG to set it:

  1. With element.appendChild(aChild);
  2. With parentNode.insertBefore(newNode, referenceNode);
  3. With targetElement.insertAdjacentElement(positionStr, newElement); (No support in IE for SVG)

Interactive demo example

With all this 3 functions.

_x000D_
_x000D_
var state = 0,_x000D_
    index = 100;_x000D_
_x000D_
document.onclick = function(e)_x000D_
{_x000D_
    if(e.target.getAttribute('class') == 'clickable')_x000D_
    {_x000D_
        var parent = e.target.parentNode;_x000D_
_x000D_
        if(state == 0)_x000D_
            parent.appendChild(e.target);_x000D_
        else if(state == 1)_x000D_
            parent.insertBefore(e.target, null); //null - adds it on the end_x000D_
        else if(state == 2)_x000D_
            parent.insertAdjacentElement('beforeend', e.target);_x000D_
        else_x000D_
            e.target.style.zIndex = index++;_x000D_
    }_x000D_
};_x000D_
_x000D_
if(!document.querySelector('svg').insertAdjacentElement)_x000D_
{_x000D_
    var label = document.querySelectorAll('label')[2];_x000D_
    label.setAttribute('disabled','disabled');_x000D_
    label.style.color = '#aaa';_x000D_
    label.style.background = '#eee';_x000D_
    label.style.cursor = 'not-allowed';_x000D_
    label.title = 'This function is not supported in SVG for your browser.';_x000D_
}
_x000D_
label{background:#cef;padding:5px;cursor:pointer}_x000D_
.clickable{cursor:pointer}
_x000D_
With: _x000D_
<label><input type="radio" name="check" onclick="state=0" checked/>appendChild()</label>_x000D_
<label><input type="radio" name="check" onclick="state=1"/>insertBefore()</label><br><br>_x000D_
<label><input type="radio" name="check" onclick="state=2"/>insertAdjacentElement()</label>_x000D_
<label><input type="radio" name="check" onclick="state=3"/>Try it with z-index</label>_x000D_
<br>_x000D_
<svg width="150" height="150" viewBox="0 0 150 150">_x000D_
    <g stroke="none">_x000D_
        <rect id="i1" class="clickable" x="10" y="10" width="50" height="50" fill="#80f"/>_x000D_
        <rect id="i2" class="clickable" x="40" y="40" width="50" height="50" fill="#8f0"/>_x000D_
        <rect id="i3" class="clickable" x="70" y="70" width="50" height="50" fill="#08f"/>_x000D_
    </g>_x000D_
</svg>
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to svg

How to extract svg as file from web page How to display svg icons(.svg files) in UI using React Component? Why don’t my SVG images scale using the CSS "width" property? How to show SVG file on React Native? Easiest way to use SVG in Android? IE11 meta element Breaks SVG img src SVG changing the styles with CSS How to use SVG markers in Google Maps API v3 Embedding SVG into ReactJS How to change the color of an svg element?

Examples related to z-index

Bootstrap Modal sitting behind backdrop How to make a Div appear on top of everything else on the screen? Floating Div Over An Image How to use z-index in svg elements? How to make child element higher z-index than parent? Bring element to front using CSS z-index issue with twitter bootstrap dropdown menu How to "z-index" to make a menu always on top of the content Why does z-index not work? Can't change z-index with JQuery