✨ 2 Parallax Background Effects in CSS - Simple & Horizontal Parallax : Part1

WHAT TO KNOW - Sep 1 - - Dev Community

<!DOCTYPE html>











2 Parallax Background Effects in CSS - Simple & Horizontal Parallax: Part 1



<br>
body {<br>
font-family: sans-serif;<br>
margin: 0;<br>
}</p>
<div class="highlight"><pre class="highlight plaintext"><code> .container {
position: relative;
overflow: hidden;
height: 500px;
}
.layer {
    position: absolute;
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}

.layer-1 {
    background-image: url("https://images.unsplash.com/photo-1501870190084-cdf29f15ef87?ixlib=rb-4.0.3&amp;amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=1950&amp;amp;q=80");
    background-attachment: fixed;
}

.layer-2 {
    background-image: url("https://images.unsplash.com/photo-1498583368813-27a9c347c929?ixlib=rb-4.0.3&amp;amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=1950&amp;amp;q=80");
    background-attachment: scroll;
}

.layer-2 {
    background-position: center;
}

.layer-3 {
    background-image: url("https://images.unsplash.com/photo-1454281542913-95c77ea14560?ixlib=rb-4.0.3&amp;amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=1950&amp;amp;q=80");
    background-attachment: scroll;
}

.layer-3 {
    background-position: center;
}

.layer-4 {
    background-image: url("https://images.unsplash.com/photo-1512476527991-5c2778273d83?ixlib=rb-4.0.3&amp;amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=1950&amp;amp;q=80");
    background-attachment: scroll;
}

.layer-4 {
    background-position: center;
}

.content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    text-align: center;
    z-index: 10;
}

h1 {
    font-size: 3em;
}

p {
    font-size: 1.2em;
}
Enter fullscreen mode Exit fullscreen mode

</code></pre></div>
<p>








2 Parallax Background Effects in CSS - Simple & Horizontal Parallax: Part 1








Introduction





Parallax is a visual effect that creates an illusion of depth by moving background elements at different speeds relative to the foreground. This effect is often used in web design to add visual interest and engagement to a website. It can create a sense of movement and depth, enhancing the user experience.





In this article, we'll explore two popular parallax techniques in CSS: Simple Parallax and Horizontal Parallax. We'll cover their implementations, advantages, and limitations. Let's dive into the exciting world of parallax effects!










Simple Parallax





Simple Parallax is the most basic form of parallax. It involves layering background images with different scrolling speeds. The background images are set to fixed or scrolling backgrounds, creating the illusion of depth as the user scrolls.





The key to creating this effect is to set the background-attachment property of the different layers. A background-attachment of "fixed" will ensure that the image remains stationary as the user scrolls, while a background-attachment of "scroll" will make the image move along with the user's scrolling.





Here's a basic example of how to implement simple parallax using CSS:


























Simple Parallax





Scroll down to see the parallax effect.









In this code:



  • We create four layers using the "layer" class and assign them different background images.
  • The first layer ("layer-1") has a background-attachment of "fixed", making it stationary.
  • The remaining layers ("layer-2", "layer-3", and "layer-4") have a background-attachment of "scroll", making them scroll with the user.
  • The "content" element holds the text and is positioned above the layers to be visible.




As you scroll, the background images move at different speeds, creating a simple parallax effect. This technique is great for adding subtle depth and movement to your designs.










Horizontal Parallax





Horizontal Parallax creates a sense of movement across the screen. It typically involves a background image that is wider than the viewport and scrolls horizontally at a slower rate than the content. This gives the impression that the background is moving laterally as the user scrolls vertically.





Horizontal Parallax can be implemented using CSS transforms. The key here is to set the transform property of the background element to translateX and then animate it based on the scroll position.





Here's an example of how to implement horizontal parallax using CSS:


























Horizontal Parallax





Scroll down to see the parallax effect.









In this code:



  • We create four layers using the "layer" class and assign them different background images.
  • The "layer-1" has a background-attachment of "fixed", making it stationary.
  • The remaining layers ("layer-2", "layer-3", and "layer-4") have a background-attachment of "scroll", making them scroll with the user.
  • The "content" element holds the text and is positioned above the layers to be visible.




As you scroll, the background images move at different speeds, creating a simple parallax effect. This technique is great for adding subtle depth and movement to your designs.





To create a horizontal parallax effect, we would need to adjust the CSS code. For example, we could add a new class "horizontal-parallax" to the layer elements and apply a transform to them with a translateX value that is proportional to the scroll position.










Advantages and Limitations of Parallax






Advantages





  • Enhanced User Engagement:

    Parallax effects add visual interest and depth, making websites more engaging and captivating for users.


  • Improved Storytelling:

    By creating a sense of movement and depth, parallax can enhance the storytelling aspect of a website, adding context and dimension to the content.


  • Creative Design Possibilities:

    Parallax opens up a vast array of design possibilities, allowing designers to create unique and innovative website layouts and experiences.





Limitations





  • Performance Issues:

    Excessive parallax effects can negatively impact website performance, especially on mobile devices. It's crucial to optimize the number of layers and animations to maintain a smooth user experience.


  • Accessibility Concerns:

    Parallax effects can pose accessibility challenges for users with visual impairments or using screen readers. It's essential to ensure that the content remains accessible despite the visual effects.


  • Compatibility Issues:

    Parallax effects might not be fully supported in all browsers, especially older versions. It's crucial to test the effect across different browsers to ensure compatibility.









Conclusion





Parallax effects are a valuable tool in the web design toolkit, adding depth, engagement, and a sense of movement to websites. Simple and horizontal parallax are two easy-to-implement techniques that can enhance the user experience. However, it's essential to consider the potential performance, accessibility, and compatibility implications before incorporating parallax into a website.





Remember to balance visual appeal with performance and accessibility to create a website that is both engaging and functional for all users. Stay tuned for Part 2 of this series, where we'll explore more advanced parallax techniques and delve into real-world examples.






. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player