Create a four-div grid that dynamically adjusts its height to match the tallest element

Seeking a way to design a grid layout that automatically adjusts the height of child divs to match the tallest one in each row. In a 2x2 grid scenario, where each div contains text, the div with the most content should determine the height for the rest.

I've managed to make the div heights align within each row using display: flex; and flex-wrap: wrap;, but now I need all rows to have matching heights.

Wondering if this goal can be achieved through CSS alone or if jQuery will be necessary?

<section class="grid two">
    <div>
        <div>
            <p>The top left div</p>
        </div>
    </div>
    <div>
        <div>
            <p>The top right div</p>
        </div>
    </div>
    <div>
        <div>
            <p>The bottom left div</p>
        </div>
    </div>
    <div>
        <div>
            <p>The bottom right div</p>
            <p>This has more text and should stretch others to match its height.</p>
        </div>
    </div>
</section>

Current CSS code:

.grid {
    display: flex;
    flex-wrap: wrap;
}

.grid > div {
    box-sizing: border-box;
    display: flex;
    flex-directon: column;
    padding: 10px;
    width: 50%;
}

.grid > div > div {
    background: #EEE;
    flex: 1 1 auto;
    width: 100%;
}

See live example on Fiddle: http://jsfiddle.net/g41xas6w/


Update 1: Fixed height not an option for two reasons:

  1. The client may edit text, so the height is unknown.
  2. This site is responsive, requiring extensive adjustments to maintain proper display height.

Answer №1

Check out this helpful link: http://brm.io/jquery-match-height/

This website introduces a jQuery plugin that can assist you in achieving the desired effect.

Answer №2

Even though your solution may work for the 4 DIVs in a single horizontal line, it's important to note that changing the width could affect how they look. If you want to achieve a 2X2 layout where all 4 DIVs have the same height dynamically, CSS alone may not be sufficient. You may need to use scripting to manage this or simply set a fixed height, which would remove the dynamic aspect of it!

Answer №3

By inserting a height attribute inside the .grid > div selector, such as setting it to 200px, you can ensure that all elements within this container will have a height of 200px.

.grid > div {
    box-sizing: border-box;
    display: flex;
    flex-directon: column;
    padding: 10px;
    width: 50%;
    height: 200px;
}

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

Bootstrap 4 allows for the use of the d-lg-flex class to create a flexible column layout

I'm facing an issue with my table on smaller screens where some columns need to be hidden. Here's the code snippet I'm using: <table> <tr> <td>Normal 1</td> <td class="d-lg-flex d-none">hidden 1</t ...

Is the connection still open post test completion?

I have a straightforward postgres.js wrapper file. const pg = require("pg") const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL }); function shutDown() { return pool.end() } module.exports = { end: pool.end, close: shutDown ...

Iterating over an array of objects and executing asynchronous operations

I am currently working with NextJS and managing an array of assets (images) within my state. The task at hand is to post these images to the API. To accomplish this, I have a specific object that handles the posting process using the following syntax: let ...

A dynamic 3-column layout featuring a fluid design, with the middle div expanding based on the

Sorry for the vague title, I'm struggling to explain my issue clearly, so let me elaborate. I am using flexbox to create a 3-column layout and want the middle column to expand when either or both of the side panels are collapsed. Here is a screenshot ...

Exploring the process of assigning responses to questions within my software program

I am looking to display my question choices as radio buttons in a modal window. I have tried several solutions without success. Here is my question module: import questions from "./Data"; const QuestionModel = () => { return ( <div cl ...

Access a SQL database to retrieve a data value and seamlessly integrate it into an HTML document with the help of node

I am new to web development and currently facing a challenge in displaying SQL content on an HTML page. My stack includes Node.js, Express, and SQLite3. I have a folder named public containing HTML, CSS, and JS files. The objective is to retrieve a varia ...

The value of NUMBER_VALUE cannot be transformed into a String while trying to update the Item

I've encountered a strange issue with DynamoDB where I'm unable to update an item. Below is the command I'm using: TableName: 'UserTable', Key: { UID: { S: 'h4XJj3YRxZiF7TDcGkxAhc' } }, UpdateExpression: 'SET numRat ...

I am looking to implement a feature that will disable unchecked checkboxes based on certain conditions in a

Upon selection of an option from a dataset, an API call is triggered. The API response includes an object with a nested array, whose values are listed as checkboxes. Additionally, the API returns a key named choose(const name primaryMax) indicating the max ...

JavaScript/AJAX Functionality Only Operates Correctly During Debugging

I am currently facing an issue with dynamically populating a website. The code works perfectly when I step through it, but it fails to work as intended on page load. Here is the relevant code snippet: <body onload="populate_all(string)";> function ...

v-for based on element type

I'm facing an issue with a basic vue.js application. I have a list of referees and I need to iterate through that list and add data from an ajax request to each item. Strangely, when I try to display the list using v-for on a span element, it works pe ...

Adjust the hue of the SVG sprite

I am currently using an SVG sprite as the background image for my project: .naar-logo { background: url(images/salt_sprite.svg) no-repeat -8px -78px; width: 36px; height: 49px; position: relative; top: 38px; } <div class="naar-logo ...

Is there a way to showcase English and CJK text vertically with CSS?

This particular issue was first discussed 14 years ago on Stack Overflow, but a definitive solution still remains elusive. My goal is to have both CJK and Latin characters displayed vertically, with their bottoms facing the right side. While writing-mode ...

Is there a way to implement coding similar to jQuery in the C programming language?

Can C language recreate jQuery functionality? Imagine we have a structure (similar to HTML): <div id="mymute"> <div> </div> <div> <img src="/agents/sleeping.png" /> </div> <div> </div> </div> ...

Launch the sidebar window using jQuery

I've been attempting to create something similar to this. However, I'm having trouble replicating it exactly. What I've attempted I've managed to create a replica, but the issue is that the buttons are fixed. Could setting the left ...

A guide on efficiently incorporating a php variable into json format, then transferring it to ajax

My Sample Code var ajaxResponse = xmlhttp.responseText; //ajax response from my php file jsonData = JSON.parse(ajaxResponse); alert(jsonData.result); And in my PHP Script $resultValue = 'Hello'; echo '{ "result":"' . $result ...

Allow a nested element to overlap its container instead of displacing it

As a CSS newbie writing a React app, this question may be the easiest one in the history of stackoverflow. I'm trying to make the image element overlap the header instead of pushing it down. In this basic HTML example, the cat image is stretching the ...

What is causing the "bad control character" error when attempting to parse JSON with Chinese characters included?

I currently have a typical HTML5 client/server arrangement in place. The server side mainly consists of Java, while the client side is built with JavaScript. I make use of ajax to send queries and receive responses. So far, everything has been running smoo ...

Guide on merging an array in the state of a React Component

I've been working on developing a timesheet app. In the index.js file, I have set up the rendering of a table where the rows are populated from a children array that reads the state to ensure it stays updated. The function AddRow() is functioning prop ...

Generating an ordered array that begins at a specified value and has a specific number of elements

Searching for a stylish solution (without using a for loop) to generate a sequential array in Javascript that begins with a specific number and contains a set number of elements. For instance: Commencing with 2017 and including 4 items would appear as fol ...

What could be causing my webpage content to be partially hidden by my CSS menu?

I am currently working on my website which features a CSS menu with my website logo, a text box, and a dropdown. Additionally, I have created a login form using a table like the one shown below. <p>&nbsp;</p><table width="40%" border="0 ...