Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and setting z-index values with jQuery on Webkit browsers

jQuery("#X").css("z-index"); always returns "auto" on Webkit browsers. Works ok on Firefox.

It also appears that you can't set the z-index with something like: jQuery("#X").css("z-index",5);

Test case follows:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>jQuery CSS Z-index Fail</title>
    <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){
        $("#X").css("z-index",5);       
        $("#Y").append($("#X").css("z-index"));
      });
    </script>
    <style type="text/css">
      h1 {z-index: 3;}
    </style>
  </head>
  <body>
     <h1 id="X">Some Text</h1>
     <div id="Y"> Z-index is: </div>
  </body>
</html>
like image 306
NetFool Avatar asked Jun 25 '11 06:06

NetFool


1 Answers

You NEED to specify position to use z-index.

<h1 id="X" style='position:relative;'>Some Text</h1>

fixes it immediately in Chrome.

like image 165
AR. Avatar answered Sep 20 '22 17:09

AR.