Why is the object appearing on top of the div even though it's supposed to be behind

I'm currently developing a React web application and adding a feature that involves creating a "blob" following the mouse cursor. To achieve this effect, I decided to blur the blob by layering another div on top of it and using backdrop-filter. However, I encountered an issue where the backdrop-filter was also blurring other elements when placing another div over the blob.

Here is the code snippet for the Blob component in ReactJS:

import { useEffect } from "react";

const Blob = () => {
    useEffect(() => {
        const myBlob = document.getElementById("blob");

        if(myBlob) {
            document.body.onpointermove = event => {
                const {clientX, clientY} = event;
        
                myBlob.animate({
                    left: `${clientX - 150}px`,
                    top: `${clientY - 100}px`
                }, {duration: 3000, fill: "forwards"});
            }
        }
    }, [])
    
    return (
        <div>
            <div id="blob" className="blob"></div>
            <div id="blur" className="blur"></div>
        </div>
    );
}

export default Blob;

Below is the code snippet for the main page in ReactJS:

import Blob from "../atoms/trinkets/Blob";
const Home = () => {
    return (
        <div className="return-container">
            <Blob/>
            <div className="block"></div>
        </div>
    )
}

export default Home;

And here is the CSS code applied to both components:

.blob {
    background: linear-gradient(to right, aquamarine, mediumpurple);
    height: 200px;
    width: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    border-radius: 50%;
    animation: rotate360 20s infinite;
}

.blur {
    height: 100%;
    position: absolute;
    width: 100%;
    z-index: 2;
    backdrop-filter: blur(75px);
}

@keyframes rotate360 {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

.block {
    height: 500px;
    width: 500px;
    background-color: pink;
    z-index: 5;
}

The problem lies in the #blur div, which is causing the backdrop-filter to affect everything else. Despite trying various solutions, I couldn't get the .block div to appear above all other elements even after inspecting with Dev Tools.

Answer №1

When it comes to layers in CSS, #block is not at the top, but actually at the bottom.

Here is the order of layers you have:

  1. #blur (Top Layer)
  2. #blob
  3. #block (Bottom Layer)

This means that div#blur is above everything because it follows div#blob in the Document Object Model (DOM). Since both elements have the same position:absolute, the last element will override the first one (div#blur overrides div#blob).

Your #blur has a z-index: 2, which is unnecessary because it will already be above #blob due to its position in the DOM hierarchy.

div#block does not have a position:absolute, placing it at the bottom layer below div#blur and div#blob.

Since #block lacks a position, it should not have a z-index property.

If you want to learn more about CSS positioning, I recommend checking out a video or article on the subject like this one from FreeCodeCamp Article.

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

Why is this.props.onPress not functioning? The reference to `this.props.onPress(e)` resolves to "this.page1"

I'm a beginner in react native and I'm looking to set the destination page within my const. (I am using router flux) Could someone please explain how to do this correctly as I keep getting an error stating that "this.props.onPress is not a functi ...

Assistance with CSS: How to Remove a Shape

Is it possible to remove the triangles above the element labels using CSS? To view them, click on a link (hear, see, savor). There is a dotted line and text with a small red triangle/arrow that is not centered. I have managed to disable them for the menus, ...

Is there a way to limit MuiPhoneNumber to only accept one specific country code?

Is there a way to limit the selection to just one country code, preventing users from choosing other countries? <MuiPhoneNumber fullWidth label="Phone Number" data-cy="user-phone" defaultCountry={"us" ...

Position the text to the right of an image

Upon close observation of the page , you may notice that the 5th wine, AMARONE, has text wrapping around and under the bottle image. Is there a method to prevent this from occurring and instead have all the text on the right side without needing to create ...

Looking to integrate Symfony, ReactJS, and WordPress simultaneously?

Our project involved integrating a Symfony app and a WordPress blog. This setup includes installing WordPress as a subdirectory named /blog within the /web folder of Symfony. To showcase our work, we created a temporary coming soon page at www.example.com ...

Is it possible to preload getServerSideProps data in Next.js?

I have a straightforward React component: const Page = ({ data }) => { return ( <header> {data.length !== 0 ? <> {data((d) => // render data )} </> ...

How to Toggle the :invalid State to True on a Dropdown Element Using JQuery

$("#select_id").is(':invalid') false Is there a way to change it to true? $("#select_id").addClass(':invalid'); $("#select_id").prop('invalid',true) Unfortunately, this method does not seem t ...

Setting the data type for a React Stateless Functional Component (SFC) in TypeScript

By assigning a type of React.FC<PropsType> to a variable, it becomes recognized as a React Stateless Functional Component. Here's an example: //Interface declaration interface ButtonProps { color: string, text: string, disabled?: boolean ...

The function .click() fails to execute upon page load, yet it functions successfully when utilized in the console

This is the code I am using: <script> document.getElementById("test").click(); </script> When I try to trigger the click(); function on page load, it doesn't work. However, when I execute it in the console on Firefox, it works ...

When the collapse button is clicked, I aim to increase the top value by 100. Subsequently, upon a second click, the top value should

https://i.stack.imgur.com/p9owU.jpghttps://i.stack.imgur.com/Kwtnh.jpg I attempted to utilize jQuery toggle, but unfortunately, it is not functioning as expected. <script> $(document).ready(function () { $(".sidebar ul li&quo ...

Capture audio from Google Meet using Puppeteer

Currently, my goal is to capture a Google Meet session using puppeteer. I have successfully managed to join the meeting as a bot and record the video. Unfortunately, I have encountered a hurdle when trying to record the audio. Despite experimenting with va ...

Encountering an issue such as "Exceeding the maximum number of re-renders. React restricts the amount of renders to avoid

Currently, I am attempting to update the state within the request data method for a string variable as shown below: const ViewChangeRequest = () => { const [requestStageValue, setRequestStage] = useState(''); const { data: request ...

Ways to retrieve the bounding rectangle of an element PRIOR to applying a transformation?

Is there a way to obtain the dimensions and position of an element using Element.getBoundingClientRect(), but without taking into consideration any transformations applied through the CSS property transform? I am looking for a method that provides the rect ...

Creating custom shortcuts with Storybook possible?

I have been attempting to create aliases for the storybook, but despite searching for a solution on the website, I still can't seem to get it to work. The existing aliases in my original webpack.config.js file are not being cached, and I'm encoun ...

What is the best way to remove a particular item based on its id using React and the fetch method?

Hi there, I've been working on this code and everything seems to be functioning well except for one thing. I am having trouble deleting items in my database using the fetch method. The objective is to retrieve the id of the currently clicked item and ...

What is causing my h2 and div elements to share the same font size?

I've been working on converting an element. The font-size conversion seems to be successful, but the margin conversion is not working as expected. Here is my code attempt: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...

How can I modify the navbar-collapse in Bootstrap to alter the background color of the entire navbar when it is shown or open on a mobile screen?

Can anyone tell me how to change the background color of the navbar when the navbar-collapse is displayed? I want one background color when the navbar-collapse is shown and a different color when it's collapsed. Is there a way to achieve this using C ...

Tips for adjusting the color of a single ion-item within an ion-list after being clicked

I've been struggling to find a solution for this issue. I have an ion-list with several ion-items generated from an array using *ngFor. My goal is to change the color of only the clicked item, but so far, all items are changing color on click. Here&a ...

Issues regarding aligning content vertically in flexbox

I can't seem to figure out this error, even though it seems simple. .box { display: flex; align-items: center; justify-content: center; } <div class="box"> <div><img src="https://via.placeholder.com/150x150" alt=""></di ...

What are the limitations of using useState with complex nested objects and arrays in React components?

In my scenario, I am working with an array of characters. Each character contains multiple builds, and each build includes a string for weapons and a string for artifacts. I am developing a tool to extract specific portions of these strings and assign them ...