Twisting columns causing a problem with rows of consistent height

I am facing a challenge with my CSS HTML code where I need to create a snaking column of labels/values. If one of the values on the left spans two lines, the corresponding one on the right should also adjust its height accordingly.

Currently, I can only achieve this by setting a specific height for the outer container, but I want it to dynamically adjust to the content height.

 <!DOCTYPE html>
<html lang="zxx">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  <style>     
        .container {
            display: flex;
            flex-wrap: wrap;
            height: 100%;
        }

        .row {
            flex: 1 0 14rem;
            display: flex;
            flex-direction: column;
        }


        .column {
            flex: 1;
            border: 1px solid black;
            padding: 10px;
        }


    </style>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="column">1) Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque labore fuga iste, ut nam quod voluptates vel! Ex est obcaecati itaque architecto totam, sapientea...
            <div class="column">2) Lorem ipsum dolor sit, amet consectetur adipisicing elit. Vel debitis voluptas fugiat vero dolorum consequuntur ullam magnam perspiciatis, veniam culpa, quos ex voluptatem. Vitae offi...
        </div>
        <div class="row">
            <div class="column">3) Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repudiandae vel aspernatur sequi facilis earum assumenda dolorem molestiae sunt, numquam autem. Maiores ullam odit quae velit ex? Iu...
            <div class="column">4) Lorem ipsum dolor, sit amet consectetur adipisicing elit. Ab ducimus blanditiis similique saepe necessitatibus placeat, expedita ratione quisquam consequatur a dicta exercitationem molestias, ...
        </div>
    </div>
</body>

</html>

For reference, here is the desired layout: https://i.sstatic.net/nXtcr.png

Answer №1

To easily create a 2-column grid using CSS Grid, simply use the code

grid-template-columns: repeat(2, 1fr);
. This eliminates the need for sub-containers to define rows. The cards will automatically adjust their height to match the tallest element in the row:

.container {
  grid-auto-flow: column;
  display: grid;
  grid-template: repeat(2, auto) / repeat(2, 1fr);
  grid-gap: 2rem; 
}

.card {
  border: 2px dashed red;
  padding: 10px;
}
<div class="container">
  <div class="card">1) Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque labore fuga iste, ut nam quod voluptates vel! Ex est obcaecati itaque architecto totam, sapiente aliquam veniam repudiandae assumenda. Provident, nostrum? Officiis expedita,
                repudiandae dignissimos veniam sit architecto voluptate nam est earum? Fugiat natus exercitationem hic at, iusto dolore soluta distinctio illo voluptates ratione? Distinctio, veniam deserunt libero aspernatur optio obcaecati. Unde est
                doloribus iste aut quod perspiciatis, fugit necessitatibus magni animi excepturi eaque dolor sequi, ipsa rem ipsam maiores facilis dignissimos nam. Voluptate cum quo eius. Impedit quisquam optio vero! Voluptatem magni, nisi quo consequuntur
                itaque totam quia voluptate repellendus alias? Laboriosam possimus unde debitis facere numquam asperiores dolor aliquam amet nam, totam quaerat voluptate modi libero maiores, similique magni. Ipsam cupiditate autem est aliquam, magnam
                quasi perspiciatis minus consequatur alias dolor quibusdam, distinctio, adipisci facere impedit minima necessitatibus. Reiciendis ducimus corrupti, ab quis mollitia aspernatur cum dignissimos deleniti incidunt.</div>
  <div class="card">2) Lorem ipsum dolor sit, amet consectetur adipisicing elit. Vel debitis voluptas fugiat vero dolorum consequuntur ullam magnam perspiciatis, veniam culpa, quos ex voluptatem. Vitae officiis eius illo doloremque natus quas. Quis neque id repudiandae
                ut sunt! Voluptate eligendi odio placeat enim quod laudantium! Dolorem velit sapiente dolorum vero? Iusto ad nam commodi voluptatem? Distinctio quas voluptate alias quo consectetur laboriosam. Nam beatae suscipit architecto quaerat id
                esse sit, aliquid consectetur enim expedita. Rem obcaecati ducimus cupiditate expedita illo perspiciatis, itaque ipsam ipsa placeat delectus excepturi corrupti dicta non voluptas ex. Ea sint dignissimos itaque ducimus adipisci nemo debitis
                totam culpa laudantium. Molestias ipsum eaque temporibus odio excepturi, libero ipsam vero sequi voluptate quis repudiandae quod corporis. Repellat commodi saepe omnis!</div>
  <div class="card">3) Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repudiandae vel aspernatur sequi facilis earum assumenda dolorem molestiae sunt, numquam autem. Maiores ullam odit quae velit ex? Iusto enim dicta voluptates. Alias excepturi vero
                voluptatibus, saepe error iusto laudantium illo nemo veritatis iure quidem. Maxime cum eveniet nihil voluptas odio obcaecati, accusamus voluptate sed autem? Illo adipisci dignissimos esse reprehenderit labore.</div>
  <div class="card">4) Lorem ipsum dolor, sit amet consectetur adipisicing elit. Ab ducimus blanditiis similique saepe necessitatibus placeat, expedita ratione quisquam consequatur a dicta exercitationem molestias, neque, error totam reiciendis veritatis vitae
                nihil!
            </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

Steps to showcase a form on a webpage using a button

My webpage features an HTML table with a table navigation bar that allows users to add items or inventory. However, when the "add item" button is clicked, a form appears below the table instead of on top of it. I want the form to display itself right on to ...

The Ultimate Guide to Setting up SVG's Viewbox, Width, and Height for Scalability and Responsiveness

As a beginner in SVG design, I find it challenging to scale, zoom in/out with the container and ensure responsiveness across different devices. I wonder if it's possible to create an SVG that adapts to all device sizes and effectively handles browsin ...

Fixed and percentage widths in horizontal divs for children, with the percentage width exceeding the desired size

Here's the code snippet I'm working with: http://pastebin.com/W3ggtgZB. The body of this code can be found here: http://pastebin.com/2tkmhnfW. My goal is to create a div containing two child divs. One child div should have a fixed width, while t ...

Creative ways to position and underline text using CSS

I'm having difficulty with positioning the text and extending the underline to the edge of the screen in order to create a specific visual effect. .header { height: 550px; width: 100%; background-color: #1F2041; position: relative; } . ...

Iterating through data to showcase vertical columns for a specific time span of 3 years, highlighting only the months with available data

If data has been available for every month over the past 3 years, I need it to be displayed in a specific format. You can refer to this example fiddle for reference: https://jsfiddle.net/bthorn/ncqn0jwy/ However, there are times when certain months may no ...

Enhance the appearance of an HTML select tag for optimal viewing on iOS Safari browser for

I have customized three dropdown lists using CSS. I set the style as follows: .dropdown{ font-size: 20px; background: white; border:none; position: relative; } While the dropdowns appear fine in Chrome, there seems to be a slight differen ...

Clicking on text will open a popup div using HTML and CSS

I am looking to create a popup div instead of a popup window for my 'About' picture/page with the current button. Here is an example: ...

Any suggestions on how to avoid code repetition using Jquery?

This code allows me to create a menu for my gallery. By setting the .masonry # elements to display:none, I can use the corresponding ID in the menu to show the correct gallery when clicked. However, there is a lot of repetitive code in this implementatio ...

"Which is better for handling form data: using $.ajax with hidden fields or PHP form

Creating a streamlined admin tool using PHP and JQuery to handle image data management is my current project. The main goal of this utility is to enable an admin to retrieve images from a database, view associated tags for each image, and add new tags as n ...

Eliminate spacing between divs of varying heights

I'm facing an issue with my small gallery of images. Despite having the same width, they vary in height causing the second row to start below the tallest image from the previous row. How can I eliminate these empty spaces while still maintaining the v ...

Having trouble with the Bootstrap dropdown? The unordered list isn't dropping down as expected. What could be the issue?

I am facing an issue with two dropdowns in my template. Both of them are not functioning properly, one with Thymeleaf attributes and the other without. I have not attempted to change the CSS properties as they were working fine before but now they do not ...

Positioning Google Chart in HTML

I'm attempting to arrange three distinct Google pie charts in a horizontal layout. At present, I have applied inline CSS for formatting purposes. Oddly enough, the third chart always ends up on a separate line. Could anyone kindly point out my error? ...

Issue with Laravel project: Views are not updating after code changes

Despite my efforts to clear and refresh everything, my Laravel project is not reflecting the changes. I have attempted clearing the view and deleting files manually. I also ran cache:clear, changed the root folder name, but the project continues to displa ...

Is there a way to perfectly align the content at the center of the section?

I'm trying to ensure that the content in each section is centered. What is the correct way to achieve this? Should I focus on the containers/rows for alignment? I have tried placing the alignment properties in different elements (section, container, r ...

The Iframe contains its own scroll feature within the body

I am facing an issue with displaying an Iframe inside a modal. The challenge is that the content within the Iframe varies in height, making it difficult to set a fixed height for the Iframe itself. When I try setting the height to 100%, the result is a 150 ...

Secret button concealed within a div, revealing only a helpful hint

On my website, there is a list of items where each item is represented by either a picture or a name enclosed in an <article> tag. Here is an example of how it looks: <article class="client-card"> <p><img width="211" height="106" ...

Steps to position images and text side by side in a grid layout

Trying to create a grid layout with profile images and text aligned next to each other, but struggling with CSS. If anyone could provide some guidance, that would be greatly appreciated! Here is the HTML code snippet: <div class="col-sm-3" ...

Show the chosen choice in a select dropdown with custom text using Angular

I successfully implemented this code snippet to display a dropdown list of countries and their phone codes. However, I encountered an issue where I need to only show the selected option's code without the country name. The first screenshot shows how i ...

Having trouble getting my HTML file and CSS styles to render properly in Chrome

Currently, I am in the process of developing a website and facing challenges with displaying CSS properties accurately. Despite seeking input from friends and users, my HTML file is not rendering as expected within various browsers such as Chrome, Edge, an ...

Creating a data visualization in JavaScript or jQuery without relying on pre-built graph libraries

Hey there, I could really use some assistance. Does anyone know if it's possible to create a line graph in JavaScript or jQuery without relying on any external libraries? It doesn't have to be visually stunning, just functional. If you have any i ...