Determine the maximum size of a specified number of square divs that can fit within responsive parent divs

I am working on creating a grid of square divs inside a container with variable height and width.

The number of square div's is predetermined. All square divs will have images with the same dimensions (similar to the example). They should align left and wrap around at the end of one row to the next. I need an algorithm to calculate the number of columns, rows, and their maximum size to fit them all into the parent div.

Can someone provide me with a suggestion on how to determine the maximum size of the squares so that each square fits within the parent container? It seems like I am facing a packing problem here.

The current code I am using to calculate the image height is:

var imageHeight = Math.floor((maxHeight - imageMargin) / 3);

For more detailed code, you can check out this working example on JSFiddle

var $container = $('.container');
var $square = $('.square');

adjustSquares();
function adjustSquares() {
// CALCULATE MAXIMUM CONTAINER HEIGHT (for instance, half of window height)
  $container.removeAttr('style');
  var maxHeight = Math.floor($(window).height() / 2);
  $container.css({
    'max-height': maxHeight + 'px',
    'overflow': 'hidden'
  });

  // CALCULATE MAXIMUM IMAGE HEIGHT (based on the number of squares and the maximum container height)
  var imageMargin = $square.outerWidth(true) - $square.width();
  var imageHeight = Math.floor((maxHeight - imageMargin) / 3); // How to calculate this image height?
  $square.find('img').width(imageHeight);
  $square.find('img').height(imageHeight);

  // CALCULATE CONTAINER WIDTH (to determine the maximum number of squares per row and center them)
  var maxWidth = $container.width();
  var blockWidth = $square.outerWidth(true);
  var squaresPerRow = Math.floor(maxWidth / blockWidth);
  $container.width(squaresPerRow * blockWidth);
}

var resizeTimeout;
$(window).resize(function() {
clearTimeout(resizeTimeout);
  resizeTimeout = setTimeout(adjustSquares, 200);
});
body {
  padding: 0;
  margin: 0;
}

.container {
  width: 100%;
  margin: 0 auto;
  font-size: 0; /* REMOVE inline-block SPACE */
}

.square {
  display: inline-block;
  margin: 5px;
}

img {
  max-width: 100%;
  height: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <div class="container">
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
    <div class="square">
      <img src="http://lorempixel.com/100/100" alt="square" />
    </div>
  </div>
</body>

Answer №1

To properly adjust the height of all elements with the class .square, you will need to iterate through each of them using the $.each() function.

$(function () {
  var tallestHeight = 0;
  $(".square").each(function () {
    var currentHeight = $(this).height();
    tallestHeight = (currentHeight > tallestHeight) ? currentHeight : tallestHeight;
  });
  $(".square").css("height", tallestHeight);
});

Answer №2

Hooray, I did it!

After referencing this helpful post on Math Stackexchange, I was able to find a practical solution.

The step-by-step process I followed:

  var containerWidth = $container.width();
  var containerHeight = Math.floor($(window).height() / 2);

  $container.css({
    'max-height': containerHeight + 'px',
    'overflow': 'hidden'
  });

  // DETERMINING MAXIMUM SQUARE SIZE based on the number of squares and the dimensions of the container
  var hw = containerHeight / containerWidth;
  var wh = containerWidth / containerHeight;

  var px = Math.ceil(Math.sqrt(squareCount * hw));
  var py = Math.ceil(Math.sqrt(squareCount * wh));

  var sx;
  if (Math.floor(px * wh) * px < squareCount) {
    sx = containerWidth / Math.ceil(px * wh);
  }
  else {
    sx = containerHeight / px;
  }

  var sy;
  if (Math.floor(py * hw) * py < squareCount) {
    sy = containerHeight / Math.ceil(py * hw);
  }
  else {
    sy = containerWidth / py;
  }

  var squareDimension = Math.floor(Math.max(sx, sy) - squareMargin);
  $squares.find('img').width(squareDimension);
  $squares.find('img').height(squareDimension);

Take a look at this demo link to see the updated calculation algorithm in action.

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

Issues with executing basic unit test in Angular Js

THE ISSUE: In an attempt to create unit tests for my Angular application, I set up a basic test app and wrote a simple unit test. However, the test is not functioning as expected. APPLICATION CODE: var app = angular.module( 'myApp', [] ); app ...

Troubleshooting MaterialUI: Is there a way to prevent the InputLabel text from disappearing when setting a background color on the Select component?

My goal is to change the background color on dropdown elements while keeping the default text visible. Currently, if I don't explicitly set a background color on the Select component, the text specified in the InputLabel displays correctly. However, o ...

Clicking on the spinner does not activate it

I've implemented a basic spinner on my website, and it works fine when using the keyboard. However, I'm experiencing issues with mouse clicks. I can't seem to figure out what is causing this problem. Below is the code snippet related to the ...

Tips for ensuring that divs resize to match the container while preserving their original proportions:

#container { height: 500px; width: 500px; background-color: blue; } #container>div { background-color: rgb(36, 209, 13); height: 100px; } #one { width: 1000px; } #two { width: 800px; } <div id="container"> <div id="one">&l ...

Step-by-step guide on permanently assigning a value in Node.js

I recently started working with node js and I am facing an issue where I need to save an id in a global variable that is required in multiple functions. However, every time the server restarts, the variable gets emptied. Is there a way to persist this va ...

Monitor a nested watch property for potential undefined or default values

Currently tackling an issue in my Vue2 project related to a specific property I'm trying to watch. Here's what the code snippet looks like: watch: { 'car.wheels': function(){ ... } Sometimes, 'wheels' is undefined ...

HTML code now launches in the existing electron window rather than opening a new window

<html> <head> <title></title> </head> <input type="button" name="new" id="newmon" value="new"> <button onclick="open1()">monitor 1</button&g ...

Is there a way to verify if the request query value is empty like ""?

When creating a Node.js component, I encountered an issue with one of my APIs that sometimes returns empty query values like "". In order to handle this scenario, I need to implement a conditional statement. For example, the query may look like this: { ...

How can I retrieve objects using the JSON function?

I need to design a function that returns objects like the following: function customFunction(){ new somewhere.api({ var fullAddress = ''; (process address using API) (return JSON data) })open(); } <input type= ...

Utilizing CSS Grid to dynamically stretch and wrap rows based on adjacent siblings within a flat DOM structure

Looking for a way to create a grid layout that adjusts row sizes based on adjacent siblings. Interested in SCSS or CSS solutions using grid, flexbox, or other techniques. I am working with a dynamically generated list based on configurations. The length, ...

Encountering an unexpected token 'Indent' while working with Jade in Eclipse

Currently, I am delving into the world of node, angular, javascript, express, and jade. Transitioning from a purely .net based coding environment has presented me with quite the learning curve. While following a tutorial, I encountered a roadblock at step ...

The switch statement remains unchanged for varying variables

Here is some code that I am working with: updateTable(selectedIndex) { console.log("running updateTable function"); let level = ''; // if (selectedIndex == 1){ // this.setState({level: 'day'}) // th ...

Discover the method to retrieve the value of the closest input using JQuery

On my webpage, I have several buttons all sharing the same class. Whenever a user clicks on one of these buttons, I need to retrieve the value from the closest input box. It's important to note that there are multiple input boxes and buttons with the ...

What is the best way to create a sortable column that is based on a nested object within data.record?

I am utilizing jquery jtable to showcase some data in a table. I have been using the following code snippet for each field in the table to display the data and enable sorting: sorting: true, display: (data) => { return data.record.<whatever_value ...

What exactly does this jquery library aim to achieve?

Recently, I discovered a new library for CSS3 animations. As someone who is still learning JavaScript, I am unsure of the benefits it offers. It appears to replicate what jQuery can already achieve but by utilizing CSS3. So, what advantage does using a to ...

When creating a new instance of the Date object in Javascript, the constructor will output a date that is

In my project using TypeScript (Angular 5), I encountered the following scenario: let date = new Date(2018, 8, 17, 14, 0); The expected output should be "Fri Aug 17 2018 14:00:00 GMT-0400 (Eastern Daylight Time)", but instead, it is returning: Mon Sep ...

What is the process for creating a div and its children without inheriting the styles of its parent?

Imagine having the following structure: <div class="building"> <p>...</p> <div class="room"> <p>...</p> </div> </div> <div class="building"> <p>...</p> <div class="room ba ...

What is the best approach for establishing an asynchronous connection to a MongoDB database?

Managing a MongoDB database using JavaScript and Node.js with Mongoose, I needed to retrieve an array containing the names of all collections in the database. Taking this into consideration, I implemented the following code snippet. let connection = mongoo ...

What is the best way to apply a top padding to a background image using CSS?

I attempted to adjust the top padding using various pixel values in the style, but the image padding relative to the webpage remained unchanged. For example: padding-top: 5px; Here is part of my code snippet: <div class="row pb-5 mt-4" style ...

Jasmine - What is the best way to simulate a finally block?

After creating a controller in my test, I encountered an issue with the updateActionList function... this.createScope = function(scope) { if (scope) { this.scope = scope; } else { this.scope = $rootScope.$new(); } this.contro ...