How can the height of nested rows in side-by-side cards be adjusted to match each other?

I am facing a situation where I have two side-by-side cards with nested rows containing dynamic content. My goal is to make sure that the height of each row in the first card matches the height of the corresponding row in the second card.

To achieve this, I need to calculate the heights as follows: height of A1 = max(height of A1, height of A2), height of A2 = max(height of A1, height of A2), height of B1 = max(height of B1, height of B2), height of B2 = max(height of B1, height of B2).

The code snippet for my setup looks like this:

<div class="row">
    <div class="col-lg-6 mb-3">
        <div class="card">
            <div class="card-body">
                <div id="a1" class="row">
                    ...
                </div>
                <div id="b1" class="row">
                    ...
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-6 mb-3">
        <div class="card">
            <div class="card-body">
                <div id="a2" class="row">
                    ...
                </div>
                <div id="b2" class="row">
                    ...
                </div>
            </div>
        </div>
    </div>
</div>

Currently, my layout looks like this:

https://i.sstatic.net/GbcYs.png

What I am aiming for is to achieve a layout similar to this:

https://i.sstatic.net/zBwSG.png

Is there a way to accomplish this using only Bootstrap 4 and CSS?

Answer №1

To achieve the desired layout with Bootstrap cards, you'll need to get creative. Try using custom borders on a CSS grid instead, where one card spans two rows. By omitting wrapper elements that disrupt row flow, you can create the illusion of separate cards.

For mobile responsiveness, consider adding a media query at your preferred breakpoint (such as 992px). This will ensure the "cards" stack correctly on smaller screens. While Bootstrap lacks built-in support for CSS grid, implementing custom styles allows for better control and maintains semantic order in your content structure.

Check out the provided CSS code snippet for styling guidance, and remember to organize your document content logically with headings and paragraphs. This approach ensures accessibility and readability across different viewport sizes.

Answer №2

To achieve this functionality, I will be utilizing jQuery library.

var calculateMaxHeight = function(elements){
    return Math.max.apply(null, elements.map(function ()
    {
        return $(this).height();
	}).get());
}

$('.a').css('height', calculateMaxHeight($(".a")));
$('.b').css('height', calculateMaxHeight($(".b")));
.col-6{
	border: 1px solid black;
	padding: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row p-2">
  <div class="col-6">
    <div class="row match-height">
      <div class="col-12 ">
        <div class="card a">
          <div class="card-body">
            #A1
            <br>
            Extraa
          </div>
        </div>
      </div>
    </div>
    <div class="row match-height">
      <div class="col-12 ">
        <div class="card b">
          <div class="card-body">
            #B1
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="col-6">
    <div class="row match-height">
      <div class="col-12">
        <div class="card a">
          <div class="card-body">
            #A2
          </div>
        </div>
      </div>
    </div>
    <div class="row match-height">
      <div class="col-12">
        <div class="card b">
          <div class="card-body">
            #B2
          </div>
        </div>
      </div>
    </div>
  </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 is the best way to navigate to a different HTML file in a MEAN Stack application?

This particular software communicates with MongoDB. Within Index.html, there is a button: View When the "View" button is clicked, it triggers a function called view() that requests data from MongoDB and displays it on the same page Index.html. The view( ...

Utilize varied CSS files for distinct sections within two different subdirectories

In my Angular application, I have created two sub-roots under the main app root: websiteMaster and systemMaster. I want the CSS files of the website to be loaded only when a user is logged in, and the CSS files of the systems to be loaded only when a us ...

Display a video-thumbnail in place of a static image when sharing a link

When setting up my website, I made sure to include OpenGraph metadata to customize how it appears when shared on social networks and messaging apps. This includes specifying the thumbnail, title, and description: <meta property="og:title" content="Titl ...

What is the best way to align line items containing both text and images vertically on a page?

I'm struggling with aligning the text vertically in my menu items which are created using ul and li tags. The default _layout.cshtml page is being used, but the text is aligned at the top instead of the middle. I attempted to use the vertical-align a ...

Guide on how to organize a list into three columns using a single ul tag

Is there a way to split a ul list into 3 columns using CSS? This is how my HTML structure looks like: <ul> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> <li> ...

Guide on how to use JavaScript to upload media stream recorder blobs onto YouTube using the YouTube API

Using mediastreamrecorder.js, I am successfully recording video in 5-second blobs within the ondataavailable function. Here is an example of the code: mediaRecorder.ondataavailable = function(blob) { blob=blob; }; While I am a ...

Navigating a mobile-friendly menu anytime!

I'm in the process of creating a responsive "hamburger" menu for mobile devices. The HTML code I have implemented is as follows... .menu_closed { color: red; } .menu_open { color: blue; } <script src="https://ajax.googleapis.com/ajax/libs/jq ...

Transforming hexadecimal color codes into spans

I've been experimenting with regex patterns to transform hexadecimal colors into spans. Take this example: #FF0000Hello#00FF00There <span style="color: #FF0000">Hello</span><span style="color: #00FF00">There</span> ...

Sending data to CSS file within Django

Can variables be passed in CSS files, similar to HTML files? For example: In views.py: def home(request): bgcolor = "#999" ... ... In the CSS file: body { background-color : {{bgcolor}}; } If it is indeed possible, could you please pro ...

How can I delete a CSS class from an element?

Is there a way to remove the "ui-widget-content" class from the code below? It appears in multiple places throughout my code. Here is an example snippet: <pre> <form id="clientForm"> <div id="clientData"> <div id="gbox_grid_1" class=" ...

Adding a fresh, spacious breakpoint in Bootstrap 4 with just CSS

I encountered an issue when the extra large Bootsrap 4 max container width, set at 1140px, was not wide enough for my needs. I aim to ensure that my websites look good on large monitors as well, but the bootstrap grid is too narrow. The most straightforwar ...

Ensure all input-group-addons have equal width with Bootstrap 4

Can the width of all prepend add-ons be made the same? For example, in this image, I would like Email, License Key 1, & License Key 2 to have equal lengths. Is this achievable? If I were to forgo add-ons and only use regular labels and a form grid, it wo ...

Centering input boxes within a panel body through the implementation of CSS grid technology

I am facing an issue with implementing a form input box inside a panel body using CSS grid layout. The input box seems to overflow outside the panel body, which is not the desired behavior. Strangely, the code works fine with regular CSS, but I prefer usin ...

Is there a way to include an instance variable as a CSS rule in Rails?

Assume I have the following ActiveRecord class: ......... store :skin_properties, accessors: [ :background_color, :font_size, :font_family, :color ] ......... This class saves values in the format shown below: {"background_color"=> ...

Customizing Nav Bar Color in Bootstrap

Is there a way I can change the font color of the Nav Bar to white? I'm having trouble figuring out which CSS class to use for 'Contact', 'Portfolio', and 'About' as 'Navbar' is already white due to Bootstraps&a ...

What could be causing my toggle button to not trigger my responsive menu in JavaScript?

How can I make the navigation menu open when clicking the hamburger menu in the first list item? I tried using JavaScript to target the `nav-list` class in the second list item, but nothing happens on click. Initially, I had the hamburger icon and links i ...

Retrieve data from multiple inputs with the same name in a Spring MVC form

I have a JSP page with input fields that can be dynamically added or removed. I need to map these input fields to the @ModelAttributes in Spring MVC, but I'm unsure of how to do this. I want to submit a form that includes: <form enctype="multipar ...

A guide on utilizing buttons within ion list items to execute actions independently for both the button and the list item

Currently, I have a list item with a button that is displayed based on a certain condition. My issue is that when I click the button, a popup should appear, but instead, it also navigates to the next page in the background. Can someone help me figure out h ...

When hovering, transition occurs unless the cursor is over a specific element

I have created a small box element with a close button that looks like this: ___ | X| ‾‾‾ In order to make it more interactive, I applied some CSS so that when hovered, the box expands like this: ___________________ | X| ‾ ...

What is the best way to restrict the selection of specific days of the week on an HTML form date input using a combination of JavaScript, React, and HTML?

I need help customizing my Forms Date Input to only allow selection of Thursdays, Fridays, and Saturdays. I've searched for a solution but haven't been successful so far. Is there any JavaScript or HTML code that can help me achieve this? Below ...