Utilizing Z-Index with Fixed Positioning and Transitions in CSS

Currently in the process of setting up a fresh portfolio website and utilizing the Onepage Scroll Plugin created by Pete R.

Recently, I included a fixed navigation bar and now aiming to have elements within a slide overlapping this navigation. Here's a preview example on codepen:

http://codepen.io/terrorpixel/pen/BNxYxq

HTML

<nav></nav>
<div class="container">
  <div>Bring me to the front!</div>
</div>

CSS

nav {
    height: 82px;
    left: 0;
    position: fixed;
    top: 0;
    -webkit-transition: background 1.15s ease-in-out 0s;
    -moz-transition: background 1.15s ease-in-out 0s;
    transition: background 1.15s ease-in-out 0s;
    width: 100%;
    z-index: 2;  
    background:rgba(0,255,0,.85);
}

.container {
    background:blue;
    position: relative;
    -webkit-transform: translate3d(0px, -1%, 0px);
    -moz-transform: translate3d(0px, -1%, 0px);
    transform: translate3d(0px, -1%, 0px);
    -webkit-transition: all 2s ease 0s;
    -moz-transition: all 2ms ease 0s;
    transition: all 2ms ease 0s;
    height: 5000px; 
    z-index:1;
    width: 70%;
    margin: 0 auto;
}

.container div {
    padding: 250px 100px;
    z-index:10;
    position:absolute;
    right:0; 
    top:0;
    background:red;
}

The main issue is trying to bring the red box to the forefront. It seems like the problem lies in using z-index within different stacking contexts. The z-index for the div inside the .container did not work either. Is there a way to achieve that effect? :/?

Answer №1

To achieve the desired outcome, it is necessary to relocate .container div outside of .container. When nesting positioned elements, a new stacking order for the children is initiated based on the parent's context. This means that even if a child element is assigned a z-index value of 10000 within a parent with a z-index of 2, it will essentially behave as if it has a z-index of 2.10000.

Here's a simplified example to illustrate this concept:

nav {
  height: 82px;
  left: 0;
  right: 0;
  position: fixed;
  top: 0;
  -webkit-transition: background 1.15s ease-in-out 0s;
  -moz-transition: background 1.15s ease-in-out 0s;
  transition: background 1.15s ease-in-out 0s;
  z-index: 2;
  background: rgba(0, 255, 0, .85);
}
.container {
  background: blue;
  position: relative;
  -webkit-transform: translate3d(0px, -1%, 0px);
  -moz-transform: translate3d(0px, -1%, 0px);
  transform: translate3d(0px, -1%, 0px);
  -webkit-transition: all 2s ease 0s;
  -moz-transition: all 2ms ease 0s;
  transition: all 2ms ease 0s;
  height: 5000px;
  z-index: 1;
  width: 70%;
  margin: 0 auto;
}
.front {
  z-index: 3;
  position: absolute;
  right: 15%; /* half of 30% (the left over of 70% container width) */
  top: 82px;
  background: red;
}
<nav></nav>
<div class="container">
</div>
<div class="front">Bring me to the front!</div>
</div>

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

Upon refreshing the page in Javascript, the element mysteriously vanishes without a trace

After implementing a function that should display "barProgress25" and hide "barProgress0" when a button is clicked, I encountered an issue. Upon clicking the button, the function works as intended but upon page refresh, "barProgres ...

Place the HTML images in the center of the page

I would like the two images to be centered on the page. Please refer to this visual representation for guidance: Here is the code snippet: * { box-sizing: border-box; } .column { float: left; width: 28%; height: 28%; padding: 5px; } /* Cle ...

Creating lasting placeholder text with gaps between each word

Looking to create a unique text input with static content on the right and editable text on the left https://i.stack.imgur.com/K0YfM.png Any suggestions? ...

Adjust the width of the element to match the size of the image within

My webpage features an image along with some accompanying metadata that I want to be centered on the page. I want the div holding the metadata to be the same width as the image. Here's an example: Unfortunately, I am unable to determine the image&apo ...

The border width of the div is set to 2 pixels, but upon inspection it appears to be 1

Greetings! I have created the following code snippet: <!DOCTYPE html> <html> <head> <title>Padding</title> <style type="text/css"> #main { width: 400px; height: 400px; border: 2px blac ...

Text fades into view from the bottom after a line break using CSS and JavaScript

I'm looking to create a unique fade reveal text effect for a multi-line H1. The goal is for each line of the text to fade up from its own row, rather than simply revealing from the bottom. Here's an example I've already developed, but it cu ...

Issues with CSS hover event causing animation to fail to trigger

I have a container that contains an image holder and a text holder. I am able to set it so that when hovered over, the image scales and the same effect applies to the text. However, when attempting to set up the image holder hover to scale the image and ...

Tips for enlarging an image element without causing the header to expand

I'm looking to enhance a logo image within a header element by expanding it on hover or creating a pulse effect. I thought of increasing the max-width and adding a transition for simplicity. However, my main concern is how to make only the img element ...

Unable to alter or eliminate the grey background using material-ui

Struggling with a React application that incorporates material-ui. Despite my efforts, I can't seem to get rid of an unwanted grey background in one section of the app. Attempted solutions: body { background-color: #ffffff !important; } * { b ...

Changing the height of SimpleBar in a component without using global styles in Angular

Is there a way to customize the height of my scrollbar without affecting other components? I tried using simplebar-scrollbar :: before in global style but it impacted other areas as well. I'm looking for a solution that only applies to my component. ...

Boost the dimensions of the content property within a CSS class

I am currently using the content property to display a dashed arrow "-->" in my CSS class. However, the arrow appears very small and I would like to increase its size directly in the CSS class itself. This arrow is meant to be displayed before each elem ...

Floating above a divided rectangle div

Imagine a rectangular div divided into two parts, each part forming an L shape (as illustrated below), with region 1 representing the left-side L and region 2 representing the right-side L. I am interested in changing the background color of region 1 on h ...

Adjusting the height of the v-select component in Vuetify 3: A step-by-step guide

Is there a new method to set the height of the "v-select" component in Vuetify version 3? In previous versions, the 'height' prop was used in this way: <v-select :items="..." height="30" /> However, this prop has been ...

My navigation bar's CSS code falls short of extending across the full width of my screen, leaving a gap of approximately 10 pixels before reaching the edges

Can someone help me figure out what went wrong? I've been trying to remove the margins but it's not working. The background colors of other elements seem to be seeping into this empty space. Any thoughts or suggestions would be greatly appreciate ...

Place the second division beneath the first within a single navigation bar that adjusts accordingly to all screen sizes

I am experiencing an issue with the layout of my page that has 2 divs within one nav element. When the screen width is greater than 1024px, everything looks fine. However, when I reduce the width to less than 768px, the two divs merge into one line instead ...

What is the best way to incorporate a floating label into an input field?

My goal is to create a form where the label of the input field moves up when the user starts typing, and stays up if they enter any text. However, if the user leaves the input area blank, the label should revert back to its original position. I have tried ...

Ways to align an image at the center within a Span

How can I horizontally center an image within a <span> element? https://i.sstatic.net/40IL0.png <div class="col-1"> <span class="dot"> <h:graphicImage library="imgs" name="campus.png" width="25" height="25" style="ali ...

How to make the slides in a Bootstrap 4 carousel slide in and out with animation

I'm currently utilizing the bootstrap 4 carousel and have made some customizations to fit my project needs. The main requirement is: When I click on the next slide, the current next slide should become active and a new next slide should load with ...

Display an image with only a portion visible, no canvas, no frame, and no need

My dilemma involves an img with a surrounding box div. http://jsfiddle.net/6d4yC/7/ 1) I am seeking to display only a portion of the image (250x150) without generating a white overlay when it is in its large size. Placing a #box1 div around the image has ...

What steps should I follow to align these unordered lists side by side?

Is there a way to align these lists (unordered lists inside list items 1 through 4) side by side using CSS3? I've tried different methods, but I can't seem to figure it out. Here is the code I've been using: <footer> <ul> ...