CSS :before makes an appearance followed by :after

I'm having an issue with displaying a box and background on the screen. The box is set to position fixed, along with the background. Even though the z-index values are configured correctly, the background always covers the box. What could be causing this problem?

.box {
  position: fixed;
  width: 100px;
  height: 100px;
  top: 100px;
  left: 100px;
  background-color: yellow;
  border: 10px solid green;
  z-index: 1;
 }
 .box:before {
  content: '';
  position: fixed;
  width: 300px;
  height: 300px;
  top: 150px;
  left: 150px;
  background-color: tomato;
  z-index: -1;
 }
<div class="box"></div>

Answer №1

When utilizing position:fixed and positioning your :before element 150px from the top and 150px from the left, it is expected that it will appear 'after' your .box element. This is because the .box is 100px wide and 100px tall, positioned 100px from the left and 100px from the top, also with a fixed position.

If you switch to using position:absolute on the :after element, it will be positioned relative to its parent div. Here's an example:

.box {
  position: fixed;
  width: 100px;
  height: 100px;
  top: 100px;
  left: 100px;
  background-color: yellow;
  border: 10px solid green;
  z-index: 1;
 }
 .box:before {
  content: '';
  position: absolute;
  width: 100px;
  height: 100px;
  top: 0px;
  left: -110px;
  background-color: tomato;
  z-index: -1;
 }
<div class="box"></div>

Edit: In response to Amaury Hanser's comment, I have included a second snippet for positioning the :before "below" the .box, in terms of z-index:

.box:before {
  content: '';
  position: fixed;
  width: 100px;
  height: 100px;
  top: 100px;
  left: 100px;
  background-color: yellow;
  border: 10px solid green;
  z-index: 1;
 }
 .box:after {
  content: '';
  position: absolute;
  width: 300px;
  height: 300px;
  top: 150px;
  left: 150px;
  background-color: tomato;
  z-index: 0;
 }
<div class="box"></div>

Simply put, consider pseudo-elements as similar to child/parent elements. A child element cannot have a z-index lower than the parent unless the parent has no z-index assigned. Additionally, certain position CSS rules have default z-index values, preventing children from going behind their parents.

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

The slider's border-radius is not being maintained as it moves from left to right

We recently implemented a slider on our website, located at . While we added a border-radius to the slider, we noticed that when the images slide, the border-radius is slightly removed... I attempted to contain the parent element with overflow: hidden, bu ...

Interactive input field designed for modifying, adding, and removing data in MySQL

I am working on a project where I have a simple form. However, I need to dynamically change the form action from insert to update within the same page. Additionally, I also want to display the values on the same page. Within my code, I have set up the up ...

Automatically adjust padding in nested lists with ReactJS and MaterialUI v1

How can I automatically add padding to nested lists that may vary in depth due to recursion? Currently, my output looks like this: https://i.stack.imgur.com/6anY9.png: However, I would like it to look like this instead: https://i.stack.imgur.com/dgSPB. ...

Is it necessary to incorporate iOS default controls into my HTML code?

Currently in the process of developing a cordova app, I am inclined to steer clear of default popups like the spinner triggered when selecting an item <select></select> (I plan on crafting my own dropdown tailored for mobile use). Additionally, ...

Initial loading size of Google chart

After creating a Google chart, I noticed that on the initial load, the chart appears tiny. Although it becomes responsive when adjusting the screen size, I would prefer it to be responsive from the start. Please note that there is a dropdown menu for sele ...

To collapse a div in an HTML Angular environment, the button must be clicked twice

A series of divs in my code are currently grouped together with expand and collapse functionality. It works well, except for the fact that I have to click a button twice in order to open another div. Initially, the first click only collapses the first div. ...

I seem to be encountering a z-index dilemma

Is there a way to make my slogan float above an image on a wide screen? I've tried adjusting the z-index without success. You can find the site here: Here is the CSS code: The #Slogan contains the words: Fitness Bike Guides - Top Models - Discount ...

Using query strings to manipulate the URL of an iframe and add additional information

I am facing a challenge with integrating my legacy perl application into an MVC application. I have set up a structure where the legacy application is loaded into an iframe within default.aspx, but I need to capture the URL of each page within the iframe a ...

Organizing components within a division

It's maddening... I'm working on a website and trying to create a header div that includes the site name, search bar, and sign in/log in buttons. I want the logo on the left, search bar in the center, and sign in/log in buttons on the right, sta ...

tips for getting the sidebar to close immediately after it has been clicked

My understanding of how the menu sidebar works is a bit unclear, but here's my issue. I want the menu sidebar to close automatically when a user clicks on a menu link item or anywhere on the body. Then, it should reopen when the menu icon is pressed. ...

Setting a default currency value (either in dollars or euros) in an input field

Is there a way to automatically insert a currency symbol (€, $) in a text field by default when entering a number? Are there any default values for this feature? Thank you Here is an example ...

change the css back to its original state when a key is pressed

Is there a way to retrieve the original CSS of an element that changes on hover without rewriting it all? Below is my code: $(document).keydown(function(e) { if (e.keyCode == 27) { $(NBSmegamenu).css('display', 'none');} ...

Tips for disabling automatic sliding in Bootstrap 5 carousel specifically on larger screens

I am working with a Bootstrap 5 carousel that I need to configure to stop autosliding on large screens, but maintain autoslide functionality on mobile devices. Is there a way to achieve this using only HTML and CSS/SCSS? I am aware of the data-bs-interval ...

Tips for optimizing the placement of div elements for top advertisements on Amazon's website

I'm looking to overlay some divs on top of Amazon.com ads, similar to the image above. This is part of a personal project where I need to position these divs precisely over the ads. To achieve this effect, I've been using getBoundingClientRect t ...

Get the audio file to start downloading by clicking an HTML anchor link, without relying on the browser's

In my current project, I have an anchor link that directs to an MP3 file. My goal is to allow users to download this file by either right-clicking and selecting "Save Target As..." or by simply clicking on the link. However, I have encountered a problem w ...

What is the significance of vendor prefixes in the world of CSS3?

While I can see the rationale behind using prefixes for experimental non-official features to avoid conflicts, what is the reason for using them on shadows and similar elements? Shouldn't all vendors be implementing effects consistently according to ...

Utilize Bootstrap in an Angular template to effortlessly expand one element while collapsing all others

I'm facing an issue with a table in my component. Each row has a button that, when clicked, should expand to show some calculated data specific to that row. However, the problem is that when one button is expanded, it keeps other buttons expanded as w ...

Using Jquery to determine if a div is empty, excluding any content before or after it

I'm looking to use JQuery to determine if a Div is empty without taking into account any content added before or after it. When checking for emptiness, I want to ignore anything placed before or after the div. $(function(){ if($('#content&ap ...

Is it possible to use JavaScript to upload and manipulate multiple HTML files, replacing text within them to create new output HTML pages?

Is it possible to modify the following code to accept multiple files, process them, and then return the output? <html> <head> </head> <body> <div> <label for="text">Choose file</label> <inp ...

When a hyperlink is placed inside an HTML table cell, it loses its ability to be clicked on after

While utilizing Bootstrap, I have hyperlinks inside the cells of an HTML table. One of them triggers a bootstrap Modal class and another links to itself. These work well when the browser window is small in size, but once maximized, the hyperlinks stop fu ...