Dynamic layout of images with varying dimensions

Discovering the explore section on Flickr was quite intriguing for me:

I found it impressive how they showcase pictures in various sizes and how the layout adapts when you resize the browser window.

[UPDATE] How do you think one could create a similar display without relying on the masonry library as mentioned in the comment below? I've been tinkering with some JavaScript pseudo code, trying to figure out a logic that might work. Any thoughts on this approach? How could we turn it into a functional library?

var rowWidhtPx=document.getElementById('gridContainer').offsetWidth;; 
var maxRowHeightPX= 300;
var minRowHeightPX= 200;

var imageArray=[
    {
        src:'somesrc',
        width:200,
        height:300,
    },
    ...
    ,{
        src:'somesrc',
        width:200,
        height:300,
    }
];
var rowMatrix;
var rowsIndex=0;
rowMatrix[rowsIndex].setHeight(maxRowHeightPX);
imageArray.forEach(function (img,index){
    img.scaleHeight(maxRowHeightPX);

    if(rowMatrix[rowsIndex].actualWidth+img.widht<rowWidhtPx){
        rowMatrix[rowsIndex].push(img);//set margin gutter to prev image
    }else{
        rowMatrix[rowsIndex].scaleHeight(minRowHeightPX) ;//scale also image heights
        if(rowMatrix[rowsIndex].actualWidth+img.scaleHeight(minRowHeightPX)<rowWidhtPx ){
            rowMatrix[rowsIndex].push(img);
        }else{
            rowMatrix[rowsIndex].setOptimalHeight();
            //starting from minimum height, take total widht and set it to rowWidthPX, and scale the height accordingly 
            //then takes single images and set also the height              
            rowsIndex++; // add new row
        }
    }
}); 

Answer №1

This design layout is known as Masonry Grids .... Feel free to explore it further!

Masonry Portfolio Collection .... Implement this library for stunning gallery designs.

Answer №2

If you're looking to create a grid layout, consider using a responsive CSS framework like Bootstrap or Foundation instead of custom CSS styling.

For instance, with Bootstrap, designing responsive grid structures becomes simple. Let's say we want a webpage resembling Flickr, with the first row displaying 4 images, the second row showing 2 images, and the third row showcasing 6 images. As the screen size changes, the images should rearrange and resize accordingly. This can be achieved easily with Bootstrap:

<div class="container">
  <div class="row">
    <div class="col-xs-4 col-md-3">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-3">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-3">
      <image src="..." alt="...">
    </div>
  </div>
  <div class="row">
    <div class="col-xs-12 col-md-6">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-12 col-md-6">
      <image src="..." alt="...">
    </div>
  </div>
  <div class="row">
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
    <div class="col-xs-4 col-md-2">
      <image src="..." alt="...">
    </div>
  </div>
</div>

In this setup, .row represents an image row while the .col-*-* classes define the appropriate columns. To delve deeper into Bootstrap, visit their website: http://getbootstrap.com/

Hopefully, this clarifies things for you.

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

Enhance information flow within pages using SWR in NextJS

Utilizing SWR in a NextJS project has been a great experience for me. I have successfully fetched a list of data on my index page and added a new entry to the data on my create page. Now, I am looking to take advantage of SWR's mutate feature to updat ...

Ways to turn off the adaptive transient feature

Is there a way to turn off the responsive features and grid layout of Bootstrap? While creating a website for a company, I ensured that it was responsive for mobile devices. However, the company now wants a different design specifically for mobile, with t ...

The placement of the sliding navigation menu is consistently positioned beneath the header

I created a jQuery menu that's also available on JSfiddle here: $(document).ready(function () { $(".navigation_button").on('click', function () { var $panel = $(this).next('.panel'); if ($panel.is(':visible ...

What is the best way to apply lodash's max function to a jQuery array?

I'm having trouble extracting the maximum number of rows from two tables. My variable maxRows ends up being a tbody jQuery element instead of the actual maximum value. I've experimented with both the pluck syntax and the long form, but both metho ...

unable to locate the PHP file on the server

Struggling to make an API call via POST method to retrieve data from a PHP file. Surprisingly, the code functions properly on another device without any hiccups. However, every time I attempt to initiate the call, I encounter this inconvenient error messag ...

The styling of divIcons in React Leaflet Material UI is not applied as expected

When using divIcon in React Leaflet to render a custom Material UI marker with a background color prop, I noticed that the background style is not being applied correctly when the marker is displayed in Leaflet. Below you can find the code for the project ...

Enhance your PrimeNG p-calendar by customizing the background-color of the dates

Hello, I am currently attempting to customize the appearance of the p-calendar, but I am struggling with changing the color of the displayed dates. Can anyone provide assistance? Thank you in advance. Below is my template: <div class="p-field p-co ...

Monitoring the content of a page with jQuery and adjusting the size as needed

Here is a code snippet: function adjustContainerHeight() { $('div#mainContainer').css({ 'min-height': $(document).height() - 104 // -104 compensates for a fixed header }).removeShadow().dropShadow({ 'blur&a ...

Using AJAX for redirection following a successful PHP post without any errors

I am fairly new to javascript, but I have successfully posted form data to a php file. However, I am now encountering an issue with validations on the php file. What I need is for this ajax request to display the error message if any validation fails and t ...

Is it possible to target elements based on a specific CSS3 property they use?

Is there a method to target all elements in the DOM with a border-radius other than 0? If anyone has suggestions, it would be greatly appreciated! ...

Managing the response from a variable returned by jQuery's .load method

I am facing an issue with my code. I am using jQuery post to validate whether the name of the filter is available for use. $.post('check-username.php', $("#myform").serialize(), function(data) { alert(data); }); When I use the alert to disp ...

How can I use ngx-editor to insert an HTML block at the current cursor position by clicking a button?

I am currently using ngx-editor within Angular 7. My goal is to insert HTML at the cursor's position upon clicking on parameters from a list. The current view displays how the parameter is appended when clicked, as shown in the image attached https:// ...

When cascading, $q.All did not wait for all promises to finish

Working with $q.all to execute an array of promises asynchronously has been my recent task. I encountered a situation where I needed to ensure that one of the elements in the array completes its promise execution before moving on to the outermost 'the ...

When the button is clicked, two forms will be sent simultaneously and the PHP script will be

I have successfully set up my website with PayFast integration. Now, I need to run another PHP script when the user clicks on the place order button to save details in the database. Unfortunately, my code is quite long and I am not familiar with Ajax or Ja ...

How can I create a design that adjusts to show 5 columns on larger screens and 2 columns on smaller screens using Bootstrap 4?

I am working on a responsive column layout for an online store. I need to display 5 columns on large screens and 2 columns on mobile screens using Bootstrap4. However, on screens below 576px width, only one column is being rendered instead of two. How ca ...

The alignment of SVG is not in sync with the aspect ratio

I've been struggling with this issue for some time now, trying to align the SVGs correctly for different width and height values. My viewBox has an aspect ratio of 16:9, and I want to scale the SVG accordingly. It's working fine for certain combi ...

Searching for values using keys in Angular

Currently, I am working on a project using Angular where I need to store information based on specific identifiers. To display this information in the Angular application, I am pulling data for different identifiers and showing it on the screen. At the mo ...

Using NGRX Effects to Load Data for a Specific Item in Angular

On my website, there is a page that displays a range of products from the store managed by a reducer called products. When an action PRODUCTS.LOAD_ALL is dispatched, it triggers an API call through an effect and then sends a PRODUCTS.LOAD_ALL_SUCCESS actio ...

Dynamic image loading in React with custom properties

I'm facing an issue while trying to display an image using the source stored in this.props.src. The correct name of the image file I want to load, "koolImg", is being output by this.props.src. I have imported the file using: import koolImg from ' ...

Guide to saving an Object to a file (JSON) within the project directory using React Native for Debuggingpurposes

Feeling a bit overwhelmed trying to tackle this issue. My goal is to save data to a JSON file in real-time while debugging my application. I have a Redux store state that I want to organize neatly in a file for easier debugging, so exporting/writing the ob ...