EN
CSS - parallax scrolling
0 points
In this article, we would like to show you how to create parallax scrolling effect using CSS.
Quick solution:
xxxxxxxxxx
1
background: url("https://example.com/image.png") 0 / cover no-repeat fixed;
In order to create parallax scrolling effect, we need to set background-image
for our parallax
element with additional background properties.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
.parallax {
7
height: 250px;
8
background-image: url("https://dirask.com/static/bucket/1669209714713-xgDEWrBQWo--image.png"); /* <--- required */
9
background-size: cover;
10
background-repeat: no-repeat;
11
background-attachment: fixed; /* <--- required */
12
}
13
14
</style>
15
</head>
16
<body>
17
<div style="height: 50px; background: lightgray">
18
Scroll this web page to see the parallax scrolling effect
19
</div>
20
<div class="parallax"></div>
21
<div style="height: 500px; background: lightgray"></div>
22
</body>
23
</html>
Explanation:
background-image: url('https://example.com/image.png')
- sets image as web page background,background-attachment: fixed
- makes the background doesn't move with the element, even if an element has a scrolling mechanism,background-size: cover
- scales the image (preserving its ratio) to the smallest possible size to fill the container, leaving no empty space.background-repeat: no-repeat
- makes the background image doesn't repeat.