
CSS Colors and Backgrounds: Properties, Values, and Examples
11/10/2019
CSS Text Properties: Alignment, Spacing, and Decoration
11/10/2019
Parallax is a web design technique that creates depth by making layers of a page appear to move at different speeds as the visitor scrolls. A background image or texture often appears to move more slowly than the content in front of it.
1. How to create a parallax effect with HTML and CSS
This lesson shows how to implement the effect using HTML and CSS.
2. What is the parallax effect?
Nearby objects appear to move faster than distant objects as your viewpoint changes. On a web page, moving content layers at different speeds can reproduce that impression and draw attention to particular sections.
3. Related terms and concepts
You may encounter the following terms in web design and animation:
- Scrolling effect: Any animation or visual change that happens when the page scrolls.
- Parallax scrolling: Layers move at different apparent speeds to create a sense of depth.
- Depth effect: The impression of three dimensions created by differences in movement.
- Layered scrolling: Several layers of content move at different speeds.
- Scroll-based animation: An animation triggered by, or linked to, scrolling.
- Fixed background scrolling: A variation where the background stays fixed while the content scrolls over it.
4. Steps for creating a parallax effect
4.1. Create the HTML containers
Start with an outer container for the parallax background and an inner container for the content.
<div class="parallax">is the outer container where the background image goes.<div class="content">holds the foreground content, in this case an<h1>and a paragraph.
<div class="parallax">
<div class="content">
<h1>Welcome to our site</h1>
<p>Scroll to see the parallax effect.</p>
</div>
</div>
4.2. Add the CSS properties
Add CSS to the .parallax container. The example uses these properties:
- .parallax: The selector applies the following rules to the outer container.
background-imagespecifies the image URL.heightsets the container’s height.background-attachmentfixes the image to the viewport to produce the parallax effect.background-positioncenters the background image.background-repeatprevents the image from tiling.background-sizemakes the image cover the container.
body, html {
margin: 0;
font-family: Arial, sans-serif;
}
.parallax {
background-image: url('your-image.jpg');
height: 600px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.content {
height: 500px;
text-align: center;
padding: 50px;
}
For more examples of these properties, read the lesson on CSS colors and backgrounds.
Choose an image with enough resolution to stay sharp on large screens. Test the result on a phone, where fixed backgrounds can behave differently. If you add motion, respect the visitor’s reduced-motion preference.
4.3. Practical example with a fixed background
This call to action uses a background image behind a message asking visitors to do something, such as buy a product or subscribe to a service.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Call to Action</title>
<style>
*{font-family: Arial;}
.cta-container {
background-image: url("cta-background.jpg"); /* Background image */
background-size: cover; /* Cover the entire container */
background-position: center center; /* Center the image */
background-repeat: no-repeat; /* Do not tile the background image */
background-attachment: fixed; /* Fixed relative to the viewport */
opacity: 0.9; /* Container opacity */
text-align: center; /* Center the content */
padding: 60px;
}
.cta-text {
font-size: 24px;
}
.cta-button {
background-color: #FF5733; /* Button background color */
color: #fff; /* White button text */
padding: 10px 20px; /* Button padding */
border: none; /* No border */
border-radius: 5px; /* Rounded corners */
font-size: 18px;
cursor: pointer; /* Show a pointer cursor on hover */
}
</style>
</head>
<body>
<div class="cta-container">
<h2 class="cta-text">Discover our featured product!</h2>
<p class="cta-text">Get 20% off your first purchase.</p>
<button class="cta-button">Shop now</button>
</div>
</body>
</html>
The cta-container uses cta-background.jpg and the following properties:
- background-size: cover scales the image to cover the container.
- background-position: center center centers the image both vertically and horizontally.
- background-repeat: no-repeat prevents the image from tiling.
- background-attachment: fixed anchors the image to the viewport, while the page content scrolls.
- opacity: 0.9 renders the entire container, including the text and button, at 90% opacity.
The cta-text is centered. The cta-button has a colored background, white text, rounded corners, and a pointer cursor on hover.
4.4. Practical example without a fixed background
Compare the second call to action. Without background-attachment: fixed, the image scrolls with the container and the parallax effect disappears.
.hero-image {
background-image: url("https://www.eniun.com/wp-content/uploads/eniun-home-historia.jpg");
height: 400px;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
}
Parallax can add a sense of depth with a small amount of HTML and CSS. Compare both demos on your own browser before deciding which effect fits your project.