How to place a child <div> within a parent <div> that is fixed on the page

I am currently working on creating a modal window using HTML/CSS. My goal is to have the modal window fixed in position relative to the browser window, with a white background. Inside the modal, there will be an image at the top and a div element at the bottom for description text. I want to position the child div so that it has a left offset of approximately 8.33% of the width of the parent.

Initially, I tried absolute positioning for the child div, but then the background of the parent modal did not extend to cover the child div:

To see the issue, check out this link: https://i.stack.imgur.com/6SDHW.jpg

Here is the HTML/CSS code for reference:

<div class="modal col-4 l-offset-4" id="robot-modal">
    <img src="media/robot_modal.jpg">
    <div class="col-10 l-offset-1">
    </div>
</div>

CSS code:

.col-10 {
  width: 83.33333%;
}

.l-offset-1 {
  left: 8.33333%;
}
.modal {
  @include align-vertically(fixed);
  display: none;
  z-index: 2000;
  background: white;
  img {
    width: 100%;
  }
  div{
    position: absolute;
    background-color: blue;
    height: 100px;
  }
}

However, changing the child div's position to 'relative' resolved the issue:

You can see the correct display here: https://i.stack.imgur.com/SMisG.jpg

I'm still unsure why I need to use relative positioning for an element inside a fixed parent. According to this article: , if we want to position an element relative to its immediate parent, the parent should be either absolutely or relatively positioned with the child as absolutely positioned. So why does adjusting the offset of a relatively positioned child work in my case? I also prefer not to assign a height to the parent modal as I want it to dynamically adjust when new elements are added.

Answer №1

When a parent element does not consider the size of its absolute child, it can have certain effects on the layout. As mentioned on MDN:

Absolute positioning removes the element from the document flow and positions it relative to its closest ancestor or the containing block.

If something is placed after an absolutely positioned element, it will appear on top because the absolute element no longer affects the flow of other elements.

Options for handling absolute positioning include using

position: relative; left: 8.33%; right: 8.33%
, keeping it static with margin: 0 8.33%, or if absolute positioning is necessary, setting a margin bottom for the following content based on the height of the absolute div
.modal { margin-bottom: [height of absolute DIV] }
, assuming the height remains constant.

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

Validation needed for data list option

Here are all the details <form action="order.php" method="post" name="myForm" onsubmit="return(validate());"> <input type="text" list="From" name="From" autocomplete="off" placeholder="From Place"> <datalist id="From"> <option valu ...

Navigating through Expression Engine and utilizing conditional statements

Would greatly appreciate some assistance with this issue. I am working on an Expression Engine website and encountering difficulties with simple conditionals for navigation active states. Despite having a color state designated within the styles, there s ...

Adding the !important rule in React inline CSS effortlessly

Is there a way to include !important in my inline CSS property without disrupting the entire style? import React from "react"; export default class Todo extends React.Component { render() { const {text} = this.props; const cardStyl ...

Tips for accessing a local file on a website through HTML - easy steps to follow!

Is it possible to open a local (jpg or pdf) file from a random website that is not under my control? I have tried editing the HTML code of a site in Chrome by replacing existing links with ones linking to my local file, but when I click on them, nothing ha ...

Breaking free of the constraints of a 100% height container, UL escapes its

In both Chrome and Firefox, UL list items do not seem to adhere to the parent container's width of 100%. I attempted to use list-style-position: inside;, but unfortunately, that did not solve the issue: <html style="height: 100%"> <body styl ...

Creating a dynamic trio of graphs with HTML5, CSS3, and Vanilla JavaScript

I successfully created a tree graph using HTML5 and CSS3, but currently the nodes are static. I am looking to enhance the graph by making it dynamic. By dynamic, I mean that if the number of nodes increases or there are multiple children added, the graph ...

Addressing Layout Problems When I Enlarge the Browser Width

My website is www.adventureswithross.com When the device width is larger than 601px, I have specific requirements: I need the menu and custom header to be seamlessly connected without any spacing in between. To address the issue of space above the custo ...

Firefox experiencing issues with the onchange event

Here in this block of code, I have two dropdown lists: one for department and the other for section name. Based on the selected department, I dynamically change the options available for the section name dropdown list and then populate the values from both ...

Scraping HTML data without <div> tags with Scrapy - how to do it?

After spending hours attempting to extract a specific data set for use with Scrapy in a web scraping project, here is the current python code: bedrooms_info = house_listing.css( '.search-results-listings-list__item-description__charact ...

Having difficulty retrieving an angular file from a location outside of the asset folder

I'm encountering issues with a small project that is meant to read a log and present it in table format. Here is the outline of the project structure: project structure Within the LOG directory, I should be able to access motore.log from my DataServi ...

Configuring the interactive background for a textbox

How can I set a background image for a textbox that is clickable? I tried using a div tag with position:absolute positioned above the textbox, and included the image within it. However, the issue is that the image appears above the text. I attempted to a ...

I have developed a web page using asp.net that cannot be resized

Hey there! I'm interested in building a web page that cannot be resized or minimized. Is there a way to do this? Additionally, I've noticed some advertising pages that are fixed on the screen - how can I create something similar? ...

To ensure that new tabs are opened directly within jQuery UI tabs, the tab should be created within the jQuery UI tabs interface

I have integrated jquery-UI to create a dynamic Tab panel. When I click on the "Add Tab" button, a new tab is created. However, the new tab does not open automatically. It only opens when clicked on. $(function() { var tabTitle = $( ...

Why can't the file upload button locate its CSS styles?

Check out this jFiddle demonstrating the issue. Some of the Angular code may appear broken in the example, but that's just due to it being pasted into jFiddle; it's not an actual problem I'm facing. The CSS styles aren't working on the ...

How to Customize Password Input Fields in HTML

My input field is set to password type, allowing only a six-digit number: <fieldset> <label for="password-input">Enter New Pin</label> <input type="password" name="password" id="password-input" inputmode="numeric" minlength="6" ...

Create a script that ensures my website can be set as the homepage on any internet browser

I am currently in search of a way to prompt users on my website to set it as their homepage. Upon clicking "Yes," I would like to execute a script that will automatically make my website the user's browser homepage. I have come across a Similar Thread ...

Tips on displaying an avatar above and outside the boundaries of a background element using Chakra UI

They say a picture is worth more than a thousand words, and in this case, it holds true. The best way to illustrate what I'm trying to accomplish is through the image linked below. https://i.stack.imgur.com/5WvkV.png I envision having the avatar po ...

Extract a specific pattern from a string using Javascript

Currently, I am attempting to extract a specific string from a tag in javascript using the following code: document.querySelector('.title h2').textContent However, when I execute this code, I am getting a result that includes unnecessary double ...

Break down the organization of CSS

While exploring a website, I came across this CSS code that sets the font family to "Open Sans," Helvetica, Arial, and sans-serif. However, I am having trouble understanding this structure. Any assistance would be greatly appreciated. Thank you in advanc ...

What could be causing a hydration error when utilizing a table in Next.js?

When attempting to use the tr tag in my code, I encountered an error message that reads: Unhandled Runtime Error Error: Hydration failed because the initial UI does not match what was rendered on the server. import React from 'react' import {useS ...