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

Component template using Knockout.js and RequireJS for HTML widgets

Trying to implement the widget example for knockout from here. Unfortunately, I am having issues loading the template from an external HTML file using requirejs. ko.components.register('like-or-dislike', { template: { require: &apos ...

Unusual thin border of white pixels on IE 9

Having some issues with border radius in IE-9. The left border is showing white pixels. Any thoughts on why? This problem is only in IE 9, other browsers look fine. Here is the code snippet: <ul class="tags clearfix"> ...

The conflict arises when importing between baseUrl and node_modules

I am currently working on a TypeScript project with a specific configuration setup. The partial contents of my tsconfig.json file are as follows: { "compilerOptions": { "module": "commonjs", "baseUrl": &quo ...

What is the best way to align these Iframes in the center?

This is the html <div class="main_content"> <section class="episodio"> <article class="contenedor_episodios"> <h2>Episodios</h2> <div class="episodio_spotify" ...

Navigating through a URL to grab a specific JSON object: a step-by-step guide

When I receive a json from a URL containing numerous objects, how can I extract only the slugs provided in the json below? I am open to solutions using either PHP or JavaScript. On one hand, I need to understand how to specifically retrieve the desired ob ...

Adding nested JSON data to MySQL using NodeJS

My current challenge involves using Node.js to INSERT JSON data into a MySQL database. Everything runs smoothly until I encounter nested values within the JSON structure. Here is an example snippet of my JSON data: var result2 = [{ "id": 89304, "employe ...

Strategies for injecting data into VueJS components after the page has fully loaded

My project utilizes Vue.js and Nuxt.js, and within the settings page, users can modify their personal settings. This single page allows users to navigate between tabs. <template> <div class="account-wrapper"> // Code content he ...

I possess 9 captivating visuals that gracefully unveil their larger counterparts upon being clicked. Curiously, this enchanting feature seems to encounter a perplexing issue within the realm of web browsing

<style type="text/javascript" src="jquery-1.11.0.min.js"></style> <style type="text/javascript" src="myCode.js"></style> </body> //jquery is within my site directory on my desktop $(document).ready(function(){ //note: $("#ar ...

Unable to utilize material tabs in this situation

Discovering the material tabs feature at https://material.angular.io/components/tabs/api#MatTab got me excited to implement it in my project. After adding the suggested import, I encountered an issue where I couldn't find the module "@angular/materia ...

Revert the Vue State When Navigating Back in the Browser

Background In our checkout process, we redirect to Stripe after creating an order object through an API request. To prevent users from clicking the "checkout" button multiple times during this process, we toggle a variable value to show a loading icon whe ...

Hide the footer DIV in CSS by making it disappear with a height of 100%

I'm currently facing an issue with my CSS layout. Despite managing the problem in IE and seeing everything work well, the footer container has mysteriously disappeared! I've attempted to recode the CSS without success, even after trying for days ...

Mistakes found in the 'com/badlogic/gdx/backends/gwt/GwtApplication.java file while working on an HTML 5 project

Just recently, I discovered the amazing framework known as Libgdx. As a newcomer to this framework, I am encountering a few challenges. After cloning the entire repository and running the gdx-setup-ui.jar file, I obtained 4 basic starter projects (my-gdx-g ...

Steps to execute a JSON file once another JSON has been successfully loaded

I'm facing a similar challenge to the one presented in this question. However, I need to load an additional JSON file after the when() method is executed. Essentially, I want to retrieve JSON data within the when() function and then use that informati ...

What are some tips for integrating Bluebird into Angular frameworks?

I attempted to integrate Angular with Bluebird promises: Here is the HTML code snippet: <body ng-app="HelloApp"> <div ng-controller="HomeController">{{name}} {{also}}</div> </body> The corresponding JavaScr ...

A guide on importing images into a CSS file with Reactjs

Currently, I am utilizing Reactjs (Nextjs) and have placed my images folder within the "public" directory. In my "style.css" file, I adjusted the path for the image, but unfortunately it is not displaying. Can you please help me identify where I may have ...

I'm confused, I installed the module but it's still showing an error message saying it can

I've been working on coding a discord music bot, and here is the code I have so far: const config = require('config.json') const Discord = require('discord.js'); const ffmpeg = require('ffmpeg-extra') const client = new ...

Having trouble with the page redirection issue? Here's how you can troubleshoot and resolve

My goal is to restrict access to both the user home and admin dashboard, allowing access only when logged in. Otherwise, I want to redirect users to the login or admin login page. import { NextResponse } from 'next/server' import { NextRequest } ...

Struggling to make EJS button functional on the template

I am currently facing an issue with a loop that populates a webpage with multiple items, each containing an image, text, button, and a unique ID associated with it. Although I have written a function to retrieve the ID and plan name when the button is clic ...

What steps should I take to show a particular set of data upon selecting a checkbox component?

I have a table with a column named status, which can be in progress, pending, or dispensed. My goal is to filter the data based on the checkbox that is selected above the table. For instance, if I check the "pending" checkbox, only the data with the pendi ...

Transferring specific form data through a POST request

I am attempting to transfer specific values from a form to PayPal once the form is submitted. Below is the code for my form: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="form-horizo ...