11

I'm trying to make my table to show on the top of the overlay class. So far so bad. Please help.

http://jsfiddle.net/u96coj0q/

<table>
    <tr><td>Test</td></tr>
</table>
<button>Overlay</button>

table {
    background-color: white;
    height: 300px;
    width: 300px;
}

.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: black;
    opacity: 0.5;
    z-index: 50;
}

$('button').click(function() {
        $('body').append("<div class='overlay'></div>");
        $('table').css('zIndex', '60');
        $('table').css({ zIndex: 60 });
});

4 Answers 4

16

For z-index to work, add position: relative or position: absolute; Here is a jsfiddle

table {
    position: relative; 
    background-color: white;
    height: 300px;
    width: 300px;
}
Sign up to request clarification or add additional context in comments.

Comments

8

you can just add $('table').css({ 'position': 'relative' }); to the click event

$('button').click(function() {
    $('body').append("<div class='overlay'></div>");
    $('table').css('zIndex', '60');
    $('table').css({ 'position': 'relative' });
});

2 Comments

Why not adding the position directly in CSS as it should be? You know, all this JS can be in CSS and the only requirement to display the overlay would be to add another class to .overlay. ;)
that is just another alternative way, in case you cannot edit the css file or the HTML file
0

Add position property to table. I guess in your case you need position: relative;.

z-index will never work if the element does not have a position property set up.

table {
    position: relative; /* this is required for z-index */
    background-color: white;
    height: 300px;
    width: 300px;
}

Here is a working jsfidle: http://jsfiddle.net/Smartik/u96coj0q/3/


Here is another example that will minimize the JS and make use more of CSS: http://jsfiddle.net/Smartik/u96coj0q/5/

Comments

0

Another approach is to create a class or classes and call them with .addClass() or .toggleClass()

.zindex60 {z-index:60;}

$('button').click(function() {
   $('body').append("<div class='overlay'></div>");
   $('table').addClass('zindex60');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.