Is there a way to position a pseudoelement behind its parent yet still in front of its grandparent using absolute positioning?

Imagine the following scenario:

<div id="one"/>
<div id="two">
   <div id="three"/>
</div>
<div id="four"/>

I am trying to apply a pseudoelement to three that appears behind it but in front of two (and any other ancestors if applicable). The challenge lies in the fact that I am working within a UI framework that automatically generates HTML and CSS, making it impossible to tailor a solution for this specific case alone—it must function universally. Assuming that one, two, and four are absolutely positioned, while three and its pseudoelement may have different position values as long as they act correctly.

Initially, using z-index -1 on the pseudo seemed promising locally, however, it ended up appearing behind two. I attempted to address this by creating a new stacking context on three through setting its z-index to 0, but encountered unexpected behavior. It seems like my understanding of stacking contexts might be flawed.

In case you're curious about the underlying goal, I am striving to incorporate support for rounded gradient borders into the framework.

Answer №1

After reviewing your jsfiddle, it's clear that you can achieve the desired effect by setting a negative z-index value for your pseudo element.

It's important to note that the div#three should maintain the default z-index of auto. Changing it to an integer would establish a new stacking context, making it the base and preventing any child elements from being placed behind it.

The concept of z-index is more complex than many realize. If you'd like to delve deeper into this topic, I recommend reading this informative article.

On a side note, remember that div tags are not self-closing, so always include a proper closing tag.

div {
  width: 100px;
  height: 100px;
  position: absolute;
}

#one {
  top: 0px;
  left: 0px;
  background: red;
}

#two {
  top: 20px;
  left: 20px;
  background: green;
  z-index: 1;
}

#three {
  top: 40px;
  left: 40px;
  background: lightblue;
}

#three::before {
  position: absolute;
  width: 200px;
  height: inherit;
  background: orange;
  content: '3 before';
  top: -20px;
  left: -20px;
  z-index: -1;
}
<div id="one">1</div> 
<div id="two">
  2
  <div id="three">3</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

Utilizing carouFredsel for thumbnails and compatibility with IE7

I've successfully integrated carouFredSel (using the basic example with next/prev and pagination) on Chrome, Firefox, IE9, and IE8. However, when I tested it on IE7, the plugin didn't seem to work correctly. The list just displayed vertically wit ...

Enhance your website's user experience by implementing Bootstrap breadcrumb styling integrated

I am a beginner with bootstrap and currently trying to customize the breadcrumb styling. I attempted the following code which resulted in the desired inline layout. <div class="BreadCrumbs"> <a href="www.yahoo.com" >Yahoo</a&g ...

The overflowing pop-up container

When I trigger a popup that dynamically generates a fixed background with a centered dialog, it works well. However, on smaller monitors like laptops, tablets, and phones, the content can overflow beyond the visible area due to its fixed container, making ...

Is there a way to eliminate the transform style from a draggable element?

I am looking to enhance the CDK drag and drop example by adding a preview of the dragged element with specific left and top positions, without utilizing the transform style. HTML <div class="example-boundary"> <div class="example- ...

Adjust the size of the text within the div element as needed

Here is my code on jsFiddle where I am dynamically changing the text font to fit in the div. Everything is working perfectly fine. Currently, the text is within a span element, but I would like to remove the span and have the same functionality. However, w ...

Expanding the width of the textfield is not possible in CSS styling

My current search bar design features an input area that does not match my expectations. The input length is restricted and starts from the center rather than the black border I had envisioned. If you'd like to see the code and design in action, chec ...

What is the correct way to properly wrap the div component in this code snippet?

I am currently working on implementing a Javascript component that displays pinned locations and related information on a map. I have made some progress with the implementation, but unfortunately, it does not appear correctly in the actual site. There is a ...

Leveraging jQuery to manipulate an SVG file

jQuery is designed to work within HTML pages that contain JavaScript code. While SVG and HTML both use the same DOM Level 2, SVG is XML-based and employs ECMAScript. What potential issues could arise from utilizing jQuery with SVG? Is it more advisable t ...

A Step-by-Step Guide on Importing Components in Vue.js

I have defined the following component as shown below: import Vue from 'vue'; import Datepicker from 'vuejs-datepicker'; Vue.component('DatePicker', { template: ` <datepicker name="date"></datepicker> ` ...

Is it possible to use a shell script to replace the external CSS file link in an HTML file with the actual content of the CSS file

Seeking a solution for replacing external CSS and JS file links in an HTML document with the actual content of these files. The current structure of the HTML file is as follows: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C ...

What is the reason why the show() function in JQuery only applies to one specific element, rather than all elements selected with the same selector?

JSFiddle. This example code features a nested <table> within another <table>. The main issue concerns the click listener for the #add button. Specifically, the final if/else statement in the function. When you execute this code and click the ...

Enhancing bar border with the addition of a separator

Our current situation is similar to this: https://i.stack.imgur.com/Luj1S.png but our goal is to have a chart with separators on both sides of the age brackets, like this: https://i.stack.imgur.com/03UpO.png In order to achieve this, we have utilized t ...

Tips for effective page management

After creating a navbar in my project, I've come to the realization that it requires a component for each page and subpage. This seems redundant especially when dealing with multiple navigation options like shown in this image. Is it necessary to crea ...

Why has my dropdown menu decided to go horizontal, rather than dropping down as it should?

Hi there! I'm new to css and tried following a tutorial with some tweaks. However, when adding a drop down menu using lists, it ended up going sideways instead of downward. Can anyone help me out? I also would like to have a collapsible menu implemen ...

What is the reason for the improper setting of the div position?

Welcome to the interactive Pointer Pin application! The red-bordered box is where you can drop your Pointer Pins. This area has a background image set. The blue-bordered box is where you can drag various Pointer Pins from. These pins can be dropped into ...

creating a new page upon clicking a div element

I need assistance in creating an effect similar to the one found on this website - where clicking on a div opens a new page. ...

Arranging inline-flex elements within a div container

I have been struggling to position two elements side by side within a single div, where the second element should be on the right side. <div className={classes.container}> <div className={classes.first}> First </div> <d ...

Issue with setting a background image to a div in Next.js: not functioning as expected

I haven't used Next.js before and I'm having trouble setting a background image for a specific div. I have a folder named public in the root directory where I've placed the file art-thing.jpg. My styles.css is also in the root directory, whi ...

Looking for help with aligning an icon to the right side of my text

I'm struggling with aligning the icon to the right of the quantity. When I use float:right, it places the icon at the far right of the cell instead of next to the text. Is there a way to get it to be on the right side of the text but directly adjacent ...

Is it possible to refresh a table that is currently displayed in a model popup?

My code to reload a table or div inside a model popup continuously is not working as expected. Can you help me with this issue? I attempted to utilize ajax for this purpose but unfortunately, the code below is not updating chats (I am working on a chat ta ...