Create a layered parallax effect with HTML & CSS

Kevin Powell · Beginner ·🌐 Frontend Engineering ·3y ago

Key Takeaways

Create a layered parallax effect with HTML and CSS using mask-image, without any JavaScript, and utilize tools like Figma and Pathfinder for SVG mask creation. The video demonstrates a step-by-step process for achieving this effect, including adding background images, creating and applying SVG masks, and ensuring cross-browser compatibility.

Full Transcript

I'm sure you've all seen the simple Parallax effect you can do like this where the text is moving but the background isn't oh it looks alright but it has been done before so why not step things up with just a little bit more CSS and create something where the background is moving but the text is fixed and then hides away in a really fun way if you want to learn how to do this then that's what we're going to be covering in this video [Music] hello my friend and friends thank you so much for coming back for yet another video and just in case you're new here my name is Kevin and here at my channel I help you fall madly deeply in love with CSS and one way I think we can do that is by making fun and cool things so let's dive right into it and take a look at the code that we're starting with which is very very simple and not very much where I have a header with a class on it and then my header inside of there my H1 right there now this does not need to be a header with whatever you just need to have like your parent and then the text inside of there but right now we don't have very much going on with that so in my Styles really quickly we can add in some styling just to make this a little bit bigger basically I'm using a clamp just to give it a really big font size that is responsive I have a text transform on there font weight 900 we just need some big text then on the parent of that we need to add the background image that we want to be using so background image however you want um position it if you feel like you need to position it and I added a Min height and some padding just to give it sort of the space that it needs to live the one thing that's kind of important for this if you're doing it is to make sure you have like a space underneath like whether this is buildings whether it's trees whatever it is you need to have like the room for the text Above It generally so the five Ram of padding but then that Min height just does build in a little bit of an extra space there if ever we're on a smaller screen the text is wrapping the space can grow to accommodate and that is the basis of what we need to get started with this ideally for your image you're going to have something that does have like a foreground and background and the cleaner the lines are in the foreground the easier this is going to be because we do need to create an SVG mask for this so I went and used figma to be able to do this and we're just going to fast forward myself creating this mask right here now before creating the mask itself in my preferences I am setting things up to be turning off the snapping Behavior because that just really makes it hard to do for this type of thing you probably want to turn it back on when you're done though if you do use figma regularly and then basically I trace the shape that I want to have I did make a mistake along the way because I should have traced the sky because the important thing here is we need a mask or we need to like draw a shape that is the area that will be visible where we will see the text and not where we won't be seeing the text the other thing that's really important is in figma you want to make sure that your entire canvas area for your SVG fills the original size of the image that you're going to be using and that's just to be able to get things to line up and everything so because I chose the wrong area it was a little bit more work but basically you want to have the top area being full and then the bottom area being completely transparent you can use like their Pathfinder tools to make these different layers which I did here I did some copying and pasting just to have one section that's my sky and one section that's my mountains that's basically what you need one top section one bottom section and then the bottom section you just make it fully transparent at least that's how I figured the best way to do it is take the two of them group them together and then you can export that as your SVG in this case my Mountain was also very bumpy and that made it a lot more work because I really had to zoom in and be very careful with my my path the straighter the lines the simpler it is the easier this is going to be and if you do need something that's actually like faded out like I don't know you're doing something fluffy or soft edged then you'll actually need to do this probably with a raster software like Photoshop or maybe but you could be used masking to do it and then you could export as a PNG instead with transparency but of course that makes for a much heavier file and then what do we do with it well it's not that bad we just come in and we need to wrap this in what is going to contain our mask so here I've given my sight header mask and then I have my site title inside of there and we just you just need to have that layering on there so the first thing we can do then is to apply this mask or actually before we add the mask in on the site title here let's just come and give this a position of fixed and if we do position fix you can see it does shift a little bit and it's going to scroll and move with the site and this is very important this is sort of the key to everything working here as we have my position fixed and then I'm just going to do a whip back to 100 which is going to let my text align Center work once again and there we go this is scrolling with my site and again that is sort of the key for what we want now let's come in here and we're going to add our site header uh mask and on the side header mask let's let's just start by doing we're going to give it a position of absolute and I want it to be positioned within my header here so on the header I'm also going to add a position of relative just so this becomes the containing block and then here I have my mask that's going to be in there now I'm going to give this an inset of 0 which is a top bottom left and right of zero it's a shorthand for that and then just for now I'm adding this background on here so we can see that it's overlaying exactly and here if I just change this to like A2 REM you can see it sort of shrinks it in so by doing a zero there it's filling up the entire space that my say header was which is filled up with that background image just a really quick aside here I did Cover masks in more detail in another video and when I was researching that I found this effect by the channel red stapler I don't know if he's still making content it's been a little while but I'll link to his original video down in the description below as well as his channel just because he did Inspire this video that we were watching right now and now to add the mask in it's nice and simple we can just write mask and put in the URL that we want and it's not really going to work right away if you're following along but don't worry there's a very easy solution and this is just like a background image so I'm going to go to my images and I had the mask that I saved so that was my mountainmask.svg and when I save nothing happens and that's because I am running Chrome here and the chromium browsers uh are awesome and we still need to prefix it and web kit when you're prefixing you might not see it too much these days always use the first one the prefix version at the top and then the regular one at the bottom now because my image is really big we might actually see that this is going to be you don't see a difference but look it's actually disappearing when we get to here now so that's you see the magic of what's this is sort of the idea of what we want to do but I need to make sure that my mask is lining up with my image and basically all we have to do is add this Mass position and this Mass size here which are the same as my background size and my background position that are here as long as those two values match these two values you're perfectly fine you could create a custom property there I guess too to set that up for each if you prefer and of course I'm using Chrome so I do need to get these so let's just copy this so you know for it to actually work we need to prefix it so we can come on both of those and just add the webkit prefix and there we go you can see the red has disappeared from there and now if I scroll down the magic happens and it hides behind my mountains uh so it looks pretty good my mask is a little bit rough around the edges in a few spots the one thing I will say is if you're when you're creating your mask if you're having trouble with it or if you're not sure stay a little bit like on the darker side instead of the lighter side if you have a color difference because it's going to make it less noticeable when it's like if you have a little bit of a bad section in your selection that you created and the nice thing here is obviously I have this red on here I could turn the red off completely uh the only issue is then like sometimes your text might be a little bit hard to read but we have this natural overlay that we could use here so I could just put this to zero to be black or if my text was black I can make this white and now if I save it darkens my image and if I want it darker just make the opacity higher and I can control sort of that type of thing and I can get this really fun cool effect going on right there now if you'd like to know more about image mask I did cover it in a bit more detail and look at more things that you could do with it that are not Parallax related in this video that is right here for your viewing pleasure and with that I would like to thank my enablers of awesome Jan Johnny Michael Mr Dave Patrick Simon and Tim as well as all my other patrons for their monthly support and of course until next time don't forget to make your on the internet just a little bit more awesome [Music]

Original Description

There are different ways to create parallax effects, but using mask-image might be the one that, without any JavaScript at least, is the most interesting! It takes some time to create the mask, but it can be well worth it! 🔗 Links ✅ Code from this video: https://codepen.io/kevinpowell/pen/jOKprzj??editors=1100 ✅ My introduction to mask-image: https://youtu.be/FCuNtCq3nRg ✅ Video by Red Stapler that inspired this one: https://www.youtube.com/watch?v=F22rI8jpaV4 ✅ Red Stapler’s channel: https://www.youtube.com/@RedStapler_channel ⌚ Timestamps 00:00 - Introduction 00:38 - Setting things up for this to work 02:00 - Creating the mask 03:43- Creating the parallax effect 07:17 - Making the text more readable #css -- Come hang out with other dev's in my Discord Community 💬 https://discord.gg/nTYCvrK Keep up to date with everything I'm up to ✉ https://www.kevinpowell.co/newsletter Come hang out with me live every Monday on Twitch! 📺 https://www.twitch.tv/kevinpowellcss --- Help support my channel 👨‍🎓 Get a course: https://www.kevinpowell.co/courses 👕 Buy a shirt: https://teespring.com/stores/making-the-internet-awesome 💖 Support me on Patreon: https://www.patreon.com/kevinpowell --- My editor: VS Code - https://code.visualstudio.com/ --- I'm on some other places on the internet too! If you'd like a behind the scenes and previews of what's coming up on my YouTube channel, make sure to follow me on Instagram and Twitter. Twitter: https://twitter.com/KevinJPowell Codepen: https://codepen.io/kevinpowell/ Github: https://github.com/kevin-powell --- And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Kevin Powell · Kevin Powell · 0 of 60

← Previous Next →
1 How to create an awesome navigation bar with HTML & CSS
How to create an awesome navigation bar with HTML & CSS
Kevin Powell
2 Improve your CSS by Keepin' it DRY
Improve your CSS by Keepin' it DRY
Kevin Powell
3 HTML & CSS for Beginners Part 6: Images
HTML & CSS for Beginners Part 6: Images
Kevin Powell
4 HTML & CSS for Beginners Part 7: File Structure
HTML & CSS for Beginners Part 7: File Structure
Kevin Powell
5 HTML & CSS for Beginners Part 4: Bold and Italic text and HTML comments
HTML & CSS for Beginners Part 4: Bold and Italic text and HTML comments
Kevin Powell
6 HTML & CSS for Beginners Part 5: Links
HTML & CSS for Beginners Part 5: Links
Kevin Powell
7 HTML & CSS for Beginners Part 3: Paragraphs and Headings
HTML & CSS for Beginners Part 3: Paragraphs and Headings
Kevin Powell
8 HTML and CSS for Beginners Part 1: Introduction to HTML
HTML and CSS for Beginners Part 1: Introduction to HTML
Kevin Powell
9 HTML and CSS for Beginners Part 2: Building your first web page!
HTML and CSS for Beginners Part 2: Building your first web page!
Kevin Powell
10 HTML & CSS for Beginner Part 8: Introduction to CSS
HTML & CSS for Beginner Part 8: Introduction to CSS
Kevin Powell
11 HTML & CSS for Beginners Part 9: External CSS
HTML & CSS for Beginners Part 9: External CSS
Kevin Powell
12 HTML & CSS for Beginners Part 10: Divs & Spans
HTML & CSS for Beginners Part 10: Divs & Spans
Kevin Powell
13 HTML & CSS for Beginners Part 11: Classes & IDs
HTML & CSS for Beginners Part 11: Classes & IDs
Kevin Powell
14 HTML & CSS for Beginners Part 12: The CSS Box Model - Margin, Borders & Padding explained
HTML & CSS for Beginners Part 12: The CSS Box Model - Margin, Borders & Padding explained
Kevin Powell
15 HTML & CSS for Beginners Part 13: Background Images
HTML & CSS for Beginners Part 13: Background Images
Kevin Powell
16 HTML & CSS for Beginners Part 14: Style Text with CSS
HTML & CSS for Beginners Part 14: Style Text with CSS
Kevin Powell
17 HTML & CSS for Beginners Part 15: How to style links
HTML & CSS for Beginners Part 15: How to style links
Kevin Powell
18 HTML & CSS for Beginners Part 16: CSS selectors and Specificity
HTML & CSS for Beginners Part 16: CSS selectors and Specificity
Kevin Powell
19 HTML & CSS for Beginners Part 17: How to Create and Style HTML Lists
HTML & CSS for Beginners Part 17: How to Create and Style HTML Lists
Kevin Powell
20 HTML & CSS for Beginners Part 18: How Floats and Clears work
HTML & CSS for Beginners Part 18: How Floats and Clears work
Kevin Powell
21 HTML & CSS for Beginners Part 19: Colors with CSS - hex, rgba, and hsla
HTML & CSS for Beginners Part 19: Colors with CSS - hex, rgba, and hsla
Kevin Powell
22 HTML & CSS for Beginners Part 20: How to center a div
HTML & CSS for Beginners Part 20: How to center a div
Kevin Powell
23 HTML & CSS for Beginners Part 21: How to create a basic website layout - the HTML
HTML & CSS for Beginners Part 21: How to create a basic website layout - the HTML
Kevin Powell
24 HTML & CSS for Beginners Part 22: How to create a basic layout - the CSS
HTML & CSS for Beginners Part 22: How to create a basic layout - the CSS
Kevin Powell
25 How to Create a Responsive Website from Scratch - Part 1: The HTML #Responsive #HTML5
How to Create a Responsive Website from Scratch - Part 1: The HTML #Responsive #HTML5
Kevin Powell
26 How to Create a Responsive Website from Scratch - Part 2: The Header and Hero area #Responsive #CSS3
How to Create a Responsive Website from Scratch - Part 2: The Header and Hero area #Responsive #CSS3
Kevin Powell
27 How to Create a Responsive Website from Scratch - Part 3: The About Section #Responsive #CSS
How to Create a Responsive Website from Scratch - Part 3: The About Section #Responsive #CSS
Kevin Powell
28 How to Create a Responsive Website from Scratch - Part 4: Building a Responsive Portfolio Section
How to Create a Responsive Website from Scratch - Part 4: Building a Responsive Portfolio Section
Kevin Powell
29 How to Create a Responsive Website from Scratch - Part 5: Call To Action and Footer #CSS #Responsive
How to Create a Responsive Website from Scratch - Part 5: Call To Action and Footer #CSS #Responsive
Kevin Powell
30 Tutorial: Learn how to use CSS Media Queries in less than 5 minutes
Tutorial: Learn how to use CSS Media Queries in less than 5 minutes
Kevin Powell
31 End of the year upate and what's coming to my channel to start the new year
End of the year upate and what's coming to my channel to start the new year
Kevin Powell
32 Create a CSS only Mega Dropdown Menu
Create a CSS only Mega Dropdown Menu
Kevin Powell
33 CSS Tutorial: Outline and Outline Offset
CSS Tutorial: Outline and Outline Offset
Kevin Powell
34 CSS Blending Modes
CSS Blending Modes
Kevin Powell
35 Parallax effect | 2 different ways to add it with jQuery
Parallax effect | 2 different ways to add it with jQuery
Kevin Powell
36 CSS Units: vh, vw, vmin, vmax #css #responsive #design
CSS Units: vh, vw, vmin, vmax #css #responsive #design
Kevin Powell
37 How to Create a Website - Complete workflow | Part 01: Intro + Setting things up
How to Create a Website - Complete workflow | Part 01: Intro + Setting things up
Kevin Powell
38 100 Subscribers speed coding bonus video
100 Subscribers speed coding bonus video
Kevin Powell
39 How to Create a Website - Complete workflow | Part 02: The Markup #HTML
How to Create a Website - Complete workflow | Part 02: The Markup #HTML
Kevin Powell
40 How to Create a Website - Complete workflow | Part 03: Sass Variables and a Mixin #Sass
How to Create a Website - Complete workflow | Part 03: Sass Variables and a Mixin #Sass
Kevin Powell
41 How to Create a Website - Complete workflow | Part 04: Setting up the hero and header
How to Create a Website - Complete workflow | Part 04: Setting up the hero and header
Kevin Powell
42 How to Create a Website - Complete workflow | Part 05: Typography & Buttons
How to Create a Website - Complete workflow | Part 05: Typography & Buttons
Kevin Powell
43 How to Create a Website - Complete workflow | Part 06.1: Building the navigation with Flexbox
How to Create a Website - Complete workflow | Part 06.1: Building the navigation with Flexbox
Kevin Powell
44 How to Create a Website - Complete workflow | Part 06.2: Making the nav work with jQuery
How to Create a Website - Complete workflow | Part 06.2: Making the nav work with jQuery
Kevin Powell
45 Redesigning & Coding My Website #CreateICG
Redesigning & Coding My Website #CreateICG
Kevin Powell
46 How to Create a Website - Complete workflow | Part 07: Starting the flexbox grid
How to Create a Website - Complete workflow | Part 07: Starting the flexbox grid
Kevin Powell
47 How to Create a Website - Complete workflow | Part 08: Promo & Problem shooting!
How to Create a Website - Complete workflow | Part 08: Promo & Problem shooting!
Kevin Powell
48 How to Create a Website - Complete workflow | Part 09: The CTA and Footer
How to Create a Website - Complete workflow | Part 09: The CTA and Footer
Kevin Powell
49 How to Create a Website - Complete workflow | Part 10: Making it responsive
How to Create a Website - Complete workflow | Part 10: Making it responsive
Kevin Powell
50 How to Create a Website - Complete workflow | Part 11: Making it responsive con't
How to Create a Website - Complete workflow | Part 11: Making it responsive con't
Kevin Powell
51 How to Create a Website - Complete workflow | Part 12: Putting the site online
How to Create a Website - Complete workflow | Part 12: Putting the site online
Kevin Powell
52 Create a Custom Grid System with CSS Calc() and Sass
Create a Custom Grid System with CSS Calc() and Sass
Kevin Powell
53 CSS em and rem explained #CSS #responsive
CSS em and rem explained #CSS #responsive
Kevin Powell
54 Should you use Bootstrap?
Should you use Bootstrap?
Kevin Powell
55 How to add Smooth Scrolling to your one page website with jQuery
How to add Smooth Scrolling to your one page website with jQuery
Kevin Powell
56 Let's learn Bootstrap 4
Let's learn Bootstrap 4
Kevin Powell
57 How I approach designing a website - my thought process
How I approach designing a website - my thought process
Kevin Powell
58 Build a website with Bootstrap 4 - Part 1: The setup
Build a website with Bootstrap 4 - Part 1: The setup
Kevin Powell
59 Build a website with Bootstrap 4 - Introduction
Build a website with Bootstrap 4 - Introduction
Kevin Powell
60 Build a website with Bootstrap 4 - Part 2:  Customizing Variables
Build a website with Bootstrap 4 - Part 2: Customizing Variables
Kevin Powell

Learn how to create a layered parallax effect using HTML, CSS, and SVG masks, and discover how to ensure cross-browser compatibility. This video tutorial provides a step-by-step guide on achieving this effect without using JavaScript.

Key Takeaways
  1. Add a background image to the parent of the text
  2. Create an SVG mask in Figma
  3. Use Pathfinder tools to make different layers in the mask
  4. Apply the mask to the site title
  5. Give the site title position fixed and the site header position relative
  6. Add webkit prefix for cross-browser compatibility
  7. Create a custom property for setting up mask alignment
  8. Adjust opacity for darkening the image
💡 Using mask-image and SVG masks can create a layered parallax effect without JavaScript, and utilizing tools like Figma and Pathfinder can simplify the process.

Related Reads

📰
The React Compiler Just Made Your Memoization Knowledge Obsolete. Here’s What to Learn Instead.
React's new compiler makes memoization knowledge obsolete, learn about automatic memoization and React's compiler optimizations instead
Medium · JavaScript
📰
You don't need a backend to store form submissions. You need a place to ask "how many."
Learn how to store form submissions without a backend and understand the importance of reporting and aggregation
Dev.to · Omer Hochman
📰
The Matrix of Layouts: CSS Grid vs Flexbox
Learn when to use CSS Grid vs Flexbox for layouts and why it matters for efficient web development
Dev.to · Timevolt
📰
Memoization useMemo()Explained: Why React Doesn’t Need to Repeat the Same Work
Learn how React's useMemo() hook uses memoization to optimize performance by avoiding redundant calculations
Medium · JavaScript

Chapters (4)

Introduction
0:38 Setting things up for this to work
2:00 Creating the mask
7:17 Making the text more readable
Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →