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

Once the page is refreshed, the checkbox should remain in its current state and

I have a challenge with disabling all checkboxes on my page using Angular-Js and JQuery. After clicking on a checkbox, I want to disable all checkboxes but preserve their state after reloading the page. Here is an example of the code snippet: $('# ...

Header Blocks that are Fixed While Scrolling in fullPage.js Sections Using the "scrollOverflow: true" Feature

I am experiencing an issue where a fixed header on my website prevents scrolling through sections when hovering, specifically when the setting scrollOverflow: true is enabled. However, everything works fine and scrolling through all sections is possible w ...

"Enhance your website with a dynamic hover effect and striking letter spacing in

In the div below, there is a span with the following code: <div class="mission-button"><span class="mission">view our mission</span></div> Accompanied by this CSS: .mission-button::before { content:'&ap ...

What is the best method for extracting [object Text] using XPath?

I'm currently working with Selenium and attempting to extract text from a node that contains the keyword "approved," but I'm unsure of how to retrieve and validate this specific text. Here is the node structure - <div class="Notification-bo ...

Sending Data via Ajax

I am currently working on implementing an ajax invitation script that allows users to invite their friends to an event. The javascript code I have used in other parts of the website works perfectly, but for some reason, it is not functioning correctly in t ...

"Looking to spice up your website with a dynamic background-image

I've encountered a problem with my CSS code for the header's background image. Despite trying various methods to create a slideshow, nothing seems to be working. Can someone provide guidance? I have four banner images named banner1, banner2, bann ...

Boxes that expand to full width and appear to float on

I am dealing with a situation where I have a container that holds multiple child elements such as boxes or sections. <section> <div>Box 1</div> <div>Box 2</div> <div>Box 3</div> <div>Box 4< ...

Prevent the box from closing automatically after submitting a form using jQuery

Despite my efforts to keep a simple dialog box open after submitting a form, it continues to close. I tried using the preventDefault method in jQuery, but it didn't solve the issue. Below is the code snippet: $(document).ready(function(e) { $(&apo ...

Refresh PHP Calculator Display Once Results are Shown

Currently working on a basic calculator project coded in HTML, CSS, JS, and PHP. Here is a glimpse of it: The user input retrieval is handled by JS, while the actual calculations and result display are taken care of by PHP. I am striving to clear out the ...

What is the best way to transfer information from node.js to HTML?

This is a sample code in Node.js var http = require('http'); var request = require("request"); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('Hello World!& ...

ng-grid defines different cellClass based on the value of the field

I am currently using the ng-grid and I want the column to display in a different color based on different values. I have tried, but not succeeded so far... $scope.gridOptions = { ........ columnDefs: [ { field: "status", displayName: "St ...

Learn how to create a horizontally scrollable ToggleButton using MUI when the ToggleButtonGroup exceeds the screen width

Looking to enhance the responsiveness of my web design project, I aim to create a horizontally scrollable ToggleButton using Mui. The goal is to have the buttons adjust when the screen width becomes too narrow to display all three buttons at once, allowing ...

Exploring a new method for AJAX loading when handling large amounts of data into multiple div elements

As I continue my learning journey with html and jquery, I have been exploring ways to replicate monitoring systems (SCADA) into web-based systems. During this process, I discovered openseadragon as a MAP system similar to google maps that allows for overla ...

The background on my modal remains unchanged

I have integrated a modal using Iframe for user login/registration on my website. Here is the HTML code: <div class="modal" id="ays-popin-connexion" aria-hidden="true"> <div class="modal-body" aria-hidden="true"> <iframe src=" ...

Achieving the ideal positioning of content within the flex property

I am facing an issue with my containers, specifically .grid.left and .grid.right. The images within each of these grids are dynamic and enclosed within another div called .gallery. The problem arises when I apply margins to the image boxes, causing gaps w ...

Unable to remove data once it exceeds 10 entries while using AJAX, jQuery, and JavaScript/PHP

I am having an issue where I can insert data into the first 10 rows and delete any of them successfully. However, when I try to insert data starting from the 11th row, I am unable to delete any rows past the 10th. UPDATE: The deletion function does not wo ...

What is the process for retrieving my dates from local storage and displaying them without the time?

Having an event form, I collect information and store it in local storage without using an API. However, I face a challenge when extracting the startdate and enddate out of localstorage and formatting them as dd-mm-yyyy instead of yyyy-mm-ddT00:00:00.000Z. ...

display the information stored within the sports attribute using React

I am attempting to display the values stored in the sports property. So, I inserted a console log within the sports property. However, an error is being thrown: Syntax error: C:/codebase/src/containers/sports.js: Unexpected token, expec ...

The bottom section of the main page

I've successfully implemented a footer that sticks to the bottom of each page as intended. However, I'm facing an issue when there's a large amount of text on a particular page that requires scrolling. The scroll continues beyond the footer, ...

issues with the styling and scripts within the jsp document

During my project work, I encountered an issue with the front end as shown in the image below. Upon loading the project, all styles and scripts disappear completely. How can I resolve this issue promptly? I attempted to update the jQuery version by replac ...