adaptive layout showcasing a mix of images in varying sizes that accurately reflect real-life proportions and scales

My Laravel site is having an issue with inconsistently scaled product images loaded into a grid-container--fit row through a @foreach loop. The images vary in sizes and orientations, causing them to be scaled unevenly when displayed on the responsive grid. For example, portrait images may appear larger than landscape images of the same size due to the fixed min-max pixel width constraints.

While a table structure could easily rectify this problem, it is not an acceptable solution!

Below is the PHP loop:

<div class="product-grid-container grid-container--fit">
    @foreach($products as $product)
        <div class="grid-element">
            <a href="/product/{!!$product->id !!}">
                <img src="{{ asset($product->image) }}">
            </a>
        </div>
    @endforeach
</div>

Here's the corresponding CSS:

.product-grid-container {
    display: grid;
    max-width: none;
}

.grid-container--fit {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}


.grid-element {
    padding: 5%;
    color: #fff;
    text-align: center;
}

.grid-element img {
    max-height: 300px;
    max-width: 300px;
    object-fit: contain;
}

Answer №1

This issue leans more towards design than code. The current grid setup is functioning correctly, but it doesn't align with your desired layout. To achieve what you want, consider using Flexbox instead. Another challenge lies in scaling images without distorting them while maintaining a cohesive look. By setting a max-height and/or max-width, the images will adjust their proportions based on the original dimensions. Ideally, you should be aware of the image sizes to ensure a harmonious proportion between them. Here's a suggested approach:

.product-grid-container {
  display:   flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
}

.grid-element {
  padding: 24px;
}

.grid-element img {
  max-width: 100%;
  object-fit: contain;
}
<div class="product-grid-container">

  <div class="grid-element">
    <img src="https://picsum.photos/400/200">
  </div>
  
  <div class="grid-element">
    <img src="https://picsum.photos/250/450">
  </div>

  <div class="grid-element">
    <img src="https://picsum.photos/400/500">
  </div>
  
  <div class="grid-element">
    <img src="https://picsum.photos/250/350">
  </div>

  <div class="grid-element">
    <img src="https://picsum.photos/450/250">
  </div>
</div>

Answer №2

When using grid, it's important to specify how elements span through rows or columns. If you're unsure of the number of rows or columns each image needs for a patchwork layout, grid (with display and grid-template-columns) may not be sufficient.

column-width from https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Columns could provide some assistance:

CSS Multi-column Layout is a module that adds support for multi-column layouts. It allows setting the number of columns in a layout, flow of content between columns, gap sizes between columns, and appearance of column rules.

calc() can also aid in achieving responsive column sizing.

The calc() CSS function enables calculations when specifying CSS property values. It supports <length>, <frequency>, <angle>, <time>, <percentage>, <number>, or <integer>.

Explore the demo below or on CodePen for experimentation

div {
  column-width: calc(100px + 10vw); /* adjust as needed */
  padding-left: 2.5%;
  margin: 0;
}
img { 
  width: 90%;
  margin: 5% 0;
}
<h1> Column CSS - Resize window width to observe behavior</h1>
<div>
<img src="http://dummyimage.com/100">
<img src="http://dummyimage.com/100x200">
<img src="http://dummyimage.com/150x200">
<img src="http://dummyimage.com/100x250">
...
<img src="http://dummyimage.com/100x180">
<img src="http://dummyimage.com/150x200">
<img src="http://dummyimage.com/100x250">
</div>

If this approach doesn't suit your needs, consider utilizing the Masonry script.

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

Is It Possible to Have Four Equally-Spaced DIVs with Variable Widths?

I am currently facing a challenge in creating a simple sub-navigation system for my website. I want to have either 3 or 4 equally spaced DIVs across the top of the content area. Despite being a common requirement, CSS does not offer straightforward solutio ...

Dynamically alter div width using jQuery in real-time

Is it feasible to adjust the width of a div dynamically using jQuery? If so, how can this be achieved? My objective is to alter its width based on the width of the browser window in real time, ensuring that whenever the user resizes the browser window, th ...

Preserving spacing using minimum widths

Looking for some help with a page layout issue. I have a header on my page with a title and menu. Currently, the header has a margin and a minimum width set to the width of the menu. However, when the user resizes the window and reaches the minimum width, ...

Why does the HTML and CSS slider fail to load upon page refresh, yet works perfectly when the next or previous button is clicked?

There seems to be an issue with the slider images not loading on page refresh. Oddly enough, they appear only after clicking the next button. However, upon refreshing the page again, the images disappear. <div class="slide-banner"> < ...

Trouble arises when attempting to bind a JavaScript event with an innerHTML DOM element

I am currently dealing with a complex issue on how to bind a JavaScript event to an innerHTML DOM element. The scenario involves having a div with an input checkbox inside it, along with some pre-existing JavaScript event bound to that checkbox. Additional ...

Display only the static placeholder in Angular 2 multi-select feature

My experience with angular 4 is fairly new and I've recently delved into using the angular multiselect feature with the npm package available here. I've managed to successfully configure it, as well as capture selected and deselected items/event ...

What is the best way to link a URL ID to a specific piece of content stored on

Is it possible to display a product without creating separate html pages for each one using unique IDs? I prefer the layout style of Pinterest. For example, with a URL like /product/12345 When a user clicks on /product/12345, the content should be rende ...

How about inserting a fresh div block with jQuery?

I am trying to dynamically insert a new div block when the insert button is clicked. I found some resources online but they all use a constant div block structure. I want to be able to call the new div by its id and also have an option to remove it on clic ...

Retrieving an image from an input file and setting it as the background of a div element

I am currently developing a whack the mole game using Vanilla JS, and I'm attempting to allow players to choose their own target by uploading an image file. However, despite days of searching for a solution, I have not been successful. HTML: <inpu ...

What could be the reason for the SVG gradient not appearing in Explorer?

Check out my website at Everything was working smoothly with the background for the top menu, but suddenly it stopped working. The strange thing is, I only made adjustments to the position settings of some unrelated elements. ...

What is the best method for developing a draggable element that remains stable?

I have developed a simple script for draggable elements, however, I am facing an issue. Whenever I move the mouse quickly, the element loses focus. Is there a way to improve the stability of the dragging functionality regardless of how fast I move the mou ...

The slider feature is not set to automatically slide on its own

I need help creating a JavaScript slider that includes next and previous buttons. The functionality for these buttons is working properly, but I also want the slides to transition automatically. <link rel="stylesheet" href="http://www.w3schools.com/l ...

When I scroll, changing the position from fixed to absolute causes my image to jump

My goal is to implement a unique visual effect where a background image, initially positioned as "fixed", gradually moves towards the top of the webpage and then disappears as if it were absolutely positioned. However, this movement should only occur after ...

Can someone assist me in configuring the mobile display for an Angular website?

I'm currently working on a website and I'm in need of some assistance to fix the mobile view. The main issue I'm facing is with the menu which contains 5 buttons. While I'm happy with how it looks on a laptop or desktop, the mobile view ...

CSS - using !important alongside a variable

Is there a way to dynamically add an important tag to the CSS height property of #content located on line 7 in the code snippet below? I need to use the variable $newHeight to ensure flexibility. Can anyone provide guidance on how to append an !important ...

Selectors that can be applied to multiple IDs with identical functions

I have a script for popups that I want to be able to use multiple instances of. For example, I would like to have 5 popups on one page without having to change the JavaScript code. While everything in the script works fine, I am struggling to make it work ...

Email delivery confirmation tests for Event Listeners

As someone who is new to testing, I could really use some assistance. Let's start with the code snippet from my App directory in Laravel 5.5. // controller public function store(Request $request) { $foo = new Foo($request->only([ ...

Is there a way for me to retrieve the aria-expanded attribute value from a button element

Is there a way to access button properties from a click listener in order to use the aria-expanded attribute to set certain values? Here is my current code. x.html <button class="btn btn-secondary" (click)="customSearch($event.target)" type="button" d ...

I'm attempting to create an HTML document using the Shruti font (Gujarati Unicode), but instead of displaying correctly, it is showing strange, random symbols

Attempting to create an HTML file utilizing the Shruti font (Gujarati Unicode), but encountering random symbols instead. I have experimented with various methods, including the mpdf library, without success in importing the Shruti font. ...

Using HTML and CSS to Dynamically Alter Cell Text Color Based on Value

Unfortunately, I am unable to find a straightforward solution. My goal is to adjust the text color inside my cell based on its value. Here is a basic table: <table> <tbody> <tr> <td>Quantity</td> <td> ...