Is there a way for me to extend an absolute div to the full width of its grandparent when it is within an absolute parent div?

Here is a structured example of my HTML and CSS:

<div class="grandparent">
  <div class="row">...</div>
  <div class="absolute-parent">
    <div class="absolute-child">...</div>
  </div>
</div>

.grandparent {
  position: relative;
}

.absolute-parent {
  width: *gets set by JS*
  height: 30px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: *gets set by JS*
  overflow: hidden;
  margin: 0 auto;
  transition: all 0.5s ease-in-out;
}

.absolute-child{

  align-items: center;
  display: flex;
  flex-direction: row;
  justify-content: center;
  bottom: 0;
  position: absolute;
  top: 0;
  left: *gets set by JS*
  margin: auto 0;
  transition: left 0.5s ease-in-out;
}

The .absolute-parent has a fixed height, while the width and left position are dynamically set by JavaScript. It acts as a window for the content of .absolute-child, which should be layered with the content of the .row div.

Currently, .absolute-child only expands to fit its content width. I would like it to stretch across the full width of the container in the .grandparent div so that .absolute-child and .row align perfectly on top of each other.

Cheers!

Answer №1

In my experience, one way to approach this issue is by having the parent div inherit the width of its grandparent, and then have the child div inherit the width of its parent. This does require that the grandparent div has a specific width set. Alternatively, you could manually specify the width for each of the divs involved. While this solution may not be what you were hoping for, it has proven effective for me in similar situations over an extended period.

Answer №2

When dealing with absolutely positioned elements, it's important to note that they depend on their closest relatively positioned ancestor for positioning and size (especially when defined in percentages). To effectively make an element the same width as its grandparent, you can simply apply width: 100% to the CSS class .absolute-child.

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

Using multiple conditions in SetEnvIf

I am trying to implement the SetEnvIf directive in my .htaccess file for handling multiple conditions and redirecting to a specific URL. The code I have written is as follows: SetEnvIf Remote_Host "^" press_flag=0 SetEnvIf Request_URI '/press/$&apos ...

Prevent the cascading of CSS styles on child list items

When constructing my menu, I have organized it using the following HTML structure: <div id=”navigation”> <ul> <li class=”level1”>Top Level Menu item <div class=”sublevel”> <ul> ...

Ways to deactivate a button using CSS

I need to use Javascript to disable a link based on its id. By default, the link is invisible. I will only enable the link when the specific id value comes from the backend. HTML <li id="viewroleId" style="display: none;"> <a href="viewrole" ...

Issue with JQuery time picker functionality not functioning properly upon repeat usage

I am facing an issue with a modal dialog that contains a form loaded via ajax. The form includes a time field populated using the jquery timepicker. Everything works perfectly when I open the dialog for the first time. However, if I try to load the dialog ...

Scaling Images using HTML and CSS

Hey there, I'm currently learning web development and running into a bit of trouble with creating responsive images. Can anyone give me some guidance on what changes I should make in the code below? Here's the HTML code: <div class="caro ...

Component fails to update when attribute is modified

My issue is that the crud-table component does not refresh when I change currentTable. It works when I assign currentTable = 'doctor' in the created() hook, but not here. Why is that? <template> <div id="adminpanel"> <div id ...

Utilize JavaScript when sharing on social media to avoid the possibility of embedding the entire

This javascript code snippet is extracted from www.twitter.com (simply click to view the source code). I have reformatted it for better readability: if (window.top !== window.self) { document.write = ""; window.top.location = window.self.location; s ...

What is the best way to display my table?

In the index.php view, you will find my table located <table class="striped"> <thead> <tr> <th>Id</th> <th>Name</th> <th ...

Unusual Behavior Uncovered in jQuery Selectors

Have you ever noticed a peculiar behavior of jQuery selectors? I recently discovered that when the page contains elements with non-unique IDs, jQuery returns different results for the same selectors: Here's an example of HTML code: <button id=&ap ...

Is it possible to combine numerous -webkit-transition animations in my css code without using keyframes?

I'm experimenting with chaining multiple -webkit-transition animations in my CSS without using keyframes. I understand it's possible to achieve this by calling a keyframe animation, but I prefer to simply overwrite the properties. However, for s ...

CSS slideshow animation is not functioning properly

Hey there, I'm new around here and I've got a question for you all. Please bear with me if I make any mistakes. So, I'm attempting to create a simple CSS slide image. In my animation code, I want the picture to fade from the left every time ...

Modifying webpack settings for a create-react-app based project: A step-by-step guide

After starting a new react project with create-react-app, I am looking to update the webpack configuration. However, I cannot seem to locate the webpack file. Should I be creating this file myself, or is there another process involved? As someone who is ...

The display/block feature will only function if the div element is contained within a table

I am facing an issue with hiding/showing two div elements alternatively. The code works perfectly when the divs are placed within a table, but fails when they are not in a table due to compatibility issues with Internet Explorer. I prefer not to use a tabl ...

Get the game using electron / determine the game's version via electron

I'm currently working on creating a game launcher using electron. There are two main questions that I have: What is the best method for downloading files from the client (specifically in AngularJS)? FTP or HTTP? How can I implement a system to detect ...

nodemon breaks down frequently while anticipating changes in files

After cloning a project that I finished 2 months ago, I am facing an issue where nodemon won't run. Despite trying to close npm using task manager on Windows and running it again, the error persists. I am also utilizing MongoDB as my database. If any ...

Ways to implement collapsible functionality into table rows

I have a table on my website and I want to initially display only 3 rows. When the user clicks on the 'more' button, I want the rest of the rows to be displayed. However, when I tried implementing this with code, I encountered rendering issues. I ...

Utilizing a Bootstrap column design with two columns in place - one column set at a fixed width while the second column stretches across the remaining

Need help with adjusting two columns on a webpage? The right column is fixed at 300px width, while the left column should take up the remaining space. Using flex works well, but when adding a responsive ad to the left column, it extends beyond the availabl ...

Pause the for loop until all nested asynchronous database calls are completed

Currently, I am utilizing the listCollection method within mongodb to loop through each collection that is returned using a query with find. The issue arises when I attempt to construct an object within the loop that I intend to return with response.json ...

Dispatching identification to controller after submission

I am encountering an issue with a page that contains the following code: <div class="col-sm-2 displaybutton"> <div data-point-to="displaysHeader"> @using (Html.BeginForm("Administration", "Home", FormMethod.Post)) ...

Rails: Ensure that JSON form fields remain populated in case the form encounters a validation error

I am using a rails simple form to create a product with three fields inside in order to associate it with appropriate categories: <div class="form-group"> <%= f.input :child_category_id, :collection => @categories.order(:name), :l ...