Is there a way to adjust the size of items based on the window size within a flexbox layout?

I have successfully implemented a basic image gallery using flexbox in HTML/CSS. The current setup includes six images on the page, and thanks to flex-wrap, their configuration changes from 1x6 to 2x3 to 3x2 to 4+2 to 5+1 to 6x1 depending on the window size. Now, I am looking to add two additional features:

  1. I want to limit the scaling to a maximum of 3x2 to maintain the symmetry of the website. Beyond this, only the size of the images should increase.
  2. The image sizes should adjust according to the window size. For instance, when resizing the window within a 2x3 layout, the image sizes should increase until reaching the 3x2 configuration. This adjustment is meant to ensure that the images utilize the full width of the window, with some padding preserved. I attempted using percentage width but found it incompatible with flexbox.

Below is the HTML code snippet:

   


.row {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
   
    margin: 150px;
  } 

  .image
  {
    min-width: 30%;
    flex-grow: 1;
    transition: 0.2s;  
    padding: 0 2vw 4vw;
    margin: 20px 10px 20px;
  }

  
  
        
        <!--Image gallery-->
        <div class="container">
            <a href="test_image.jpeg">
                <div class="image"><img src="test_image.jpeg" href="test_image"></div>
            </a>
            <a href="test_image.jpeg">
                <div class="image"><img src="test_image.jpeg" href="test_image"></div>
            </a>
            <a href="test_image.jpeg">
                <div class="image"><img src="test_image.jpeg" href="test_image"></div>
            </a>
            <a href="test_image.jpeg">
                <div class="image"><img src="test_image.jpeg" href="test_image"></div>
            </a>
            <a href="test_image.jpeg">
                <div class="image"><img src="test_image.jpeg" href="test_image"></div>
            </a>
            <a href="test_image.jpeg">
                <div class="image"><img src="test_image.jpeg" href="test_image"></div>
            </a>
        </div>
                
   

Answer №1

If you want the images to scale and fill the container, you can achieve this by setting justify-content: stretch on the row div, adding flex-grow: 1 to the image container div, and setting width: 100% on the image itself. To ensure there are no more than 3 images per row, you can also set a min-width of around 30% on the image div.

.row {
    display: flex;
    flex-wrap: wrap;
    justify-content: stretch;
    align-items: center;
    margin: 150px 40px;
}

.image {
    padding: 0 20px 40px;
    min-width: 25%;
    flex-grow: 1;
}

img {
    width: 100%;
}

To see an example in action, check out this fiddle (note that it may be easier to visualize with a larger container size): https://jsfiddle.net/817o6r0f/

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

What are some ways to reuse an angular component multiple times?

I am utilizing an angular component to dynamically load charts into my widget: Below is an example of the component: angular.module("generalApp").component("chartPie", { template: "<div class=Pie></div>", bindings: { data: "=", }, control ...

What are some alternative methods for downloading the latest file version without relying on the client cache?

On my webpage, I have a table displaying users' data. Each row represents a user and includes their business card, which is a clickable tag that opens a PDF file stored on the server. <td class="business_card"> <a href="/static/users_doc ...

How to change the video source on Internet Explorer

When setting the source of a <video> tag with JavaScript: $("#video-player").attr("src", '/DownloadCenter/GetFile?path=' + file.Path); Initially setting the source is not an issue, but using the same snippet multiple times results in the ...

Is there a way to consolidate the following code into a single <div> tag?

Currently, the layout looks good when both showLanguageControl and showFaxNumber are true. However, if only showFaxNumber is true, the positioning is completely off, even if showLanguageControl is hidden. Is there a way to combine these two conditions? If ...

Ordering styles in Material UI

I've spent countless hours customizing this element to perfection, but the more I work on it, the more of a headache it gives me. The element in question is an outlined TextField, and my focus has been on styling its label and border. Initially, I th ...

Is Jsoup receiving the HTML incorrectly?

Currently, I am utilizing Jsoup to fetch HTML data from a website and attempting to display it in a ListView within my app. Although the application compiles without any errors, clicking the button results in an empty ListView. In my opinion, this issue ...

Creating a 3D-looking border using CSS

I'm looking to create a 3D frame border for an image using a specific texture. I want it to look like this example: https://i.sstatic.net/jdJHk.jpg I attempted to achieve this effect with the following code: <style> .woocommerce-product-gallery ...

Disabled Carousel Navigation Controls in Bootstrap 4

After following the official Bootstrap tutorial, I attempted to set up a carousel containing three images. Despite changing the links for demonstration purposes, the issue persisted. While the navigation arrows seemed functional, the image itself remained ...

Including a Glyphicon button next to a list group item

When using the standard bootstrap list-group-item: <div class="list-group-item list-group-item-action flex-column align-items-start"> <div class="d-flex w-100 justify-content-between"> <h5 class="mb-1">List group it ...

Ways to integrate html and php together

I'm currently working on integrating PHP with an HTML form. Here is a snippet of my code: <? php $path = "books /"; $books = opendir ($path); while (($book = readdir ($books))! == false) { if (substr ($book, -4) === ".txt") ...

Remove the <select> highlight specifically for Internet Explorer

Looking for a way to remove the blue highlight in Internet Explorer. I am working with a select tag and whenever an option is chosen from the dropbox, the blue highlight shows up. Unfortunately, I don't have access to IE on my Mac so I can't test ...

Interactive dropdown menu with options for different actions

They say a picture is worth a thousand words. I have an idea for a feature I want to add to my Django web application: I want to create a dynamic dropdown list that allows users to add values to the database and dynamically updates itself with the new sel ...

What is the best way to address an issue involving Django Template Language's {{ form }}?

<div class="form-title"> {% if form.errors %} <p>{{ form.errors.error }}</p> {% endif %} <form action="{% url 'login' %}" method="post" class="fs-title"> ...

Is jQuery Mobile Autocomplete's search feature activated after typing 3 letters?

Hello there, I am in the process of creating a website and have implemented an autocomplete field. However, I am facing an issue due to having over 1000 <li> elements within the <ul>, making it slow especially on mobile devices. I would like t ...

Tips for modifying HTML attributes in AngularJS

I am looking to modify an attribute of a div using AngularJS. While I am familiar with how to do this in jQuery, I am not sure how to achieve it in Angular. Here is the HTML code: <div ng-app="test"> <div ng-controller="cont"> < ...

Preload-webpack-plugin does not support pre-fetching files

I have a query about prefetching and preloading content. In my vue app, I noticed that after building, I have duplicate files loaded in my dist/index.html file. Here is an example: Additionally, the "scripts" are not being preloaded/prefetched as expec ...

Displaying a dynamic splash screen during the resource-loading process on an Android application

I would like to implement an animated image (currently using a set of PNGs) as a splash screen while my resources are loading. I have successfully displayed the splash screen using this method. However, the issue I am facing is that the splash screen appe ...

Is there a way to quickly add children to a div all at once?

I'm looking for a solution where an element is first replaced with a div, and then that div has some children added using append(): $(el).html('<div></div>').append('<span></span>').append('<span> ...

When the ID and anchor link within a URL fail to scroll to the specified anchor location

I'm trying to add an id to a URL and scroll to an anchor link <a name="tags" id='tags'></a> <a href="edit.php?id=382#tags">GO</a> I have also attempted the following: <a href="edit.php?id=382&#tags">GO& ...

Alter the background image of a DIV based on the selected menu item

I am working on a project that involves a div element with the class "jumbotron" which currently has a background image applied to it. Additionally, I have a menu consisting of 6 items that I would like to interact with. My goal is to dynamically change ...