Adjust the height of an element using CSS based on the height of another

Is there a way to have two divs per row, where the second div always displays its full content and the height of the first div matches the height of the second div? If the content in the first div exceeds the height, it should be scrollable. I've attempted to set the height using javascript but it seems inefficient as the content can change. Is there a CSS solution for this effect?

Here is my code snippet:

<html>
    <head>
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <style>
            .firstElementClass, .secondElementClass{
                float:left;
                width: 30%;
                text-align: center;
            }   
            .firstElementClass{
                background: red;
                overflow: auto;
            }
            .secondElementClass{
                background: yellow;
            }
            .rowClass{
                margin-top: 20px;
                clear:both;
                padding: 20px;
            }
        </style>
    </head>

    <body>
        <div class="rowClass">
            <div class="firstElementClass">11aa<br>11aa<br>11aa<br>11aa<br>11aa<br>11aa</div>
            <div class="secondElementClass">11bb<br>11bb<br>11bb<br>11bb</div>
        </div>
        <div class="rowClass">
            <div class="firstElementClass">AA</div>
            <div class="secondElementClass">22bb<br>22bb<br></div>
        </div>
        <div class="rowClass">
            <div class="firstElementClass">AA</div>
            <div class="secondElementClass">33bb<br>33bb<br>33bb<br>33bb</div>
        </div>
        <div class="rowClass">
            <div class="firstElementClass">AA</div>
            <div class="secondElementClass">44bb<br>44bb<br>44bb</div>
        </div>
    </body>

    <script>
        $(function(){
            $(".firstElementClass").each(function(){
                $(this).height($($(this).parent().find(".secondElementClass")[0]).height());
            });
        });
    </script>

</html>

Answer №1

Instead of using the float property, consider implementing a tabular layout like CSS Grid or utilizing display: table.

By adopting this approach, you can ensure that all divs in a row have consistent heights based on the tallest div. To fine-tune the alignment with the last div, apply position: absolute to the contents of the first div, allowing it to occupy remaining space after other elements are positioned (observe the additional .cellValue divs inside .firstElementClass in the given example).

.myTable {
    display: table;
    border-spacing: 20px;
    width: 60%;
}
.rowClass {
    display: table-row;
}
.firstElementClass,
.secondElementClass {
    display: table-cell;
}

.firstElementClass {
    position: relative;
    width: 50%;
    background: red;
}
.firstElementClass .cellValue {
    position: absolute;
    top:0;left:0;bottom:0;right:0;
    overflow: auto;
}

.secondElementClass {
    background: yellow;
}
<div class="myTable">
    <div class="rowClass">
        <div class="firstElementClass">
            <div class="cellValue">11aa<br>11aa<br>11aa<br>11aa<br>11aa<br>11aa</div>
        </div>
        <div class="secondElementClass">
            <div class="cellValue">11bb<br>11bb<br>11bb<br>11bb</div>
        </div>
    </div>
    <div class="rowClass">
        <div class="firstElementClass">
            <div class="cellValue">AA</div>
        </div>
        <div class="secondElementClass">
            <div class="cellValue">22bb<br>22bb<br></div>
        </div>
    </div>
    <div class="rowClass">
        <div class="firstElementClass">
            <div class="cellValue">AA</div>
        </div>
        <div class="secondElementClass">
            <div class="cellValue">33bb<br>33bb<br>33bb<br>33bb</div>
        </div>
    </div>
    <div class="rowClass">
        <div class="firstElementClass">
            <div class="cellValue">AA</div>
        </div>
        <div class="secondElementClass">
            <div class="cellValue">44bb<br>44bb<br>44bb</div>
        </div>
    </div>
</div>

Answer №2

Using the power of flexbox, it is possible to make two divs have equal heights.

.rowClass{
display:flex;
}
.firstEl, .secondEl{
flex:1;}

If this solution does not meet your needs, please feel free to leave a comment below.

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

Scrolling to a specific element using jQuery after a specified delay has

On my website, I have a page with an autoplaying video located at . My goal is to implement a feature where once the video completes playing after a specific duration, the webpage will automatically scroll down to the text section. This scroll action sho ...

Optimizing Bootstrap 4 Flex-Row Dimensions

Issue: I'm currently working on a relatively intricate ListGroup using bootstrap v4. The .flex-row div is displaying some mysterious white space at the bottom, and I need assistance in eliminating it. Attempts Made So Far: I've experimented w ...

No data being displayed or returned from API when using async await

My Ionic 6 + Angular 14 application is currently facing an issue with displaying data retrieved from an API... I have implemented a service to fetch the data from the API and then called this service in the component. The app compiles without any errors a ...

Utilize web scraping with Python 3 to extract data from the form

Currently, I am working on a project that involves web scraping. However, I am encountering difficulties when trying to extract information from an Iframe form, specifically the name, position, and company fields. Below is the code snippet that I have bee ...

Revamping Existing Styles: Making a Statement with `:after`

In my asp.net mvc application, I am facing an issue where the * symbol, representing a required field, and the ? symbol, representing help, are not both being displayed. The ? symbol seems to be overriding the * symbol, resulting in only one symbol being s ...

Dropdown Pattern with React CTA Modal

While using MaterialUI's ButtonGroup for a dropdown menu, I encountered an issue trying to set up a series of CTAs that are easily interchangeable within it. The goal is to have all components reusable and the choices in the dropdown dynamic. const C ...

Best practices for implementing the map function with TypeScript

I'm currently working on mapping types in a DB using the Map function in JavaScript. This is my first time trying to do this, and I'm eager to learn but I've hit a roadblock. Here is the structure of the DB: const db = { data: [ { ...

iOS now supports fully transparent tooltips and modals, offering a sleek and

I am currently working on developing tooltips and modals for an iOS app using react-native. The problem I am encountering is that the background of the tooltips and modals is not transparent, although it appears correctly on the Android version of the app. ...

When you press the back button and navigate to a different page, the scroll position will remain unchanged

I'm facing an issue with scrolling on my angularjs app. Currently, the app consists of 2 pages: The first page displays a list of customers, where you can select one to view their details. The second page is a list of companies, following a similar s ...

What is the method for including the sources directory within the require() scope in npm?

Upon examining my directory structure, the following layout is revealed: package.json /src a.js /test test_a.js The contents of package.json are as follows: { "name": "foo", "scripts": { "test": "mocha" }, "devDependencies": { "mocha ...

Exploring nested arrays of objects and applying value constraints

Is there a way to iterate through an array and display only 5 items at once, with the option to call a function on click that will add another 20 items? I have an object structured like this: let someObject = [ { data: [ { id: 123, ...

Executing a JavaScript function from the form attribute value in HTML: Steps to follow

<html> <head> <script type="text/javascript"> function add() { num1 = 20; num2 = 30; total = num1 + num2; return total; } </script> & ...

Enhance the CSS styling for the React-Calendly integration in a React project

I am trying to customize the CSS of an Inline Widget called React Calendly. I have attempted to use React Styled Component Wrapper, Frame React Component, and DOM javascript but unfortunately, the design changes are not reflecting as desired. Specifically, ...

Can the conventional HTML context menu be swapped out for a link context menu instead?

Currently, I am working on developing a custom linkbox component that is similar to the one found on this page: Here is an example of the code: export const Linkbox = ({}) => { const linkRef = useRef(null); return ( // eslint-disable-next-l ...

The variable req.body.Dates has not been declared

I am currently working on a project that involves dynamically populating two drop down menus using SQL Server. Depending on the selected items, I need to load a specific ejs template using AJAX. My goal is to load data based on the selected criteria. For e ...

What are the key distinctions between DOCS and PSD base64 URLs?

My current challenge involves displaying a preview of attachments. I want to show an IMAGE SVG preview for image attachments and a PDF preview for PDF attachments, both based on their respective base64 formats. For example, I am currently using the split m ...

A method for arranging an array of nested objects based on the objects' names

Recently, I received a complex object from an API: let curr = { "base_currency_code": "EUR", "base_currency_name": "Euro", "amount": "10.0000", "updated_date": "2024 ...

Get both div tags within a single find_all function call in Beautiful Soup

Is there a way to extract multiple HTML div tags using a single "soup.find_all" function in beautifulSoup? The divs are labeled as "event odd" and "event even" on the webpage, and I want to iterate through all of them. Here is an example of the webpage co ...

ASP.NET DIV is experiencing issues with Jquery functionality, causing it to flicker and remain fixed in place

As a beginner in JQuery, I am facing an issue with my code in asp.net. Whenever I click on linkbuttons, the behavior is not as expected. The div flickers and does not change its direction. My goal is to move the DIV left or right by 200px, but no matter wh ...

`Some Items Missing from Responsive Navigation Menu`

Hey there! I'm currently diving into the world of responsive design and I'm attempting to create a navigation bar that transforms into a menu when viewed on a mobile device or phone. Everything seems to be working fine, except that not all the na ...