Creating equal height for two element children using CSS

As I construct a pricing table, I'm faced with the challenge of ensuring that the child elements within each row are of equal height. The website is built on WordPress and utilizes a page builder, resulting in the row fields being positioned differently. In essence, it does not have the structure of a traditional table, making it tricky to achieve uniform row heights.

I attempted to utilize the matchHeight library for this purpose, but encountered difficulties in applying it specifically to the child elements within each column.

For reference, here's a demo available on CodePen

$('.col > div').matchHeight();

Answer №1

Have you considered incorporating the CSS property min-height into the .col > div selector? You can achieve this by adding

min-height: calc(100% / var(--col-rows));
, where var(--col-rows) is a CSS variable set in your stylesheet using data from the DOM via JavaScript.

.col {
/* target the div within the .col in your scss */
> div {
    /* Using the --col-rows variable to calculate 
       the number of elements in each row so they occupy equal space */      
    min-height: calc(100% / var(--col-rows));

To dynamically set the CSS variable based on the number of rows, you can use JavaScript to extract the row count and insert that value into your :root declaration with setProperty().

document.documentElement.style.setProperty(`--col-rows`, maxNum);

The CSS :root element now includes:

:root {
  --col-rows: 0; /* This will be updated by JS to reflect
                    the number of rows in the DOM */
}

To determine the row count, you can:

// Select all .col elements
const columnRows = document.querySelectorAll('.col');
// Initialize an empty array to store row lengths
const lengthArr = [];
// Iterate over the rows and gather the length of each column's rows
columnRows.forEach(item => {
  let count = item.children.length;
  // Add each row's length to the array
  lengthArr.push(count);
});
// Find the maximum row count
const maxNum = Math.max(...lengthArr);
// Update the --col-rows CSS variable using the root HTML element
document.documentElement.style.setProperty(`--col-rows`, maxNum);

With these adjustments, the number of rows will be automatically calculated using JavaScript and applied to the .col > div min-height property in your CSS styling.

Answer №2

If you're looking for a solution, this example could be helpful:

Check out this link

The concept is to hide certain headers in 4 columns or on large screens, and display them in 2 columns or on smaller screens. Additionally, the cells are rearranged under the 2 columns to group gold/plat/diamond together.

In my usual approach, I use percentages like 25% and 50%, but due to borders affecting the widths, adjustments were made to achieve the desired layout. If this poses an issue, consider placing borders on inner div elements instead of the cells directly.

Since your requirements weren't entirely clear, this may not be perfect but should provide enough guidance to help you achieve the desired outcome.

html

<div class="wrapper">
        <div class="wrapper-inner">
            <div class="col">
              
                <div class="o1"># of virtual attendee tickets (to use as giveaways, remote staff, etc.)</div>
                <div class="o1">Gold</div>
                <div class="rs o2"># of virtual attendee tickets (to use as giveaways, remote staff, etc.)</div>
                <div class="o2">Platinum</div>
                <div class="rs o3"># of virtual attendee tickets (to use as giveaways, remote staff, etc.)</div>
                <div class="o3">Diamond</div>

                <!-- Additional content here -->
              
            </div>
        </div>
    </div>

css

body {
  font-family: "Inter";
}
.wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  &-inner {
    display: flex;
    max-width: 900px;
    margin: 0 auto;
  }
  .col {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    > div {
      padding: 20px 0;
      border: 1px solid #e1e1e1;
      flex: 1 0 auto;
      width: 21%;
    }
  }
}
@media screen and (min-width: 799px) {
  .wrapper {
    &-inner {
      > .col {
        > div {
          &.rs {
            display: none;
          }
        }
      }
    }
  }
}
@media screen and (max-width: 800px) {
  .wrapper {
    &-inner {
      > .col {
        > div {
          width: 48%;
          
          &.o1 {
            order: 1;
          }
          &.o2 {
            order: 2;
          }
          &.o3 {
            order: 3;
          }
        }
      }
    }
  }
}

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

"An error occurred with the Ajax post request - bad request

I've encountered a strange issue while attempting to make a POST request using AJAX with the code snippet below: $('.login-form').on('submit', function(e) { e.preventDefault(); $.ajax({ type: "post", url: " ...

Using Javascript/JQuery to attach a javascript object to a form in order to perform a traditional form

On a page, there is a form that consists mostly of plain HTML. One section of the form creates a quite complex multidimensional object using JavaScript. Usually, if I wanted to include a variable in the POST request, I would add an <input type="hidden" ...

Navigate to a particular row in jsgrid

I've implemented JS Grid for rendering data in a grid view from . The grid I'm using has multiple pages. I need to navigate to the page that includes the newly inserted row and highlight it after insertion. Is there an option in jsgrid that allo ...

Encountered an issue when attempting to save data to the database using an AJAX request in Symfony 3

i need to input text in a form and store it in my database as well as display it on my view using jQuery. here is the input form: <div class="panel rounded shadow"> <form method="post"> <textarea id="txt" class="form-control in ...

Invisibility of form data on new page - PHP and Javascript

I have a webpage with multiple hyperlinks. Each hyperlink should redirect the user to a new page with the URL sent as POST data. What I can do: 1. Open the new page successfully. The issue I'm facing: 1. On the new page, I cannot access the URL ...

deactivating image mapping on mobile media screens

I'm looking to disable my image map on mobile screens using media queries. I've attempted to include some javascript in the head of my html file, but I encountered an error: <script src="http://code.jquery.com/jquery-1.11.3.min.js"></s ...

Add HTML and JavaScript code dynamically with JavaScript

Currently, I am working on a project that involves an HTML table with some basic JS interactions triggered by user clicks. The structure looks something like this: htmlfile.html ... ... position action ...

how can I transfer model values to a dashboard in Rails 5?

I have developed an app that includes an adminlte dashboard. The dashboard is populated with various values obtained by a jQuery file. I am trying to pass module values to the dashboard. For example, the number of users shown in the dashboard should be fet ...

Once the form has been loaded without any information, go ahead and submit

I have created my index.php file with the following code: <script src="js/jquery-1.9.1.js"></script> <script> $(document).ready(function(){ $("#sload").load('save1.php',function(forma){ $(".csave").click(function(){ ...

``The background color will dynamically change according to the result of a function

My function named shift_color generates different color codes like "#FF5F74", "#5FFF66", and "#5F8AFF". I am looking to use this output to style a navigation menu background. I have tried the following code: .topnav { background-color: <?php echo shi ...

Using jQuery to smoothly animate a sliding box horizontally

Is there a way to smoothly slide a div left and right using jQuery animation? I have been trying to achieve this by implementing the code below, which can be found in this fiddle. The issue I am facing is that every time I click on the left or right butto ...

Storing the background color in a JavaScript variable

I've been experimenting with creating a fade in and out effect for a background image on a website. I've also been attempting to capture the background color of a div and store it in a variable. Here's what I have tried: elem = document.ge ...

What is the best method for applying a gradient color to the parent class in CSS using pseudo classes (before and after)?

"Is there a way to overlay gradient colors on pseudo-classes (before and after) while working with parent classes in CSS? I am attempting to create arrow shapes using div elements and the 'after' class. The div's background color consis ...

If the condition is not satisfied, decrease the number of loop iterations

There is a code snippet below: for (var i = 0; i < data.status.length; i++) { if(data.status[i].isMode == 0) { var row = '<tr><td>' + data.status[i].Name + '</td><td>' + data.status[i].Pay + &apos ...

What about a Material UI-inspired homepage design?

Looking at the demo image on the main page for Material UI has me really impressed. I am interested in using a similar theme with MUI for my web application. Can you advise me on how I can achieve this look, or do I have to start from scratch? https://i.s ...

Ways to toggle the display of a div depending on various radio button selections

Currently, I am working on a project using HTML along with Bootstrap and Jquery. My objective is to dynamically hide a div based on the selection of multiple radio buttons without requiring a submit button. The code snippet that I have provided below works ...

"Discover a unique online platform showcasing videos and images with a creative

I recently updated my personal website to showcase some of the tools I've developed through short looping videos. Initially, everything was working well with just images, but when I switched to videos, I encountered some unexpected behaviors. Interest ...

How can we tally the content in the drop area using JQuery Drag and Drop?

Currently, I am developing a Jquery project that involves allowing the user to drag a 'pill' into a 'cup', and then displaying the number of pills in the cup. However, I am encountering an issue where if the user moves a pill once it&ap ...

What is the most efficient way to access a cell within an HTML table using jQuery or the Document Object Model (

I have an unchangeable HTML table styled with CSS. My goal is to extract the value from the first column for filtering purposes. I've been struggling to locate a suitable jQuery or DOM function to accomplish this task. Unable to find a way to access ...

Transferring a parameter from link_to to a popup window in a Rails application

I am facing an issue with passing an object to my modal in Rails. The object has a table of results with an attribute "email", but I seem unable to pass it so that I can use it within my modal. index.html.erb <div class="modal fade bs-example-modal-lg ...