Can a CSS property be created that is relative to the heights of certain elements?

I have a container in my HTML that contains child elements, some of which have the CSS class child-active.

I am trying to make the height of the container adjust dynamically based on the tallest active child. Essentially, I want the container's max-height to be equal to the height of its tallest child with the class child-active. This is to address an issue I am having with CSS transitions.

An example of what I am looking for:

.container {
   max-height: calc(max(/* all the heights of .container > .child-active*/));
}

.child {
   ...
}

.child-active {
   ...
}
<div class="container">
   <div class="child"> the height of this content is ignored </div>
   <div class="child child-active"> the height of this content sets the height of the container </div>
</div>

Is it feasible to achieve this? It seems like JavaScript would be necessary.

Answer №1

Is setting the .container element to have a display:flex property not an effective solution?

If not, it might be beneficial to explore container queries.

Answer №2

If you're looking for a straightforward solution, you can set the height of the child-active element and then use that same height as the max-height for the container. Alternatively, experiment with max-height: max-content, max-height: auto, or max-height: fit-content. Another approach is to utilize display: flex, define the heights of both the child and active child elements.

.container {
   display: flex;
   border: 1px solid red;
}

.child {
   height: 20px;
   border: 1px solid blue;
}

.child-active {
   height:40px;
   border: 1px solid green;
   
}
<div class="container">
   <div class="child"> the height of this content is ignored </div>
   <div class="child child-active"> the height of this content sets the height of the container </div>
</div>

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 could be causing my website to be wider than the window?

Currently, I am engaged in an educational project that involves developing a brief webpage to introduce users to a topic of their choice. However, I am facing an issue where my website is displaying a horizontal scrollbar, and I'm unable to pinpoint t ...

We could not locate the requested resource with a DELETE request using the fetch JSON method

Currently, I am in the process of developing a webpage that utilizes JSON API REST alongside XAMPP with an Apache server. Up until now, everything has been working smoothly as I have been utilizing the DELETE method successfully. However, I seem to have hi ...

Having an absolute element can lead to overflow causing a scroll

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .wrap { ...

What is the method for automatically scrolling a WebView HTML document?

I am seeking a way to implement autoscroll on my webpage. Here is the solution I have come up with: public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCon ...

Engaging with tasks like incorporating fresh elements into JavaScript code

I am looking to implement an event listener that triggers whenever a new element is added to the document or any of its children. Can someone recommend a method for accomplishing this? ...

Tips for incorporating dynamic colors into text containers in HTML: "Enhance your

I'm looking for a solution to change the color of a span every time a button is clicked. The colors are stored in an array like this: var colorArray = ["red","blue","red","red","green","blue","green","red"] The array values may vary as they are read ...

How come it's so difficult to set the perspective camera to accurately display sizes in three js?

Currently, I am using Three Js to create a visual representation of a car. However, I am encountering an issue with the perspective camera that I can't seem to resolve. My goal is to make it appear more realistic by only showing the front of the car, ...

Instructions on using $.post in jquery to sude uploaded file to a php file for processing

Is there a way to upload photos asynchronously using $.post without relying on .serializeArray()? I want to send a form, containing an upload file, to a PHP processing file. Any suggestions? HTML: <form action="upload1.php" method="post" id="usrform" ...

Is there a way to listen for the validation of an HTML5 form before it is submitted?

I've been trying to figure out a way to detect if a form has been validated, but so far, no luck. For example: User clicks the submit button The form is invalid, so the submit event doesn't occur At this point, I want to add the class .form-fe ...

Is there a way to remove the entire row if I dismiss a bootstrap alert?

Hey there! I'm having a little issue with an alert in a row-fluid. Curious to see? Check out my fiddle. So, when I click the "x" button to dismiss the alert, the row is still visible. Any idea how I can make the entire row disappear when I dismiss it? ...

Designing forms using jQuery/Ajax and PHP for dynamic processing

My approach to developing forms that require PHP processing involves putting the form and PHP code into a single file, such as signup.php. I use the $_GET method to determine the action needed for the form. For instance, if /site/signup.php is requested, ...

Tips for choosing a dropdown option based on its class name in Selenium WebDriver with Java

Just starting out with selenium and currently I'm diving into it. My goal is to choose a value from a drop down menu. The dropdown has the class "ns-dropdown" and contains several options, one of which is Australian Dollar. Below is the HTML snippet: ...

Refreshing the images array in the DOM using ajax technology

I have a webpage with various images scattered under a table, like in the example of homepage.htm: <table id="imagesTable"> <tr> <td> <img src="img1.png"> <div> <img src= ...

create an HTML element using JavaScript to display a box with dimensions of n

I am attempting to create a grid in an HTML document using only plain JavaScript. The idea is to take a number from a URL and use that as the basis for generating the grid. For example, if my URL looks like this: abc.html?num=5, then I would need to creat ...

What purpose does tagging serve in my template for polymer property binding?

I'm currently exploring the way Polymer handles the rendering of properties in a custom element's template. I've come across some interesting behavior that I haven't been able to fully grasp yet. Specifically, I noticed that certain pro ...

Managing excess space in a flexbox: Tips for handling events

Is there a way to manage events triggered by clicking on the violet-colored space around elements? I tried adding a general onclick event to the container, but it seems to be interfering with the individual item behaviors that already have their own intern ...

When utilizing content: url() for a local image, it does not show up, whereas it will display for a web URL. What could be causing this discrepancy

I'm attempting to convert this image into a button. It works fine when acquiring the image through a website URL (not from my own website), but it doesn't work when using a relative path. The image just won't show up. For instance: .my-cla ...

Drag and drop a Jquery image onto the div with its top left corner

Drop targets: <div id="targetContainer" style="width:100px; height:100px;> <div id="tar_0-0" class="target" style="position: relative; float:left; width:50px; height:50px; background-color: #fff;"></div> <div id="tar_1-0" class="t ...

HTML combined with Python coding allows for dynamic and interactive web

Can I incorporate Python scripts into HTML code, similar to how PHP is written between <?php ... ?> tags? I want to make sure my Python application can be executed in the browser. Thank you for any assistance! ...

Swap out the image backdrop by utilizing the forward and backward buttons

I am currently working on developing a Character Selection feature for Airconsole. I had the idea of implementing this using a Jquery method similar to a Gallery. In order to achieve this, I require a previous button, a next button, and the character disp ...