Search code examples
htmlcssparallax

Simple CSS parallax creates whitespace in Chrome


I'm trying to make a parallax layout for a site. Following a tutorial that has a great end result but is too much for my needs, so I just left the first two layers. But this results in a big white space right below the last layer that I only see en chrome and not firefox but have no idea how to get rid of. Here is a jsfiddle snippet, as you can see it is very little code but I can't find where this could be coming from. Of course the error only shows in this fiddle if you open it in chrome.

/* Parallax base styles
  --------------------------------------------- */

  .parallax {
    height: 500px; /* fallback for older browsers */
    height: 100vh;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-perspective: 300px;
    perspective: 300px;
    -webkit-perspective-origin-x: 100%;
    perspective-origin-x: 100%;
  }

  .parallax__group {
    position: relative;
    height: 500px; /* fallback for older browsers */
    height: 100vh;
    -webkit-transform-style: preserve-3d;
    transform-style: preserve-3d;
  }

  .parallax__layer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    -webkit-transform-origin-x: 100%;
    transform-origin-x: 100%;
  }

  .parallax__layer--fore {
    -webkit-transform: translateZ(90px) scale(.7);
    transform: translateZ(90px) scale(.7);
    z-index: 1;
  }

  .parallax__layer--base {
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    z-index: 4;
  }

  .parallax__layer--back {
    -webkit-transform: translateZ(-290px) scale(2);
    transform: translateZ(-290px) scale(2);
    z-index: 3;
  }

  .parallax__layer--deep {
    -webkit-transform: translateZ(-600px) scale(3);
    transform: translateZ(-600px) scale(3);
    z-index: 2;
  }
  
  /* demo styles */
  
  body, html {
    overflow: hidden;
  }

  body {
    font: 100% / 1.5 Arial;
  }

  * {
    margin:0;
    padding:0;
  }

  .parallax {
    font-size: 200%;
  }

   /* centre the content in the parallax layers */
  .title {
    text-align: center;
    position: absolute;
    left: 50%;
    top: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
  }

  #group1 {
    z-index: 5; /* slide over group 2 */
  }
  #group1 .parallax__layer--base {
    background: rgb(102,204,102);
  }

  #group2 {
    z-index: 3; /* slide under groups 1 and 3 */
  }
  #group2 .parallax__layer--back {
    background: rgb(123,210,102);
  }
<div class="parallax">
        <div id="group1" class="parallax__group">
          <div class="parallax__layer parallax__layer--base">
            <div class="title">Base Layer</div>
          </div>
        </div>
        <div id="group2" class="parallax__group">
          <div class="parallax__layer parallax__layer--back">
            <div class="title">Background Layer</div>
          </div>
        </div>
      </div>


Solution

  • I think your expected result is like this.

    The tricky things is that you need to set the position of background layer as absolute but not relative. Then you will manage easier.

    The reason of your code doesnt work is that the browser may read your background as position relative first, and then your inside div you set it absolute, transform: translateZ(-290px), so that there is a space at the bottom. For another example, just like you set a div height as 600px and then set the padding-bottom as 400px which means the height is 600px, but the actual content will be 200px. You know css is quite stupid.Haha

    /* Parallax base styles
      --------------------------------------------- */
    
      .parallax {
    overflow-x: hidden;
        overflow-y: auto;
         -webkit-perspective: 300px; 
        perspective: 300px;
      }
    
      .parallax__group {
    
      }
    
      .parallax__layer {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        -webkit-transform-origin-x: 100%;
        transform-origin-x: 100%;
      }
    
      .parallax__layer--base {
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
        z-index: 4;
      }
    
      .parallax__layer--back {
    background: rgb(123,210,102);
      }
    
    
    
      .parallax {
        font-size: 200%;
      }
    
       /* centre the content in the parallax layers */
      .title {
        text-align: center;
        position: absolute;
        left: 50%;
        top: 50%;
        -webkit-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
      }
    
      #group1 {
      position: relative;
      height: 100vh;
          -webkit-transform-style: preserve-3d;
        transform-style: preserve-3d;
        z-index: 5; /* slide over group 2 */
      }
      #group1 .parallax__layer--base {
        background: rgb(102,204,102);
      }
    
      #group2 {
        z-index: 3;
        position: absolute;
        width: 100%;
        transform: translateZ(-290px) scale(2);
        height: 100vh;
        -webkit-transform-style: preserve-3d;
        transform-style: preserve-3d;
      }
    <div class="parallax">
            <div id="group1" class="parallax__group">
              <div class="parallax__layer parallax__layer--base">
                <div class="title">Base Layer</div>
              </div>
            </div>
            <div id="group2" class="parallax__group">
              <div class="parallax__layer parallax__layer--back">
                <div class="title">Background Layer</div>
              </div>
            </div>
          </div>