Experience smooth, seamless scrolling with CSS parallax effect

I'm working on creating a website where the background scrolls slower than the foreground. After exploring different options, I settled on using CSS parallax for this effect. It's working as intended, but there's one issue - it doesn't scroll automatically and instead creates a scrollbar below my title bar. I'm struggling to figure out how to make it scroll smoothly without the presence of a visible scrollbar. Here's a snippet of the code I've put together so far:

index.html

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <title>Title</title>
        <link rel="icon" type="image/png" href="icon.png">
        <div id="top">
            <img src="icon.png" alt="Icon" 
style="width:150px;height:150px;">
            <nav>
                <a href="index.html">Home</a> &nbsp
                <a href="b.html"/>B</a>
            </nav>
        </div>
    </head>
    <body>
        <div class="parallax">
            <div class="parallax__layer parallax__layer--back">
                <img src="apt.jpg" alt="Apartment">
            </div>
            <div class="parallax__layer parallax__layer--base">
                <h1>Welcome!</h1>
                <p>Sample Text</p>
        </div>
    </div>
</body>

css/style.css

.parallax
{
    -webkit-perspective: 1px;
    perspective: 1px;
    height: 100vh;
    overflow: hidden;
    position: relative;
    z-index: -1;
}
.parallax__layer
{
    position: absolute;
    overflow: auto;
    right: 0;
    left: 0;
}
.parallax__layer--base
{
    top: 150px;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    margin-left: 15%;
    margin-right: 15%;
}
.parallax__layer--back
{
    -webkit-transform: translateZ(-1px);
    transform: translateZ(-1px) scale(2);
}

nav
{
    position: fixed;
    top: 0px;
    z-index: 3200;
    font-size: 40px;
    top: 55px;
    right: 30px;
}

a
{
    text-decoration: none;
    color: red;
}

h1
{
    color: red;
}

p
{
    color: red;
}

div
{
    background-color: 2f2f2f;
}

#top
{
    position: fixed;
    top: 0;
    left: 0;
    height: 150px;
    width: 100%;
    background-color: 3c3c3c;
}

Any suggestions or assistance would be greatly appreciated!

Answer №1

Dealing with my problem turned out to be simpler than I thought. The key is to enclose the elements you want to apply this to in a div and disable overflow within that div. Then, in the child divs where scrolling is needed, re-enable overflow but set the scrollbar width to 0. Additionally, using the parent div can help address positioning issues that may arise.

Here's an example snippet from index.html:

<html>
<head>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <title>Title</title>
    <link rel="icon" type="image/png" href="icon.png">
    <div class="navbar">
        <img src="icon.png" alt="Red Star" style="width:150px;height:150px;">
        <nav>
            <a href="index.html">Home</a> &nbsp
            <a href=b.html/>B</a>
        </nav>
    </div>
</head>
<body>
    <div class="base">
        <div class="parallax">
            <div class="parallax-layer parallax-back">
                <img src="apt.jpg" alt="Apartment">
            </div>
            <div class="parallax-layer parallax-base">
                <h1>Welcome to the website!</h1>
                <p>Sample text!</p>
            </div>
        </div>
    </div>
</body>

.parallax::-webkit-scrollbar { 
    width: 0;
}

.base
{
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    overflow: hidden;
}
.navbar
{
    position: fixed;
    top: 0;
    left: 0;
    height: 150px;
    width: 100%;
    background-color: 3c3c3c;
    z-index: 3200;
}

.parallax
{
    width: 100%;
    -webkit-perspective: 1px;
    perspective: 1px;
    height: 100vh;
    overflow-x: hidden;
    overflow-y: auto;
    z-index: -1;
}
.parallax-layer
{
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
}
.parallax-base
{
    top: 175px;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    margin-left: 15%;
    margin-right: 15%;
}
.parallax-back
{
    width: 100%;
    top: 150px;
    -webkit-transform: translateZ(-3px);
    transform: translateZ(-3px) scale(4);
}

nav
{
    position: fixed;
    top: 0px;
    z-index: 3200;
    font-size: 40px;
    top: 55px;
    right: 30px;
}

a
{
    text-decoration: none;
    color: red;
}

h1
{
    color: red;
}

p
{
    color: red;
}

div
{
    background-color: 2f2f2f;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I assign an attribute to an element that has been dynamically generated?

Upon receiving a response from a $.post, I found that it contains various elements including <a> and <img>. To prevent the <a> element from performing its default actions when clicked, and to disable the draggable option for both the < ...

Displaying information based on selection in AngularJSIn this tutorial

I am a beginner in Angularjs and currently working on developing a website. One of the tasks I have is to create a combo box that, when an option is selected, calls a method to load values into a Scope variable and update it. Below is the code I have so fa ...

What's the reason behind the data not being retrieved by $.ajax?

After attempting to validate a form and send the values using $.ajax, I encountered an error message stating "Undefined index: is_ajax". Why is the form_data not being received? What could be causing this issue and how can it be resolved? function validat ...

Is there a way to manipulate the placement of an image within a div using CSS?

Teaching myself HTML has been quite the journey. Right now, I'm in the process of building a website purely for its aesthetic appeal, and I seem to be hitting a roadblock with the CSS/div element. As of now, it appears like this. When the h1 is remove ...

Using HTML5 and an ASP.NET web method to allow for file uploads

My attempt to upload a file to the server using an HTML5 input control with type="file" is not working as expected. Below is the client-side JavaScript code I am using: var fd = new FormData(); fd.append("fileToUpload", document.getElementById(&ap ...

If the font size exceeds a certain value in SCSS, then apply a different value

Can SCSS handle this scenario? I am looking to set the font size to 22px in case it is greater than 30px, especially within a media query for a max width of 480px. ...

Display the bash script results on an HTML webpage

My bash script fetches device status and packet loss information, slightly adjusted for privacy: #!/bin/bash TSTAMP=$(date +'%Y-%m-%d %H:%M') device1=`ping -c 1 100.1.0.2 | grep packet | awk '{ print $6 " " $7 " " $8 }'` device2=`pin ...

The sticky top feature of the Bootstrap 4 navbar does not function properly when it is nested inside a div

I am just getting started as a beginner working on a Web application and utilizing Bootstrap 4 for my web design. I have a quick question that needs some clarification. When the code is structured like this, the Navbar does not stay fixed at the top: < ...

Styling for main banner image on large desktop screens and mobile devices

I need help with website design and CSS solutions. Currently, I have a WordPress website with a onepager theme. At the top of the page, there is an image that almost fills the screen along with a title. My concern is how this image behaves with different b ...

A method for utilizing wildcards in conjunction with the .load function

Currently, I am utilizing jquery, jquery mobile, js, html, and css within Phonegap to develop my android/ios application. However, my understanding of js & jquery is somewhat limited. In my index.html file, I use .load to load other html files into s ...

What is the best method to align these images in the center of this div?

Struggling to center the images inside the div...any suggestions? I know it's basic, just getting back into it. Hopefully someone understands what I'm trying to achieve here? Tryin' to get those images centered in the div but can't fig ...

Need help resolving an issue with an HTML header that's overlapping in JavaScript?

Hey there! I was working on implementing a dynamic header that resizes as you scroll on the page. I also decided to try out Headroom as an additional solution, and it seems to be functioning well in terms of hiding the header. You can check out my progress ...

Creating a Multi-Step Checkout System for a WooCommerce Store

Struggling to make my code work - it just won't fire up. I suspect there might be a version-related issue that I'm overlooking. I'm currently using the default checkout setup from the WooCommerce plugin. Instead of buttons, text is showing ...

There are two separate CSS files linked to the same page, but the browser is only rendering the styles from

I am in the process of developing a new web page where I want to implement my own CSS and incorporate some components from this source: . I'm facing an issue with one of the components, as the browser seems to be applying only the styles from my custo ...

Unusual Actions from the Text Field

Please take a look at this link <div id="textarea_wrapper"> <textarea>How and where is my width derived from?</textarea> </div> #textarea_wrapper{ height: 250px; border:thick solid green; } textarea{ background-color: #930; ...

Distributing elements evenly across the parent width using Angular FlexLayout for a responsive design

I need to create 3 div squares: div-left, div-middle, and div-right, each with a width of 300px. These squares should be arranged horizontally within the parent div main-section, which will adjust its size based on the screen width, being 1300px if the scr ...

Switch to display only when selected

When I click on the details link, it will show all information. However, what I actually want is for it to toggle only the specific detail that I clicked on. Check out this example fiddle Here is the JavaScript code: $('.listt ul').hide(); $( ...

Is It Possible for an HTML Page to Load Within an iframe?

Recently, I embedded an iframe link from a game hosted on Scirra onto my website. However, I noticed that the page automatically scrolls down to where the iframe is positioned once opened, which is quite annoying. Does anyone know how to resolve this iss ...

Restricting the amount of text within a span element

Can someone explain how to limit the display of characters in a tag using CSS, similar to the way YouTube truncates titles? I've searched through the styles but couldn't figure it out. Maybe they used JavaScript? Thanks! ...

Best practice for dynamically adding/storing/creating HTML elements on the fly

What is the best approach to dynamically add and store HTML controls for dynamically added elements on an HTML page? Currently, I am developing a widget-based system where users can choose which widgets they want to display. They have the flexibility to s ...