Adjust the height based on the size of another div when the window is resized

Two divs are positioned next to each other with a width of 50% each. One of the divs contains more text, causing it to be longer than the other div. A javascript function adjusts the height of the shorter div to match the height of the longer div, ensuring they both appear the same height.

Issue: After resizing the window to a smaller size (tablet size) both divs adjust their height accordingly. However, when the window is returned to its normal size, the div heights remain the same as after the resize and do not revert to their original state.

JavaScript code for handling window resize:

$(document).ready(function() {
function adjustHeight() {
var leftHeight = $('.leftblock').height();
var rightHeight = $('.rightblock').height();
if (leftHeight > rightHeight){ 
  $('.rightblock').css('height', leftHeight); 
} else {
  $('.leftblock').css('height', rightHeight); 
}
}
$(adjustHeight);
$(window).resize(adjustHeight);
});

Example:
http://jsfiddle.net/hg0L01ex/

Answer №1

Contributed by Erwin van Ekeren:
The optimal solution for the issue is functioning without the use of JavaScript, but by utilizing CSS table-cells: http://jsfiddle.net/2uogqxyt


Avoid utilizing JavaScript and opt for CSS instead. You can choose either the FlexBox model or table-cells;

Here is an example with the table-cell approach :

.blocks { display:table-row;}
.leftblock          { display:table-cell; width:50%; padding:100px 0 100px 0; margin:0; background:#e14f47; }
.rightblock         { display:table-cell; width:50%; padding:100px 0 100px 0; margin:0; background:#cd2c24; }

Visual demonstration using table-cell

.blocks { display:flex; width:100%; align-items:stretch;}
.leftblock          { flex-grow:1; padding:100px 0 100px 0; margin:0; background:#e14f47; }
.rightblock         { flex-grow:1; padding:100px 0 100px 0; margin:0; background:#cd2c24; }

Visual demonstration using flexbox

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

The jQuery .get() function is only operational in debugger mode

I am attempting to retrieve data from , which provides a plain text response, and store it in a variable for future use. Below is my javascript code: var extractData = function() { var copiedData = "not successful"; $.get('http://numbersapi.c ...

Modify the color of certain terms within the input field

I'm currently working on an editor for Cucumber feature files. As I write step definitions, I'd like the keywords Given, When, Then, etc. to appear in blue text, and any text between < > brackets to show up in red. Is there a way to change ...

Prevent users from navigating back in their browser with jQuery

As I work on developing an online testing app, one of the requirements is to prevent users from refreshing the page or going back during the test until it has ended. While I have successfully disabled the refresh action in jQuery using various methods with ...

Unable to trigger onSelect event on the datepicker component

Whenever a date is chosen, I need to trigger a javascript function. Here is the javascript code: $(document).ready(function(){ var date_input=$('input[name="date"]'); //our date input has the name "date" var container=$('.bootstrap- ...

What are the steps to applying strike-through text in Vue.js?

I am currently working on a to-do application using Vue.js and I want to implement a feature where the text rendered in HTML has a line through it when the user checks an item off the list. Here is the snippet of my code: <html> <head> & ...

The images on my website are not displaying properly on different devices

My attempt at creating a website for fun and testing purposes is facing issues when viewed on mobile devices. The images and specially positioned elements are not displaying correctly, extending beyond the page boundaries. I have tried adding the viewport ...

How can you apply an active class using React-Router?

My React-Router navigation issue nav.tsx import React from 'react' import { menu } from './menu' import { Link } from 'react-router-dom' import styles from './HamburgerMenu.module.scss' const HamburgerMenu: React.F ...

Enhancing Values Across a Timeframe Using JavaScript

Last time, I asked about my code. Here is what I have currently: var secs = 100; setInterval(function() { var $badge = $('#nhb_01'); $badge.text((parseFloat($badge.text())+0.01).toFixed(2)); }, secs); I want the counter to increase by ...

Using jQuery to target the element before

Is there a way to determine the width of elements located before an element when it is hovered over? I attempted to achieve this using the following code: $('ul li').hover(function() { $(this).prevAll().each(function() { var margin = $(this ...

Adjusting the sidebarPanel height to match the mainPanel content in a shiny app

I'm currently developing an app that features a scrollable sidebarPanel alongside a mainPanel that can have varying heights, such as: library(shiny) ui <- fluidPage( headerPanel('Iris k-means clustering'), sidebarPanel(style = " ...

Implement a fade-in effect when loading content into a DIV using Jquery

Here is the code snippet I am working with: $('.LINK').on('click', function (event) { event.preventDefault(); $('#Grid').load($(this).attr('href') + ' .project_thumb'); }); The current functionality ...

Copy the AJAX response from a JQuery request to the clipboard seamlessly without requiring the user to manually click a button

I'm attempting to automatically copy a portion of a JSON AJAX response to the user's clipboard. When the user submits a form by clicking a button, I want the response to be copied without any additional interaction required. This is my progress ...

Unable to logout with ExpressJS passport-local

After pasting the passport-local app into my own, I noticed something interesting: I managed to successfully log in users, but for some reason, I can't seem to get them logged out. app.get('/logout', function(req, res){ req.logout(); r ...

Tips on effectively utilizing dynamic data with Google Charts

I am working on creating a dynamic chart using Google Charts with multiple CSV files. I want to display a different chart depending on the selection made by the user. The first file loads successfully and displays a chart, but the $("#selection").change(.. ...

Flame-inspired CSS editor

My current CSS editing workflow goes something like this: I ask for feedback on a page Suggestions are given to make post titles bigger I use Firebug to inspect the post title element I locate the corresponding CSS statement in Firebug (like h2.post_title ...

Unveiling the secrets to isolating elements from a list based on distinct characteristics

Currently, I am attempting to scrape job skills from the Wuzzuf website using the following code snippet: result = requests.get("https://wuzzuf.net/search/jobs/?q=data+analysis&a=navbl") src = result.content soup = BeautifulSoup(src, "lx ...

Adjusting the width of a button can result in gaps forming between neighboring buttons

I am currently working on a calculator project that involves customizing the width of a specific button with the ID equal. However, when I adjust the width of this button, it results in unexpected spacing between the third and fourth buttons in each row, c ...

Tips for creating a clickable ::before image

I'm trying to create a hover effect where an image appears and is clickable when hovering over an element, similar to what is seen on many websites. For example, I want to display a bin icon when hovering over an element by using the ::before pseudo-e ...

Could someone assist me with understanding how to use the Load function in JQuery?

I have a set of links with the class "ajax" that are meant to fetch content from an element with the id "test" in the file "data.html" located in the same directory. This content should then be inserted into the div with the id "content" on my page: <s ...

Ajax is utilized to run an external page and refresh the current page simultaneously

Currently, I am retrieving data from a database and using a button to selectively delete entries. The code below is what I have been using on a button click, and it functions just as I intend: $(".delete").click( function() { if (confirm('Delete ...