Increase the size of the parent div as the height of the position:absolute child div grows

I am facing an issue with a parent div and its 2 child divs.

<div id="parent">
        <div class="child1 col-2"></div>
        <div class="child2 col-10"></div>
    </div>
    

The child1 div has absolute positioning and collapsible items. When I expand the collapsible items within child1, its height increases. How can I adjust the height of the parent div and/or child2 div to accommodate the increased height of child1 after the page has already loaded?

Answer №1

To position your first child (child1) absolutely, you must calculate the height of the first div and assign the top value to the second div.

See Demo

HTML

<div id="parent">
    <div class="child1 col-2">Child1<br/>test1
        <a class="collapse" href="#">collapse</a>
        <div class="insideDiv">
            test inside
        </div>
    </div>
    <div class="child2 col-10">Child2</div>
</div>

CSS

.child1 {
    position:absolute;
}
.child2 {
    position:relative;
}
.insideDiv {
    display:none;
}

jQuery

var child1Height = $(".child1").height();
$(".child2").css('top' , child1Height); 

$(".child1 .collapse").click(function(){
    $(".insideDiv").slideDown(function(){        
        var countHeight = $(".child1").height();
        $(".child2").css('top' , countHeight);    
    });
})

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

Trigger the click event on the ul element instead of the li element using jQuery

Is there a way to make the click event only affect ul tags and not all li elements using jQuery? <!-- HTML --> <ul class="wrap"> <li>test1</li> <li>test2</li> <li>test3</li> </ul> I attemp ...

Creating a draggable div with JQuery that can be sorted and saving its position in a database

I am working on a project where I need to implement drag and drop functionality for multiple div tags on a page using jQuery. Additionally, I want to save the position of all the divs in a SQL Server database. Essentially, I am trying to create a page simi ...

Troubleshooting a mysterious anomaly with a jQuery UI button

Would like to achieve something similar to this: http://jqueryui.com/demos/button/#icons When trying to replicate it, http://jsfiddle.net/p5PzU/1/ Why is the height so small? I expected a square shape but am getting a rectangle instead. What could be c ...

Tips for creating Card-Title responsiveness across various screen dimensions with Bootstrap 4

I'm a beginner in web design and I'm wondering if it's possible to adjust the font-size of the card-title and the size of the card image based on different screen sizes using bootstrap 4. When viewed on small screens, they appear too large. ...

Challenging design with CSS shapes

Can this specific shape be created using just one HTML element? I am looking to use it for image cropping purposes, so having only one element would make the process easier ...

polygon with five or more sides

Can divs be created with shapes such as five or six corners? I am looking to create clickable zones on a map and any alternative methods would be greatly appreciated. ...

Dynamic Select Option housing Bootstrap Popover

I am attempting to create a popover that generates dynamic content and reacts when an option is selected. On my page, I have a select element and one of the options should trigger a popover containing a form with 'Accept' and 'Cancel' ...

What is the best way to extract a CSS rule using PHP?

Below is an example of the stylesheet I am currently using: #thing { display: none; } I am looking to retrieve the value of the 'display' property using PHP instead of Javascript. While I know how to do this with Javascript, I would prefer to u ...

Struggling with getting the navigation bar to show up in a row instead of stacking

Hey there, I need some help with the layout of my page. Here's the link to the page I'm currently working on: At the moment, the list is showing up vertically instead of horizontally. CSS: li { display:inline; list-style-type:none; ...

Tips for sending multiple HTTP requests to a single page from a single client

Is there a way to run multiple AJAX calls on the same page from the same client without them getting queued by the server? The calls are starting correctly, but it seems like the server is executing only one request at a time. I've checked the start ...

Is there a way to send zip file(s) through a $.ajax() POST request without relying on an HTML form?

I have used the JSZIP library to create a zip file containing 2 xml files: jszip.js jszip-load.js jszip-inflate.js jszip-deflate.js var zip = new JSZip(); zip.file("hi.xml", "<?xml version="1.0"?><root/></root></xml>"); zip.fil ...

Adding new rows between existing rows in an HTML table using Jquery syntax

How can jQuery be used to insert rows between existing rows in an HTML table? <Table id="TEST"> <tr>1</tr> <tr>2</tr> </table> I need to include a new row <tr>xx</tr> between the rows with values ...

What error am I making in the Date calculator for the select box using Javascript/jQuery?

$(.dateselboxes) .change( function(){ var y; y=$("#year").val(); var m; m=$("#month").val(); var d; // check for leap year var isLeapYear; if(y%4==0) { if(y%100==0) { if(y%400==0) {isLeapYear=true;} else {isLeapYear=false;} } ...

How to Monitor Clicks on a Flash Movie using jQuery

I've been working on a dynamic banner system that can support image banners and flash banners using object/embed. The site relies heavily on jQuery, especially for handling 'click' events. While tracking clicks on image banners is straightf ...

Issue with Vuetify carousel not resizing to fit the entire width and height

I am looking to create a carousel that spans the entire width and height of a viewport in my Vue.js 2 project using Vuetify. Below is the code I have so far: <head> <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500, ...

How can I ensure that the image width in jQuery is limited to the original size and does not

Hello everyone, I am currently trying to figure out how to solve the following issue: function setimgwidth() { var bw = $('.post-body').width(); $(".post-body p img").each(function() { var os = $(this).width(); //if (bw & ...

Tips for dynamically adjusting the size of a div container according to the user's

I have been working on a unique landing page design featuring a menu made up of custom hexagons. I aim to make these hexagons dynamically adjust to fill the entire screen based on the user's resolution, eliminating the need for scrolling. For a previ ...

The effectiveness of border-radius is limited to only one side of the div

After experimenting with applying the border-radius property to a div, I encountered an issue where the border radius only worked on one side when the radius value was too large. How can I resolve this problem while maintaining the border-radius value on a ...

Can a vertical line be sketched next to a horizontal line?

Let's keep it short and sweet. Here's what I'm attempting to achieve: https://i.sstatic.net/e2ZU2.png Currently, this is what I have: https://i.sstatic.net/cagv7.png Below is the code snippet: #ligne_horizontale_experience { ma ...

What are some methods for decelerating typewriter text animation?

I'm currently updating my portfolio and have incorporated a typewriter effect into the second line of text. However, I'm finding that the speed of the effect is a bit too fast for my liking. I've attempted to adjust the animation seconds, de ...