How to position a child element beneath its parent using CSS

Can you position a child element (C) beneath its parent (B), but above B's neighboring element (D)?

Explaining this can be tricky, so feel free to check out an example here.

The main question is: how do you place the blue div.inner above the red div.neighbor, but still under the green div.outer?

Here's an illustration of the setup:

HTML structure:

 <div class="neighbor">&nbsp;</div>
 <div class="outer">
     <div class="inner"></div>
 </div>

CSS styling:

.neighbor{
background-color: red;
height: 500px;
width: 500px;    
}

.outer{
background-color: green;
width: 300px;
height: 300px;
position: fixed;
top: 0px;
left: 0px;    
}

.inner{
background-color: blue; 
width: 100px; 
height: 100px; 
position: fixed; 
top: 0px; 
left:250px;    
}

Answer №1

Check out this JsFiddle for more!

Here's the HTML structure:

<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>

And here's the corresponding CSS styling:

.red {
    background-color: red;
    height: 500px;
    width: 500px;
    z-index: 1;
}

.green {
    background-color: green;
    width: 300px;
    height: 300px;
    position: fixed;
    top: 0px;
    left: 0px;
    z-index: 3;
}

.blue {
    background-color: blue;
    width: 100px;
    height: 100px;
    position: fixed;
    top: 0px;
    left: 250px;
    z-index: 2;
}

Answer №2

.neighborhood {
        background-color: crimson;
        height: 550px;
        width: 550px;
        position:fixed;
        z-index:-250;
    }

    .district {
        background-color: lime;
        width: 320px;
        height: 320px;
        position: absolute;
        top: 10px;
        left: 10px;
    }

    .interior {
        background-color: navy;
        width: 120px;
        height: 120px;
        position:relative;
        z-index: -150;
        top: 20px;
        left: 270px;
    }

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

Creating a JavaScript function that responds to multiple click events

Can someone please help me out? I have the link to the output of my work below using JavaScript and HTML: My goal is for only one circle to be active when clicked, while the others are disabled. Currently, when I click on a circle and then another one, bo ...

How tall can an image be to load successfully on an iPad website?

Similar Query: Max image width in Mobile Safari? Dealing with unwanted downscaling on panoramas What is the maximum height an image can be loaded at on a website when viewed on an iPad? I've tried loading an image (a sprite) with a height exceeding ...

Changing the background color using ReactJS

After streamlining my project to focus on the main issue, I am still facing a problem. Here is the relevant code snippet: .App{ background-color: aqua; } .AboutMe{ color:blue; background-color: yellow; } This is from App.js: import logo from '. ...

Positioning div at the top of the body section

Presently, I have this specific designhttps://i.sstatic.net/U4IT4.png The issue I am facing is with the alignment of the jumbotron (boostrap v5). It is currently centered, but I would like it to be positioned at the very top of the body. I have experiment ...

What is the process to set a Woocommerce checkout field as optional when a checkbox is marked?

I need assistance with customizing the checkout page on Wordpress Woocommerce. Specifically, I want to make the field 'billing_name' not required if the checkbox 'buy_on_company' is checked. Currently, I have successfully hidden ' ...

What methods can be used to defer the visibility of a block in HTML?

Is there a way to reveal a block of text (h4) after a delay once the page has finished loading? Would it be necessary to utilize setTimeout for this purpose? ...

Disable responsiveness for low-resolution devices

Is there a way to disable responsive design on small resolution devices? The responsive layout appears distorted when accessed on a phone. See screenshots for more details. Phone Desktop ...

Controlling the Flow of Events in JavaScript Documents

Explore the integration of two interconnected Javascript files and delve into managing event propagation between them effectively. 1st js file var red = [0, 100, 63]; var orange = [40, 100, 60]; var green = [75, 100, 40]; var blue = [196, 77, 55]; var ...

Switch up the link color when it pops up

Is there a method to change the link color to gray without encountering glitches like on my site, where it mistakenly shows "Quick Nav."? Click here to view the page with the glitch I only want this particular link to be bold and not any other links. He ...

Displaying a check icon on a chosen card using React.js

Is there a way to display a check icon on the selected card similar to this example https://i.sstatic.net/Usuj6.png If you have any ideas or suggestions, please help. Thank you in advance! ...

Executing JavaScript function by clicking on <img>

I've been developing a website similar to YouTube, and I'm facing difficulties with the Like/Dislike feature for comments. Currently, I have implemented it in the following way: when a user clicks on an image (thumbsUp.png or thumbsDown.png), a ...

Example TypeScript code: Use the following function in Angular 5 to calculate the total by summing up the subtotals. This function multiplies the price by the quantity

I have a table shown in the image. I am looking to create a function that calculates price* quantity = subtotal for each row, and then sum up all the subtotals to get the total amount with Total=Sum(Subtotal). https://i.stack.imgur.com/4JjfL.png This is ...

What is the best way to design a grid consisting of 14 divs that span the entire width of the browser screen, incorporating dividers to separate different groups, utilizing a combination of html,

I am interested in creating a layout with two rows of divs that span the entire width of the screen, with a total of 14 divs in each row. These divs are fixed in size and grouped by color, with dividers separating each color group. I am hoping to achieve ...

Utilizing Dynamic URLs and Transmitting Non-User Input Data from HTML to Python via Flask

After doing some research, I discovered that data can only be sent from input forms in HTML to Python using the POST method. I am now trying to find a way to pass a value (originally stored in a dictionary passed from Python) from HTML to Python. I have t ...

When utilizing dynamic binding within *ngfor in Angular 4, the image fails to display properly

I'm encountering an issue with an <img> tag that isn't behaving as expected in my code snippet: <div *ngFor="let familyPerson of userDataModel.family" class="col-md-6 col-lg-4 family-member"> <div class="fm-wrapper"> ...

The issue of CSS transform scaleX with opacity not functioning correctly in Safari

Check out the code here. In Safari on Mac or iPhone, when using scaleX with opacity in a background image, it doesn't work. To fix this issue, we can use scale or scale3d(sx,sy,sz). But why does this happen? Is it a bug specific to Safari? @keyframe ...

What is the best way to ensure these 2 divs are aligned vertically in the provided html (starting at the same vertical level)?

<div class="container"> <div class="row"> <div class="col-lg-12"> <div class="news-title"> <h3 class="title">NEWS & ARTICLES</h3> ...

Having trouble with selecting checkboxes in a div using jQuery? While it may work in IE and Firefox, Chrome seems to be causing issues

In my div, I have several checkboxes placed under labels for formatting purposes. There is a list of checkbox names that need to be checked upon certain actions. Here is the list: var columns= ['2','5','4'] This is the curren ...

CSS tooltip box with embedded HTML for interactive content display

After doing some research, I came across a code that almost does what I need. The code allows me to hover over text and display a tooltip box with HTML content such as images and links. It worked perfectly with jQuery, but the challenge is that eBay doesn ...

Center the text within a span element in an inline-block container

I'm currently working on a website design that features a flat aesthetic. I have a header at the top and two blocks of different colors placed side by side underneath it. Initially, I attempted to use float left and right for positioning but was recom ...