Perspective Shift

In this example below you will see how to do a Perspective Shift with some HTML / CSS and Javascript

This awesome code was written by matt-west, you can see more from this user in the personal repository.
You can find the original code on
Copyright matt-west ©


  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

  <meta charset="UTF-8">
  <title>Perspective Shift</title>
      <link rel="stylesheet" href="css/style.css">



  <div class="container">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <script src=''></script>


    <script  src="js/index.js"></script>



/*Downloaded from */
.container {
  position: relative;
  width: 400px;
  margin: 100px auto;

.box {
  width: 400px;
  height: 200px;
  background: red;
  position: absolute;
  top: 40px;
  left: 0;
  z-index: 3;
.box:nth-child(2) {
  background: blue;
  width: 340px;
  height: 160px;
  top: 20px;
  left: 30px;
  z-index: 2;
.box:nth-child(3) {
  background: green;
  width: 280px;
  height: 140px;
  top: 0;
  left: 60px;
  z-index: 1;

/*Downloaded from */
$(function() {

  var boxTwo = document.querySelector('.box:nth-child(2)');
  var boxThree = document.querySelector('.box:nth-child(3)');

  if (window.DeviceOrientationEvent) {
    window.addEventListener("deviceorientation", function(event) {
      var xValue = Math.round(event.gamma);
      var yValue = Math.round(event.beta);
      var Rotation = Math.round(event.alpha);


    }, true);