How To Create a Parallax Scrolling Effect
THE WORLD'S LARGEST WEB DEVELOPER SITE
×

HOW TO

HowTo Home

Menus

Icon Bar Menu Icon Accordion Tabs Vertical Tabs Tab Headers Full Page Tabs Top Navigation Responsive Topnav Search Bar Fixed Sidebar Side Navigation Fullscreen Navigation Off-Canvas Menu Hover Sidenav Buttons Horizontal Scroll Menu Vertical Menu Bottom Navigation Responsive Bottom Nav Bottom Border Nav Links Right Aligned Menu Links Centered Menu Links Fixed Menu Slide Down Bar on Scroll Hide Navbar on Scroll Sticky Navbar Hover Dropdowns Click Dropdowns Dropdown in Topnav Dropdown in Sidenav Resp Navbar Dropdown Dropup Mega Menu Pagination Breadcrumbs Button Group Vertical Button Group Sticky Social Bar Responsive Header

Images

Slideshow Slideshow Gallery Modal Images Lightbox Responsive Image Grid Image Grid Tab Gallery Image Overlay Fade Image Overlay Slide Image Overlay Zoom Image Overlay Title Image Overlay Icon Image Effects Black and White Image Image Text Image Text Blocks Transparent Image Text Full Page Image Form on Image Hero Image Side-by-Side Images Rounded Images Avatar Images Responsive Images Center Images Thumbnails Meet the Team Sticky Image Flip an Image Shake an Image Portfolio Gallery Portfolio with Filtering Image Zoom Image Magnifier Glass Image Comparison Slider

Buttons

Alert Buttons Outline Buttons Split Buttons Animated Buttons Fading Buttons Button on Image Social Media Buttons Loading Buttons Download Buttons Icon Buttons Next/prev Buttons Block Buttons Text Buttons Round Buttons Scroll To Top Button

Forms

Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Responsive Form Clear Input Field Copy Text to Clipboard Animated Search Search Button Fullscreen Search Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Password Validation Toggle Password Visibility Multiple Step Form Autocomplete

Filters

Filter List Filter Table Filter Elements Filter Dropdown Sort List Sort Table

Tables

Zebra Striped Table Responsive Tables Comparison Table

More

Fullscreen Video Modal Boxes Timeline Scroll Indicator Progress Bars Skill Bar Range Sliders Tooltips Popups Collapsible Calendar HTML Includes To Do List Loaders Star Rating User Rating Overlay Effect Contact Chips Cards Profile Card Alerts Notes Labels Circles Coupon Responsive Text Fixed Footer Sticky Element Equal Height Clearfix Snackbar Scroll Drawing Sticky Header Pricing Table Parallax Aspect Ratio Toggle Like/Dislike Toggle Hide/Show Toggle Text Toggle Class Add Class Remove Class Active Class Zoom Hover Transition on Hover Arrows Shapes Browser Window Custom Scrollbar Device Look Placeholder Color Vertical Line Animate Icons Countdown Timer Typewriter Coming Soon Page Chat Messages Split Screen Testimonials Quotes Slideshow Closable List Items Typical Device Breakpoints Draggable HTML Element Trigger Button on Enter JS Media Queries Syntax Highlighter JS Animations Get Iframe Elements

Website

Make a Website Make a Website (W3.CSS) Make a Website (BS3) Make a Website (BS4) Center Website Contact Section

Grid

2 Column Layout 3 Column Layout 4 Column Layout Expanding Grid List Grid View Mixed Column Layout Blog Layout

Google

Google Maps Google Maps BW Google Translate Google Charts Google Fonts

Converters

Convert Weight Convert Temperature Convert Length Convert Speed

How TO - Parallax Scrolling


Learn how to create a "parallax" scrolling effect with CSS.


Parallax

Parallax scrolling is a web site trend where the background content (i.e. an image) is moved at a different speed than the foreground content while scrolling. Click on the links below to see the difference between a website with and without parallax scrolling.

Demo with parallax scrolling

Demo without parallax scrolling

Note: Parallax scrolling does not always work on mobile devices/smart phones. However, you can use media queries to turn off the effect on mobile devices (see last example on this page).


How To Create a Parallax Scrolling Effect

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. The other background properties are used to center and scale the image perfectly:

Example with pixels

<style>
.parallax {
    /* The image used */
    background-image: url("img_parallax.jpg");

    /* Set a specific height */
    height: 500px;

    /* Create the parallax scrolling effect */
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}
</style>

<!-- Container element -->
<div class="parallax"></div>
Try it Yourself »

The example above used pixels to set the height of the image. If you want to use percent, for example 100%, to make the image fit the whole screen, set the height of the parallax container to 100%. Note: You must also apply height: 100% to both <html> and <body>:

Example with percent

body, html {
    height: 100%;
}

.parallax {
    /* The image used */
    background-image: url("img_parallax.jpg");

    /* Full height */
    height: 100%;

    /* Create the parallax scrolling effect */
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}
Try it Yourself »

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

Example

/* Turn off parallax scrolling for all tablets and phones. Increase/decrease the pixels if needed */
@media only screen and (max-device-width: 1366px) {
    .parallax {
        background-attachment: scroll;
    }
}
Try it Yourself »