Placing a div with absolute positioning inside another div with absolute positioning raises the question of how it functions when the outer div is not set to position: relative

Trying to grasp the concept, I experimented with the following setup:

<div id="Outer">
    <div id="Inner"></div>
</div>

In this scenario, the outer div has a relative position and the inner div has an absolute position. This allows us to position the inner div wherever we desire inside the "Outer div" (top / left / bottom etc').

To delve deeper into how positioning works, I created the following structure:

  1. Outer div.
  2. Inner div within the outer div.
  3. Inner div nested inside the inner div.

Despite changing the position of the innermost div to absolute, I noticed that I could still position it successfully. This raised questions about why this was possible even though the parent div (div number 2) is set to position: absolute, rather than position: relative as expected.

I would appreciate an explanation on why this specific arrangement functioned as intended. Below is the complete code for reference:

<html>
<head>
<style type="text/css">
.OuterDiv {
    padding: 0px;
    margin: 0px auto 0px auto;
    width: 500px;
    height: 300px;
    background-color: #E6E6E6;
    overflow: hidden;
    position: relative;
}
.InnerDiv {
    position: absolute;
    left: 50px;
    top: 50px;
    width: 200px;
    height: 200px;
    background-color: #D3DEEF;
    overflow: hidden;
}
.InnerInner {
    position: absolute;
    top: 20px;
    right: 20px;
    overflow: hidden;
}
</style>
</head>
<body>
<div class="OuterDiv">
    <div class="InnerDiv">
        <div class="InnerInner">Inner Inner</div>
    </div>
</div>
</body>
</html>

Answer №1

If a parent element of an element with position: absolute; is not set to static, the absolutely positioned element will be placed relative to its parent. The parent element's position can be anything other than static, such as absolute or relative.

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

Each page is restricted to containing only a single server-side Form tag

I inserted a page with HTML code into a master page and encountered this error when I tried to launch the page. I'm unsure of what I did wrong! <%@ Page Title="" Language="C#" MasterPageFile="~/YourGuruMaster.master" AutoEventWireup="true" Cod ...

Easily toggle between various image source paths

In my current application, all HTML files have hardcoded relative image paths that point to a specific directory. For example: <img src="projectA/style/images/Preferences.png"/> Now, I am considering switching between two different project modes: & ...

Compatibility issues with jQuery observed in Firefox and Internet Explorer 11

Check out the code here: jsfiddle demo HTML CODE: <div class="first"> <!-- Part one --> <div class="acord_col"> <div class="img_class" id="exist_site"></div> <div class="intro_text"> </div> </div&g ...

What is the best method to adjust the width of the <option> tag within the <select> tag using CSS?

<!DOCTYPE html> <html> <head> <title>Page Title</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="s ...

Using Jquery and CSS to create a sleek animation for opening a div box container

Designed an animation to reveal a div container box, but it did not meet the desired criteria. Expectations: Initiate animation horizontally from 0% to 100%. Then expand vertically to 100% height. The horizontal animation from 0% to 100% is not visible ...

The dominance of CSS specificity in favor of the flex property compared to flex-basis

Is there a specificity among CSS properties that affects how styles are applied? I've been experimenting with flexbox and encountered an interesting scenario: The second selector .flex-basis-5 is added dynamically via JavaScript based on certain con ...

If the ID (i.e. document.getElementById) is not found, then keep JavaScript running

I'm currently working on a JavaScript project where I need the script to gracefully handle missing div-ids and continue executing. I've looked into various solutions, but many involve either replacing the missing ID or placing information into an ...

The ExpressJS EJS issue arises when trying to access a property that is undefined

I'm new to NodeJS and seeking assistance. I am working on a website where users can promote their virtual conferences for others to see. I have set up a form with the POST method, where the data gets pushed into an array and then displayed in an EJS f ...

Is it possible for media queries to effectively support mobile devices?

I have a specific requirement for my <input type="number"> element where I need the up and down spinner buttons to be displayed for selecting the next value. However, on mobile devices, these spinner buttons are not visible. To address this ...

Disappearances of sliding jQuery divs

I am currently working on a website using jQuery, and I have implemented a slide in and out div to display share buttons. The issue I am facing is that the code works perfectly on the first page, but on every other page, the div slides out momentarily and ...

Optimal guidelines for logging in

After successfully creating a website using HTML, CSS, and JavaScript, I am now looking to implement a login feature to provide privacy for certain users. I have noticed that some developers use PHP, while others use JS or Node.js for this purpose. However ...

repeating the identical content in the same setting

I am encountering an issue with jquery as I have 2 different sets of icons, each of which I want to do the same thing: My primary codes are <ul> <li> <a href=""> <i class="fa fa-facebook"></i> ...

Learn how to utilize the getElementByClassName method in JavaScript to insert checkboxes into two lists that share the same class name

I have a situation where I have two lists sharing the same class name. I need to dynamically create checkboxes in both of these lists based on XML data. My current dilemma lies in how to properly utilize getElementByClassName in JavaScript to add checkbox ...

What is the best way to display cards next to each other on desktop and mobile devices while maximizing available space?

In the world of React components, there exists a card representation within a grid view that adapts based on available screen space; function EngineerCard(engineer: EngineerDetail) { return ( <div className='engineerCard'> <d ...

Remove bulleted list from HTML using JavaScript DOM

My task involved deleting the entire "agent" array using the removeChild() function, requiring the id of a <ul> element. However, I faced an issue as I couldn't set the id for the "ul" due to using the createElement() function. //old code < ...

Arrange elements in a vertical layout and align them to the left when they exceed the boundaries of their container

I am seeking a solution for laying out my design, similar to the one shown in this image Essentially, I have a container with a fixed height of 300px. I want to display a list vertically with each item taking up a width of 33%. If the list becomes too lon ...

Changing position attributes on fixed divs may not function properly on Android devices

Utilizing CSS transitions, I've successfully managed to make divs move, but unfortunately, this functionality does not work on Android devices. When the div is set to position:fixed, it remains static without any movement. Here is the original code ...

PrimeFaces: Achieving Conditional Coloring Based on Status (Passed/Failed)

Attempting to dynamically change the color of a column based on its status value (Passed/Failed/InProgress) using jQuery. I tried copying and pasting the table into jsfiddle, where it worked. However, when implemented in the actual XHTML file, the jQuery ...

Website Not Loading Correctly

Why is my webpage not loading properly, even though it has the same HTML and CSS as my other functioning pages on the site? The page loads without the h1 element. If I comment out the span used for adding a transparent background image behind the whole we ...

I am having trouble displaying SASS styles in the browser after running webpack with node

In my current project for an online course, I am utilizing sass to style everything. The compilation process completes successfully without any errors, but unfortunately, the browser does not display any of the styles. The styles folder contains five file ...