CSS tip: A workaround for hiding overflow without affecting 3D transforms is to use "overflow: hidden" in a different way

I'm currently working on creating a horizontal section with a parallax effect. My goal is to have an image in the background that scrolls at a different speed than the rest of the page.

My challenge lies in containing the parallax element within its parent element, essentially using the parent as a mask for the child: ensuring that the child is only visible within the boundaries set by the parent.

While one potential solution involves placing the parallax element between two elements with backgrounds that obstruct it from view, this method does not suit my specific case.

An obvious approach would be to use overflow: hidden on the parent element. However, this breaks the 3D transforms and eliminates the parallax effect altogether.

I'm seeking guidance on how to achieve the desired effect described above.

If you'd like to take a look at the code, here's a link to a Codepen example: https://codepen.io/rradarr/full/mdwgard. Essentially, I want the red rectangle to remain visible only within the "parallax-container" bordered in black.

* {
      margin: 0;
    }
    
    html, body {
      height: 100%
    }
    
    main {
      position: relative;
      width: 100%;
      
      perspective: 1px;
      transform-style: preserve-3d;
      overflow-y: auto;
      overflow-x: hidden;
      height: 100vh;
      
      background-color: blue;
    }
    
    .static {
      min-height: 800px;
    }
    
    .parallax-container {
      border: solid black 3px;
      height: 600px;
      width: 100%;
      transform-style: preserve-3d;
      position: relative;
    }
    
    .parallax-child {
      position: relative;
      width: 100%;
      height: 100%;
      transform: translateZ(-2px) scale(2.01);
      z-index: -1;
    }
    
    #img-or-whatever {
      height: 900px;
      width: 100%;
      background-color: red;
      position: relative;
      z-index: -1;
    }
<main>
      <div class="static"></div>
        
      <div class="parallax-container">
        <div class="parallax-child">
          <div id="img-or-whatever"></div>
        </div>
      </div>
    
      <div class="static"></div>
    </main>

Answer №1

It seems that achieving the desired effect with translateZ is not possible based on current knowledge. This conclusion is supported by information found in an article discussing CSS 3D

...setting overflow to any value other than visible essentially enforces transform-style to be flat, even when preserve-3d has been explicitly set.

The only known workaround for overflow hidden is to

overlay another element over the parallax element
(which you indicated you wish to avoid).

If overlaying an element is not feasible, you may consider implementing a similar solution using JavaScript (such as this example). However, this approach involves extensive calculations and variables, potentially resulting in a loss of 3D effects due to the need for overflow: hidden.

If maintaining 3D effects within the container is essential, a more intricate JavaScript solution omitting overflow: hidden could be devised. Yet, it is advisable to explore alternative options before pursuing this route, like placing an absolute element above the container with overflow: hidden enabled and adjusting its transparency if necessary for 3D effects.

In general, relying on .js for such tasks should be a last resort, though in this particular scenario, limited alternatives may exist.

Answer №2

Have you considered utilizing the :not() pseudo-class? It may be challenging to determine what should go inside the parentheses to achieve a similar effect as excluding something in the black square.

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 annotate the overall count of occurrences of a keyword across multiple models following filtration in Django?

I have a situation where my 'Keyword' model stores keywords for 4 other models, each with a 'key_list' field that is a ManyToManyField pointing back to the 'Keyword' model. The models have multiple keywords, and I am successfu ...

Customize default progress bar in browsers

One feature of my website allows users to update their personal information, profile image, background image, and more. After clicking the submit button, a loading screen appears with a progress percentage displayed at the bottom left corner of the page (i ...

What is causing my navigation bar to display menu items vertically?

I'm diving into the world of Bootstrap and I've created a menu, but it seems like the menu items are stacking vertically on the right side rather than aligning horizontally on the left. Can someone spot where I might have made an error? Here is ...

send css as a parameter to the component

Could it be possible to develop a component with specific CSS attributes? I am aware that the following approach is not valid. <template> <div id="textContainer"> {{ content }} </div> </template> <script> export defa ...

Is there a way to ensure dropdown links in a responsive menu can extend past the menu's height?

Recently, I have been working on creating a CSS responsive main menu by combining code snippets from different sources. Despite my efforts, I encountered a major issue in the form of dropdown menu items disappearing when they extend beyond the normal heigh ...

Is there a way to design a mosaic-style image gallery featuring images of varying sizes in a random and dynamic layout, all without relying

In my quest to create a unique mosaic-style image gallery, I have scoured the internet for suggestions and have come across numerous recommendations for using available plugins. However, I am curious if there is a way for me to personally create a mosaic ...

How to Position Icon on the Right Side of an Input Field using CSS

Is there a way to position the magnifying glass icon to the right side of my input field? Link to my Fiddle. @import url("//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"); body { margin: 30px; } .search { position: relative; ...

Organizing Pictures in a Gridded Design

My dilemma lies in displaying a set of thumbnail images within a div. The width of the div can vary depending on the screen size. Each image is fixed at 150px * 150px with a padding of 5px. I aim to arrange these thumbnails in a grid layout while ensuring ...

Revamp Your Navbar Links with Bootstrap Color Customization

Even though this topic has been discussed extensively, I still haven't found a solution that works for me. My goal is simple - to change the color of the links in my bootstrap navbar to white. I know that the CSS is correctly applied because I can adj ...

Track the cursor's movement

Looking to add an animation effect to my website. I want the navbar to follow the cursor within a limited space when hovered over. Check out this example for reference: . Here's the code I have so far, but it's not quite achieving the desired res ...

How to target the DIV elements that have at least one unordered list (UL) inside using CSS selector or JavaScript

Imagine a hierarchy structured like this: <div class="first_level"> <div class="second_level_1"> <div class="third_level_1"> <div class="fourth_level_1"> <ul><li></li><li></li></ ...

The issue of a malfunctioning Customized Bootstrap Carousel when used with flex and gap is causing

I have created a unique bootstrap carousel where instead of displaying one div, I showcase three (one in the middle and half of each on the sides). The issue arises when I add borders to my divs and attempt to create spacing between them using display: fle ...

How do you position items in the center and lower right corner of a flexbox that takes up the entire

One feature on my website is a flex box that takes up the entire window with a width of 100% and a height of 100vh. Within this flex box, I've centered text and a button, but now I'd like to add a footer to the page. If I simply place the footer ...

The React style background property for CSS styling

Is there a way to apply the css property background inline in react? I attempted to pass the following object without success: let style = { background: `linear-gradient( rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6) ...

Creating a single Vuetify expansion panel: A step-by-step guide

Is there a way to modify the Vuetify expansion panel so that only one panel can be open at a time? Currently, all panels can be closed which is causing issues. I would like the last opened panel to remain open. I also want to prevent closing the currently ...

Newly added rows to the table after filtering remain visible

I am currently using the DataTable API and jQuery to create a table by fetching rows from the server. However, I encounter an issue when I click a button to load additional rows and append them to the end of the table. The problem arises when I try to hide ...

CSS transitions do not function properly in Mozilla browsers

I have been attempting to create a hover animation transition on my website. It seems to work fine in Opera and Chrome, but not in Mozilla Firefox. Can anyone provide guidance on how to fix this issue? Here is the code I am currently using: HTML <a hr ...

Issue with getting Bootstrap sticky-top to function on sidebar column

My goal is to implement a sticky sidebar on the right side of the page. The sidebar menu is contained within a grid column. I have attempted to utilize the sticky-top class, following the guidance provided in this question, but unfortunately, it does not s ...

When the window is resized, the div shifts position

I recently added a div to my website with the following code: <div id="div"></div> #div { height: 400px; width: 300px; position: absolute; right: 59%; text-align: left; } Howe ...

Display initial messages at the bottom of a DIV, with subsequent messages moving upwards

My Chat DIV is designed to send and receive messages. Currently, messages appear at the top of the DIV and move downwards (as shown in red). Once they reach the bottom, they are pushed upwards using the jQuery snippet below: .scrollTop(activeConversation ...