The element positioned with a z-index of -1 will be placed beneath the grandparent's division

Why does using z-index:-1 on an absolutely positioned child element make it go under the grandparent div? How can I ensure that the absolute-child stays under the parent div instead?

.grandparent{
  background:rgba(0, 128, 0, 0.89);
  width:500px;
}

.parent{
  position:relative;
  width:200px;
  height:200px;
  background:yellow;
}

.absolute-child{
  position:absolute;
  width:100px;
  height:100px;
  background:red;
  top:10px;
  right:-50px;
  z-index:-1;
}
 <div class="grandparent">
    <div class="parent">
      <div class="absolute-child"> absolute ch
      </div>
      <div class="siblings">siblings</div>
    </div>
 </div

Answer №1

Here is the solution you need: I included position: relative; in the parent element to ensure that the z-index: 0; property can function correctly.

.grandparent{
  background-color: rgba(0, 128, 0, 0.89);
  width: 500px;
  z-index: 0;
  position: relative;
}

.parent{
  position: relative;
  width: 200px;
  height: 200px;
  background: yellow;
}

.absolute-child{
    position: absolute;
    width: 100px;
    height: 100px;
    background: red;
    top: 10px;
    right: -50px;
    z-index: -1;
}
<div class="grandparent">
    <div class="parent">
      <div class="absolute-child"> absolute child
      </div>
      <div class="siblings">siblings</div>
    </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

Is there a way to ensure a div expands according to screen resolution rather than content size?

I'm currently working on a website where the right-side sidebar should extend to the bottom of the page across various screen resolutions using the height:100%; property. However, I'm facing an issue where it only extends to the content within it ...

Tips for recalling the display and concealment of a div element using cookies

My HTML code looks like this: <div id='mainleft-content'>content is visible</div> <div id="expand-hidden">Button Expand +</div> To show/hide the divs, I am using JQuery as shown below: $(document).ready(function () { ...

Can one customize the background color of a segment in a radar chart using Chart.js?

Could I customize the color of the sectors in my radar chart to resemble this specific image? https://i.stack.imgur.com/U8RAb.png Is it feasible to achieve this using Chart.js? Alternatively, are there other chart libraries that have this capability? It ...

Utilizing jQuery/Ajax to send an ID parameter to a PHP script

This code is where the user interacts. <html> <head> <title></title> <script src="jquery-1.9.1.js"></script> <script src="jquery.form.js"></script> </head> <body> <?php include 'con ...

Using jQuery validation to verify that a minimum of one radio button holds a true value

I have a form with two questions. The first question asks if the product value exceeds a certain fixed amount, and the second question asks if the product value is below that fixed amount. Upon submitting the form, validation should ensure that at least on ...

Problem arising from implementing background and relative positioning in html and css

Encountering an issue with html and css. Today, attempted to create a page with a large background image for the first time. Utilizing the foundation framework, but facing a challenge. Foundation is an adaptive framework, and when resizing the browser, a ...

The body width does not extend to 100% when the browser window is less than 400 pixels wide

I'm encountering an issue where the HTML and body width are not 100% when the browser window is less than 400px wide. Strangely, everything works perfectly when the window is wider than 400px. Attempts to fix this include setting the body and html pr ...

socket.io and the use of various chat rooms

Is it possible for a client socket in a browser to connect to multiple rooms (3-5) and receive separate messages from each room while using node.js to send messages to all clients in the same room? ...

Float and tap

Can someone assist me with my code? I have 4 identical divs like this one, and when I hover over a link, all the elements receive the same code. <div class="Person-team"> <div class="profile-pic-d"> <a cl ...

Retrieving the size of every image with ajax while also storing extra image information

In my current project, the objective is to iterate through all images on a webpage and collect various details such as image name, original dimensions, actual size, ratio, display property, and FILESIZE. However, I have encountered a hurdle in my progress ...

Safari does not display disabled input fields correctly

I have designed a simple angular-material form with various inputs that are organized using angular flex-layout. The form displays correctly in all browsers except for Safari on iOS devices. The problem in Safari arises when loading a form that contains d ...

Tips for inserting database table parameters into CSS style

Is there a way to define CSS style parameters in a database table (MySQL) and then use them to style elements such as a menu? Currently, I am aware that we can use variables within styles like this: <div id="content-inner" style="<?php echo $this-&g ...

Tips for displaying and concealing a toggle button based on screen size changes

My goal is to display my userlist without a toggle button by default when the screen is in full size, and only provide a toggle button when the screen is in mobile size to allow users to show/hide the list. Currently, I am utilizing the following code: & ...

How about this: "Implement a slider in the header of a php

I am looking to add a slider to the header of my phpBB website. I have inserted the code into template/overall_header.html but it doesn't seem to be working. As someone who is new to phpBB, any assistance would be greatly appreciated. ...

Curved right side of div using CSS (see example image for reference)

I'm attempting to achieve this effect using CSS on a div element instead of an image. Here's the link I am referencing: https://i.sstatic.net/045dh.png If you take a look at the image provided in the example and focus on the right side, that&apo ...

css three columns evenly spaced in the center

I am currently using bootstrap, CSS, and HTML to design a responsive webpage with three columns in a row. The layout consists of a textbox on the left, a button in the center, and a table on the right. So far, I have made three separate attempts at achiev ...

Using the mpdf addPage function generates new empty pages

Hey there, I'm facing an issue where using $mpdf->addPage() within a for loop results in creating blank pages instead of adding content. My goal is to generate a new page when a certain condition is met and then populate it with the required conten ...

Transferring data from a table to another window

Currently, I am retrieving values from a database and displaying them in a table. I would like to add a button to each row that will allow users to view more details about that specific row. At the moment, only ThesisID, AuthorID, and Title are visible. ...

Troubleshoot: CSS Paths Broken after Deploying Django App on AWS Elastic Beanstalk

I recently deployed my first simple Django 1.8.6 application to AWS via Elastic Beanstalk. While everything appears to be working properly, the styles are not loading as expected. Below is the template code: {% load blog_tags %} {% load staticfiles %} ...

What happens when HTML5 Video quality starts to decline?

Exploring the capabilities of the HTML5 video tag has left me pondering the best approach for graceful degradation in cases where a codec is not supported. Handling older browsers (or IE) that lack support for the video tag altogether is quite straightfor ...