Is there a way in jQuery to bring a div to front?
Solution 1
This is a CSS thing, not a jQuery thing (though you can use jQuery to modify the css).
$('element').css('z-index', 9999); // note: it appears 'zIndex' no longer works
Or, absolute positioning will bring something to the top:
$('element').css('position', 'absolute');
Solution 2
With jQuery (like you asked):
$('#a-div').parent().append($('#a-div')); // Pop a div to the front
Weird how everyone went with z-index. Why override natural stacking order when you can just pop it to the front within the DOM?
Solution 3
When you are working with multiple elements you'll have to loop through and see which has the highest z-index, and set the top to that+1.
http://jsfiddle.net/forresto/Txh7R/
var topZ = 0;
$('.class-to-check').each(function(){
var thisZ = parseInt($(this).css('zIndex'), 10);
if (thisZ > topZ){
topZ = thisZ;
}
});
$('.clicked-element').css('zIndex', topZ+1);
For a non-CSS version (if none of the layers have z-index specified) you can also just append the element again:
$('.click-to-top').click(function(){
$(this).parent().append(this);
});
(I don't know if that is slower than with CSS z-index
.)
For non-CSS non-jQuery, just append it again:
// var element = document...;
element.parentNode.appendChild(element);
Solution 4
Use .css() and apply position attribute and z-index attribute.
Solution 5
It would be overkill to use jQuery for this. Instead use CSS' z-index property:
<style type="text/css">
#myElement {
z-index: 50000;
}
</style>
anthonypliu
Updated on February 18, 2020Comments
-
anthonypliu about 4 years
If I had a bunch of absolute positioned divs and they overlapped, how would I get a certain div to come to the front? Thanks again guys!
-
airmanx86 almost 14 yearsAlternatively apply a class that has those attribute that you need with jQuery.
-
Amr Elgarhy almost 14 yearsi think you will need both, so position:absolute AND z-inde:1
-
RAmPE about 13 yearsI initially agreed with this answer but it sounds like the question is about bringing a certain div (of overlapping divs) to the front when needed. Setting a particular div to the top in the static markup won't solve the issue.
-
Mavelo over 12 yearsagreed, but this really belongs in the css unless you are reacting to user interactions
-
Greg over 11 yearsIt seems like he wants to do this dynamically and wants to know if jQuery has this functionality.
-
dennisbest about 11 yearsThis is a helpful answer for those who want something dynamic, particularly if you're using draggable elements.
-
Imran Balouch almost 10 yearsAwesome idea and approach, yeh it is weird to go for z-index when this approach exists, and I accept that first thing that came in my mind was z-index :$
-
qualebs over 9 yearsIs this approach appending multiple elements of the child being appended? please clarify that for me.
-
Yarin over 9 years@qualebs no it's grabbing an existing element and moving it to top of stacking order
-
qualebs over 9 years@Yarin thanks a bunch you have saved me a paragraph of boilerplate code. This answer should be the accepted answer.
-
Nick Bedford over 9 yearsMoving the div to the end of the parent's node list also usually brings it to the top. Without using z-index, the draw order is typically first to last elements, hierarchically of course
-
teynon over 8 years5 years later. How do I bring a div to the front? Answer:
z-index: 5000000000000;
-
William Isted about 8 yearsBasically it has to be 1 higher than the highest
z-index
on the page. -
Simas Joneliunas about 4 yearsPlease consider leaving a comment on how your code helps to answer the question