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

As others here have said, z-index is defined by the order the element appears in the DOM. If manually reordering your html isn't an option or would be difficult, you can use D3 to reorder SVG groups/objects.

Use D3 to Update DOM Order and Mimic Z-Index Functionality

Updating SVG Element Z-Index With D3

At the most basic level (and if you aren't using IDs for anything else), you can use element IDs as a stand-in for z-index and reorder with those. Beyond that you can pretty much let your imagination run wild.

Examples in code snippet

_x000D_
_x000D_
var circles = d3.selectAll('circle')_x000D_
var label = d3.select('svg').append('text')_x000D_
    .attr('transform', 'translate(' + [5,100] + ')')_x000D_
_x000D_
var zOrders = {_x000D_
    IDs: circles[0].map(function(cv){ return cv.id; }),_x000D_
    xPos: circles[0].map(function(cv){ return cv.cx.baseVal.value; }),_x000D_
    yPos: circles[0].map(function(cv){ return cv.cy.baseVal.value; }),_x000D_
    radii: circles[0].map(function(cv){ return cv.r.baseVal.value; }),_x000D_
    customOrder: [3, 4, 1, 2, 5]_x000D_
}_x000D_
_x000D_
var setOrderBy = 'IDs';_x000D_
var setOrder = d3.descending;_x000D_
_x000D_
label.text(setOrderBy);_x000D_
circles.data(zOrders[setOrderBy])_x000D_
circles.sort(setOrder);
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>_x000D_
_x000D_
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 100"> _x000D_
  <circle id="1" fill="green" cx="50" cy="40" r="20"/> _x000D_
  <circle id="2" fill="orange" cx="60" cy="50" r="18"/>_x000D_
  <circle id="3" fill="red" cx="40" cy="55" r="10"/> _x000D_
  <circle id="4" fill="blue" cx="70" cy="20" r="30"/> _x000D_
  <circle id="5" fill="pink" cx="35" cy="20" r="15"/> _x000D_
</svg>
_x000D_
_x000D_
_x000D_

The basic idea is:

  1. Use D3 to select the SVG DOM elements.

    var circles = d3.selectAll('circle')
    
  2. Create some array of z-indices with a 1:1 relationship with your SVG elements (that you want to reorder). Z-index arrays used in the examples below are IDs, x & y position, radii, etc....

    var zOrders = {
        IDs: circles[0].map(function(cv){ return cv.id; }),
        xPos: circles[0].map(function(cv){ return cv.cx.baseVal.value; }),
        yPos: circles[0].map(function(cv){ return cv.cy.baseVal.value; }),
        radii: circles[0].map(function(cv){ return cv.r.baseVal.value; }),
        customOrder: [3, 4, 1, 2, 5]
    }
    
  3. Then, use D3 to bind your z-indices to that selection.

    circles.data(zOrders[setOrderBy]);
    
  4. Lastly, call D3.sort to reorder the elements in the DOM based on the data.

    circles.sort(setOrder);
    

Examples

enter image description here

  • You can stack by ID

enter image description here

  • With leftmost SVG on top

enter image description here

  • Smallest radii on top

enter image description here

  • Or Specify an array to apply z-index for a specific ordering -- in my example code the array [3,4,1,2,5] moves/reorders the 3rd circle (in the original HTML order) to be 1st in the DOM, 4th to be 2nd, 1st to be 3rd, and so on...

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