As I scroll, I hope the texts will stay fixed against the backdrop image

Is it possible to make the #home section fixed to the background while scrolling, including all the texts and buttons? I envision it like having texts embedded in an image. The text is located in the middle left of the page (let's keep this as default without any changes).
**
I just want the texts and the button (home__container) to remain fixed in position, so that when scrolled they stay in place while the other sections scroll over them**

<html>
<head>
    <link rel="stylesheet" href="style.css">
    <title>CHANEL Copy Website</title>
</head>
<body>
    <main>
        <section id="home" style="height: 90vh;">
            <div class="home__container">
                <h1>Timeless Elegance</h1>
                <h4>Shop Now</h4>
                <button>Explore</button>
                <button>Shop Collections</button>
            </div>
        </section>
        <section #other-section>
        </section>
    </main>
</body>
</html>
@import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
:root {
    --black: #161413;
    --white: #FFFFFF;
    --dark-gray: #444444;
    --light-gray: #E7E7E7;
}
body {
    font-family: 'Open Sans', sans-serif;
    font-weight: 600;
    color: var(--black);
    background-color: var(--white);
    width: 100%;
}
section {
    padding: 24px 16px;
    display: flex;
    align-items: center;
    width: 100vw;
    max-width: 100%;
}

/* home */
#home {
    height: 90vh;
    align-items: center;
    background-repeat: no-repeat;
    background-size: cover;
    background-image: url('assets/brand-home-bg-image.avif');
    color: var(--white);
    width: 100vw;
    max-width: 100%;
    background-attachment: fixed;
}
.home__container {
    margin-top: 2.5rem;
    margin-left: 1.5rem;
}
.home__container h2 {
    color: #f9f7f4;
}
.home__container button {
    margin-top: 1.5rem;
    margin-right: 1rem;
}

/* Other Section */
#other-section {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
}

I attempted using position fixed, z-index, and even incorporated javascript with gpt, but unfortunately it didn't yield the desired outcome!

Answer №1

element, you can try adjusting the text positions to absolute by using the code position: absolute; and placing it within a div along with an image. Here is an example:
<div>
  <img style="position: absolute;" src="your-image-src">
  <!-- You can insert your text here with the style="position: absolute;" as shown below -->
  <p style="position: absolute;">Your text goes here</p>
</div>

Answer №2

Feel free to test out the following code snippet. It should fulfill your needs.

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --black: #161413;
    --white: #FFFFFF;
    --dark-gray: #444444;
    --light-gray: #E7E7E7;
}

body {
    font-family: 'Open Sans', sans-serif;
    font-weight: 600;
    color: var(--black);
    background-color: var(--white);
    width: 100%;
    margin: 0;
    padding: 0;
}

section {
    padding: 24px 16px;
    display: flex;
    align-items: center;
    width: 100vw;
    max-width: 100%;
}

/* home */
#home {
    height: 90vh;
    align-items: center;
    background-repeat: no-repeat;
    background-size: cover;
    background-image: url('assets/brand-home-bg-image.avif');
    color: var(--white);
    width: 100vw;
    max-width: 100%;
    position: relative;
}

.home__container {
    position: fixed; /* Fix the position */
    margin-top: 2.5rem;
    margin-left: 1.5rem;
    z-index: 1; /* Ensure it's below the other sections */
}

/* Other Section */
#other-section {
    position: relative;
    background: white;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    z-index: 2; /* Ensure it appears above the fixed section */
}
<html>
<head>
    <link rel="stylesheet" href="style.css">
    <title>FASHIONISTA Replica Website</title>
</head>
<body>
    <main>
        <section id="home" style="height: 90vh;">
            <div class="home__container">
                <h1>Classic Beauty</h1>
                <h4>Browse Now</h4>
                <button>Discover</button>
                <button>Shop Collections</button>
            </div>
        </section>
        <section id="other-section">
        Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.
        </section>
    </main>
</body>
</html>

Answer №3

To ensure that your content always appears above other elements, you may want to consider setting the position of .home__container to fixed and increasing the z-index value significantly.

Here is a snippet of code that you can integrate into your current CSS:

.home__container{
     position: fixed;
     top: 50%
     transform: translateY(-50%)
     z-index: 9999;
}

The use of top and transform in this code snippet helps center your content vertically on the screen.

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

Personalize the color scheme for your timeline paper

I am interested in customizing this specific example of a personalized timeline: import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import Timeline from '@material-ui/lab/Timeline'; import Timeli ...

Base64 image failing to display in Internet Explorer

Is there a way to efficiently handle the base 64 form of an image in different browsers? The code I have achieves this as shown below: import base64 import requests img=requests.get('https://example.com/hello.jpg') base=base64.b64encode(img.cont ...

Steps for assigning innerHTML values to elements within a cloned div

Currently, I am setting up a search form and I require dynamically created divs to display the search results. The approach I am taking involves: Creating the necessary HTML elements. Cloning the structure for each result and updating the content of ...

Scrollbar customization feature not functional in Firefox browser

I recently finished developing a website and I have customized the main vertical scrollbar. Unfortunately, I am facing an issue where it works well on Google Chrome and Safari but not on Mozilla Firefox. Does anyone have any suggestions on how to trouble ...

Unable to display Boostrap modal upon clicking href link

Can someone help me troubleshoot why my pop modal is not displaying after a user clicks on a specific link on my webpage? I have attempted the following: <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></s ...

Swapping out bullet points for delicious food icons in an unordered list on an HTML page

I am working with the following code snippet: <div id="instructions"> <h3>Get it done</h3> <ol> <li>In a blender add the eggs, chocolate powder, butter, flour, sugar and milk.</li> <li>Then whisk ...

Require guidance and resolution

<div class="container"> <div class="top-section"> <img id="image1" /> <img id="image2" /> </div> <div class="content"> ... </div> <div class="bottom-section"> ... </div> </div ...

The HTML refresh is dynamically controlled by PHP variables

As a newcomer to php/html, I have a question about updating a page's html based on php variables in the forum. Can a page dynamically update its content using php variables? In my scenario, I have: <form action = "" method = "post"> <label&g ...

Tips for aligning a cluster of floating buttons at the center in Vuetify:

This is the code I am currently working with: <v-container height="0"> <v-row align="center" justify="center"> <v-hover v-slot:default="{ hover }" v-for="(option, index) in options" ...

What is the best way to include CSS styles in a React app with Webpack?

Having an issue with my React app. Upon successfully compiling Webpack and serving the content, no styles are displayed within the app. This is the current content of my webpack.config.js file: const path = require('path'); const BUILD_DIR = pat ...

Items in a pair of distinct columns

I want to enhance a picture by adding three paragraphs on the left-hand side. Below is my HTML code: <md-toolbar layout-align="center center"> <h3>Traffic Light Component</h3> </md-toolbar> <md-grid-list md-cols ...

Why is it that a label tag cannot be placed directly above an input tag?

On this particular page: http://getbootstrap.com/components/#input-groups-buttons If you modify the Go! button to a label (using Chrome inspector), you will observe that the Go! button is no longer positioned on top of the input field: https://i.sstatic ...

What could be causing only certain portions of my CSS to be applied?

I've been struggling to get rid of the gray border around the SIGN UP button in the tbody section. I've tried applying classes (along with the ID) and using pseudo classes like :4th-child {border:none}, but nothing seems to work. The only solutio ...

Dreamweaver constantly combines an external CSS file with locally stored CSS when running on a local server

When I used Dreamweaver CS5, XAMPP Server, and PHP files in the past, I encountered an issue. The websites within my htdocs folder seemed to be pulling a specific website's CSS file. In 'Live View,' I could see all external .css and .js file ...

Ensure images are correctly aligned

Could really use some help with this issue I'm having. It's probably a simple fix for all you experts out there, but I can't seem to align my images properly so they are even and not one higher than the other. Please take a look at the scree ...

Using Bootstrap 5 to beautifully wrap an even quantity of boxes

My challenge is to optimize the spacing between 4 boxes based on screen size: The layout should adjust as follows: If it's xxl or larger, all 4 boxes should be in a single horizontal row. If it's md or larger, two boxes should be in the firs ...

locomotory mesh decentralized sorting

I am attempting to implement in-browser sorting for my flexigrid. Currently, the grid is displaying data from a static XML file exactly how I want it, but the table itself does not sort because it is working off of local data. While researching solutions, ...

Utilize jQuery to alter the presentation in a distinct field

Whenever a checkbox is clicked, it updates a display field. I want to capture the content of this display field and also show it in another display field. Ideally, the second field, which is a total field, would add or subtract the value from the first fie ...

Display button in Django template based on user's group level

In my application, there are 3 groups with different permissions: viewer, editor, and creator. I need to display the appropriate buttons based on these permissions. For viewers, they can only see lists and details. Editors have viewer permissions plus th ...

Having trouble getting custom tabs in jQuery to function properly?

I am facing an issue with a simple script I have created. The script is supposed to display a specific div when a user clicks on a link, and hide all other divs in the container. However, when I click on the link, nothing gets hidden as expected. Even afte ...