Resizing the parent container when the height of its absolutely positioned children is unknown and dynamic upon window resize

https://codepen.io/umbriel/pen/MxazRE

One interesting feature of my slideshow is that users can change the slide by swiping their mouse cursor horizontally over the slide container.

Since the children (slides) are absolutely positioned, I faced a challenge in dynamically updating the height of the parent (slideshow container).

Given that I do not know the height of individual slides, I need to calculate the tallest div and assign that height to the parent container.

Below is the code snippet to determine the height of the tallest element:

    function getTallestSegment(element) {
      let elementsHeight = [];
      let height = 0;
      element.forEach(child => {
        elementsHeight.push(child.scrollHeight);
      });
      height = Math.max(...elementsHeight);
      return height;
    } 

Additionally, here is the window resize script to update the parent height dynamically:

    window.onresize = function(e){
      Object.assign(item.style, {
        height: getTallestSegment(getChildren) + "px"
      });
    };

While the functionality currently works when downsizing the window, as visible in the provided gif with changing height values, I am seeking a solution to make it work both ways - when expanding the window width as well!

Any suggestions on how to implement this bidirectional resizing for the parent container?

Thank you!

https://i.sstatic.net/xHYGH.gif

Answer №1

One issue with the current code is that when you expand the viewport, the elements remain at 100% height of their container. This occurs because the container has a fixed height, preventing the elements from adjusting accordingly.

By utilizing CSS grids, it's possible to overlay elements and still have the container resize based on the tallest element present. This method eliminates the need for absolute positioning or JavaScript for resizing purposes.

To implement this solution:

.hover-slide {
  display: grid;
}

All elements can then be placed in the same row and column without specifying width, height, or absolute positioning:

.hover-slide > * {     
  grid-column: 1;
  grid-row: 1;
}

With these adjustments, changing the active element will automatically adjust the height of the container to fit all elements snugly.

You can view an example of this implementation here: https://codepen.io/anon/pen/PLPLpv. I've also added a border for better visualization of the container's height.

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

What are some ways to boost the height of an element in an upward direction?

Here is the HTML Code: <ul> <li>A</li> <li>B</li> <li>C</li> <li>D</li> <li>E</li> <li>F</li> </ul> Adding one more li in the list causes it to gr ...

Tips for extracting only a portion of the JavaScript timestamp

I have a JavaScript timestamp that reads Tue Sep 30 2014 12:02:50 GMT-0400 (EDT). When I use the .getTime() method, I get 1412092970.768 Typically, this timestamp represents a specific time of today. My question is whether it's possible to extract o ...

Text beyond the body tag

My current setup includes: html, body { height: 100%; } section { width: 100%; height: 100%; } However, I have noticed that any content appearing below the .full class is not being included within the body tag. Is there a way to address ...

Trouble with sending arguments to Express middleware

I'm currently working on developing a custom input validation middleware using Express. My aim is to create a middleware that takes 2 parameters in order to validate client input effectively. Despite referencing various sources, including the Express ...

The selector By.PARTIAL_LINK_TEXT in Selenium is failing to meet the expected condition

I am trying to store the friend list of a Facebook ID. When I click on the Friends tab on the profile, the friend list should be opened if it's not blocked; otherwise, something else should open. To determine if the friend list is opened or blocked, ...

collection check_boxes = boxes for collection

Is there a way to automatically sync the checked or unchecked days in the ":committed" collection_check_boxes with the ":send_email" collection_check_boxes when a user interacts with them? For instance, if a user checks "sun" and unchecks "fri" in the "co ...

Uh oh, ERROR @wdio/runner: Looks like we hit a snag. The session could not be created because this version of ChromeDriver is only compatible with Chrome version

While trying to run a script in WebdriverIO V6 with the Cucumber framework using a local Chrome browser version 85.0.4183.83, I encountered an error stating 'ERROR @wdio/runner: Error: Failed to create session. session not created: This version of Chr ...

Discover the highest value within an array of objects, along with any numerical object attributes that have a value greater than zero

Considering an array of objects structured as follows: [{ "202201": { "WO": 900, "WS": 0, "SY": 0.915, "LY": 0.98, "CT": 75 }, "202202" ...

combining elements in JavaScript

I have two object arrays that look like this: arr1 = [ { 'v1': 'abcde', 'pv_45': 13018, 'geolocation': '17.340291,76.842807' }] arr2 =[{ 'v1':'abcde', 'pv_50&apos ...

Tips for adjusting the navigation values on a secondary page without impacting the navigation on the homepage

I'm new to working with HTML and CSS, and I'm struggling to adjust the padding and margin of my navigation bar on a second page without it impacting the main index page. Note: I want to give the nav bar a slightly different style on the second p ...

Safari iOS 8 experiencing issues with loading WebGL image textures

The example mentioned above runs smoothly on all major browsers, including Safari on Mac OS. However, it fails to display the correct image on my iPad running the latest iOS 8. Click here for the example. Similarly, the tutorial provided in the following ...

Revise for embedded frame contents

Is it possible to modify the HTML content of an iframe within a webpage? I currently have this code snippet: <iframe src="sample.html"></iframe> I am looking for a way to edit the contents of sample.html without directly altering the HTML co ...

Python Flask server struggling to find JavaScript and CSS files within index.html when paired with AngularJS frontend

I'm currently working on a simple website project that involves a Python backend running on Flask and an AngularJS frontend. The main issue I am facing is encountering 404 errors when the index template tries to load CSS and JS files. Here's how ...

Convert an array to a string using a JavaScript function

I am encountering an issue with the code below: Every time I pass the Array to "track," I encounter an error. It seems like there might be a mismatch between passing an object and a string as input, but I am uncertain and unable to verify. for (var i = 0; ...

Align the content to the right and center it within the table

One common issue I face is working with tables that contain numbers requiring right alignment to ensure the ones/tens/hundreds/thousands places line up correctly. Here's an example: 2,343 1,000,000 43 43,394 232,111 In these tables, ...

Unable to display elements from an array in the dropdown menu generated by v-for

Having just started learning Vue.js, I am facing a challenge in rendering the following array: countries: ["US", "UK", "EU" ] I want to display this array in a select menu: <select> <option disabled value="">Your Country</option& ...

Trigger a function when an A-frame element is clicked

Is there a way to have an action triggered when clicking on an object within my scene? Specifically, I want to call a function named myFunction upon clicking the object. The object has been assigned the id of #plane1 - can this be utilized to achieve the ...

Angucomplete-alt fails to display dropdown menu

On my website, there is a textarea where users need to input the name of a group project. The goal is to implement autocomplete functionality, so as users type in the project name, a dropdown menu will appear with suggestions of existing projects to assist ...

What is the best way to customize the hover-over label appearance for a time series in ChartJS?

In my Angular 8 setup, I am working with the 'Date' data type and configuring multiple datasets of a 'Cartesian' type. I have been referring to documentation from here for guidance. Despite setting up the X Axis using callbacks in my co ...

Designing a customized CloudWatch Dashboard Widget with a select input element in HTML

I am currently setting up a CloudWatch Dashboard feature that involves calling a lambda function and passing an AWS region name as a parameter. I have been attempting to implement a plain old pulldown menu for users to select the region, but have struggled ...