Equal dimensions for multiple div elements in both height and width

Looking to create responsive blocks using jQuery. I have two cube blocks with different widths. After writing a few lines of code to make them responsive, the second block now takes the height of the first div and changes on resize. Check out this example to see the issue when resizing the window width.

I was hoping for the different cubes to resize independently and maintain their cube shape.

Here is the code snippet:

function cube(){
    var divWidth = $('.cube').width(); 
    $('.cube').height(divWidth);
}

Answer №1

If you're looking to dynamically adjust the height of elements based on their width, you can leverage jQuery's .each() function. Take a look at the example snippet provided below:

cube();
$(window).resize(cube);

function cube() {
    $('.cube').each(function() {
        $(this).height($(this).width());
    }); 
}
.block1 {
  background: red;
  max-width: 300px;
  width: 100%;
}

.block2 {
  background: blue;
  max-width: 70px;
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="block1  cube"></div>
<br/>
<div class="block2 cube"></div>

Answer №2

You must utilize a forEach method to iterate through each of the elements within the divs

rotate();

$(window).resize(rotate);

function rotate(){
$('.box').each(function(){
        var boxSize = $(this).width(); 
    $(this).height(boxSize);
})

}

Answer №3

Check out the Latest Fiddle Here Latest Fiddle

<div class="block1  cube"></div>
<br/>
<div class="block2 cube"></div>

CSS :

.block1{


background:red;
  width:100%;

}

.block2{
  background:blue;
  width:100%;
}

Js :

$(window).resize(function(cube){
cube();
function cube(){
    var divWidth = $('.cube').width(); 
    $('.cube').height(divWidth);
    }

});

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

Modify the class of a div as you scroll through the page between anchor points

I have been working on implementing a feature for my website where when I scroll to an anchor, the corresponding menu item adds or removes a specific class. However, I am facing an issue with making the first menu item remove the class when scrolling to t ...

Capturing keydown events exclusively in the topmost layer of the HTML document

Currently, I am developing a web application that includes an underlying HTML file with some JavaScript functionality that I cannot modify. In my code, I create a layer on top of this page using CSS with z-index: 1000;. However, I am facing an issue where ...

Styling React components using class names

Struggling with implementing a sidebar feature using the guidance provided in this example. However, I am facing difficulty in applying the necessary styles to each className... Here's what I've attempted so far, but unfortunately, no styles see ...

Obtain the href using a combination of xpath and id

I need to extract the href links for 9 search results from this specific website. The xpath and selectors for the first, second, and third items are as follows: '//*[@id="search-results"]/div[4]/div/ctl:cache/div[3]/div[1]/div/div[2]/div[2]/div[2]/p[ ...

Leveraging the power of jQuery's ajax function within Asp.Net

I am relatively new to ASP and I am having trouble figuring out the correct URL to use for an ajax call in my application. When I enter http://localhost:someport/ in the browser, it is displayed. However, when I try adding specific extensions like ["index" ...

Introducing HTML elements into pre-existing web pages

My interest lies in the idea of injecting HTML into existing web pages for enhanced functionality. Specifically, I am exploring the concept of creating a more efficient bookmarking system. As someone new to web development, I am unsure of how to achieve th ...

Is it possible to access the service and 'self' directly from the HTML template?

When working with Angular 6, one method to access component properties from a service is to pass 'self' to the service directly from the component. An example of this implementation is shown below: myComponent.ts public myButton; constructor(p ...

Enhance the functionality of jQuery sortable by including additional details

I have a li list that I have implemented sortable functionality using jQuery. In order to ensure that the updated data is sent to the correct destination, I need to include some hidden values in the serialized data. How can I achieve this? HTML <ul i ...

Internet Explorer 9 does not display content until the ajax/json function has finished executing

I have encountered an issue with my code regarding updating a div element. The first innerHTML call seems to be ineffective and does not render in the browser. However, the second innerHTML call works as expected by appending "Complete" once the ajax call ...

Displaying the outcome of an HTML form submission on the current page

Within the navigation bar, I have included the following form code: <form id="form"> <p> <label for="textarea"></label> <textarea name="textarea" id="textarea" cols="100" rows="5"> ...

Google Chrome successfully transmits two variables with AJAX, whereas Mozilla Firefox does not send them

Having an issue with sending two values through ajax - currently only one value is being sent in Mozilla browser. Ajax script: $('#post_submit').click(function() { event.preventDefault(); var great_id = $("#post_container_supreme:first").attr(" ...

Ways to align text on the left within a centered format

It seems like there is something missing here. I am struggling to align text to the left in a centered div. Below is an example of my final work: <div class="grid-row"> <img src="http://www.fununlimitedsports.com/wp-content/images/istock ...

Refresh a div with Ajax once all data has been fully loaded

I am currently using an ajax refresh div function that reloads a specific div every 10 seconds. Occasionally, the div loads before receiving data from mysql. Is there a way to modify it so that it waits 2 seconds after reloading the div? <script type ...

Can a link be stored in a table using PHP?

I need assistance in organizing a page with a sidebar containing about 20 links as the code appears messy. Is there a way to store all <a href=.... in a table, an array, or another method for better organization? If anyone could provide an example, it ...

What could be causing my jQuery handler to not capture my form submission?

I am developing a Ruby web application and using JQuery and AJAX to send/receive data. However, I am facing an issue where pressing the enter key does not submit the form. What should I do to ensure that my form submits successfully? Within my Foundation ...

The absence of HTML and CSS is preventing the inclusion of an Ajax image viewer, back button, and

Currently, I am experimenting with jQuery and JavaScript to create an ajax overlay image viewer for a PHP website. After adding this code snippet at the end of the 'gallery page', I can successfully open the viewer and navigate using the next and ...

Having trouble using functions with dynamically loaded content via AJAX in JQuery

I am facing an issue with my code. I am trying to dynamically fetch some data and display it. I have checked the request using firebug and it seems to be successful, but the problem arises when I try to execute the activeImage() function. The images are no ...

Enhancing Numbers with JavaScript

What I'm Looking for: I have 5 counters on my webpage, each starting at 0 and counting upwards to different set values at varying speeds. For example, if I input the values of 10, 25, 1500, 400, and 500 in my variables, I want all counters to reach t ...

What is the best way to generate two <div> elements, with one of them being centrally fixed?

When it comes to CSS, I must admit that it's not my strong suit. My current challenge is to create two boxes or divs where one remains fixed in the center of the page, while the other adapts and positions itself right next to the first one. Essentiall ...

Hiding collapsible navbar in Angular 7 when in responsive mode

Hey there, I'm currently using a navbar in Angular 7 and I'm looking for a way to collapse it when clicking on another area of the page. When resizing my browser or when accessing the app on mobile, the navbar displays a menu icon with three bars ...