Search code examples
htmlcssparallax

Parallax code not working, not showing up in chrome preview


I am trying to make a parallax for the first time and am having troubles.

I'm following this tutorial and then trying to work backwards. The code isn't working however and I'm not sure where I made the mistake, I jumped around to a few other tutorials and tried to adjust the names of different divs and CSS blocks so the code is a bit messy right now.

.html {
  height: 100%;
  overflow: hidden;
}

.body {
  max-width: 30px color: #fff;
  margin: 0;
  padding: 0;
  perspective: 1px;
  transform-style: preserve-3d;
  height: 100% overflow-y: scroll;
  overflow-x: "Luna"
}

header {
  box-sizing: border-box;
  min-height: 100vh;
  padding 30vw 0 5vw;
  position: relative;
  transform-style: inherit;
  width: 100vw;
}

header h1 {
  margin-top: -100px;
}

header,
header:before {
  background: 50% 50% / cover;
}

header::before {
  bottom: 0;
  content: "";
  left: 0;
  position: absolute;
  right: 0;
  top: 0;
  display: block;
  background-image: url(picture1.jpg);
  background-size: cover;
  transform-origin: center center 0;
  transform: tranlasteZ(-1px) scale(2);
  z-index: -1;
  min-height: 100vh;
}

header * {
  font-weight: normal;
  letter-spacing: 0.2em;
  text-align: center;
  margin: 0;
  padding: 1em 0;
}

.image1 {
  background: url('img/(picture1.jpg') no-repeat center;
  background-size: cover;
  background-attachment: fixed;
  height: 500px
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Schade's Parralax</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="header">
    <p>Hi My name is schade I wrote this so I could have a test of my program.</p>
    <div class="image1"> </div>
  </div>
</body>

</html>


Solution

  • In first use a container element and add a background image to the container with a specific height. Then use the background-attachment: fixed to create the actual parallax effect.

    body,
    html {
      height: 100%;
    }
    
    h1 {
      width: 100%;
      height: 100%;
      padding: 0;
      margin: 0;
      text-transform: uppercase;
      text-align: center;
      font-family: Helvetica;
      font-size: 75px;
    }
    
    .parallax {
      background-image: url('https://images.pexels.com/photos/36764/marguerite-daisy-beautiful-beauty.jpg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260');
      height: 100%;
      /* Parallax scrolling effect */
      background-attachment: fixed; // Try to remove this property
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }
    
    .content {
      height: 300px;
      line-height: 300px;
      background: #ededed;
      position: relative;
      z-index: 1;
      box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.5);
    }
    <!DOCTYPE html>
    <html>
    
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    
    <body>
      <div class="parallax"></div>
    
      <div class="content">
        <h1>content</h1>
      </div>
    
      <div class="parallax"></div>
    </body>
    
    </html>

    Some mobile devices have a problem with background-attachment: fixed. You can use media queries to turn off the parallax effect:

    @media only screen and (max-device-width: 1366px) {
        .parallax {
            background-attachment: scroll;
        }
    }
    

    More info about fixed property.