What is the best way to ensure that 4 divs adjust dynamically to take up 25/50/100% of the width when resized below their minimum

Imagine you want to showcase 4 images, all with square dimensions, each taking up 25% of the width. As you resize the window and they get smaller, ensure they can shrink down to a minimum width of 200px.

However, if they need to be made even smaller, have the third and fourth images move below the first two, and make them take up 50% of the space each. Again, they can be resized to a minimum width of 200px.

If the size is reduced further than 200px, then adjust it so only one image is displayed at a time, each taking up 100% of the width beneath the previous one.

I've been experimenting with some styles that are close to what I'm looking for, but I can't quite achieve the desired result. Any suggestions?

Here are the styles:

html, body {
    margin:0;
    padding:0;
    width:100%;
    height:100%;
}

.container {
    width:100%;
}

.column {
    width:25%;
    float:left;
    min-width:200px;
}

.column img {
    width:100%;
    height:auto;
}

And here is the HTML:

<div class="container">
    <div class="column">
        <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />
    </div>
    <div class="column">
        <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />
    </div>
    <div class="column">
        <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />
    </div>
    <div class="column">
        <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />
    </div>
</div>

https://jsfiddle.net/au53h9jg/

Answer №1

Utilize media queries to accomplish this task, you can find the updated fiddle here.

html, body {
    margin:0;
    padding:0;
    width:100%;
    height:100%;
}

.container {
    width:100%;
}

.column {
    width:33.33333333%;
    float:left;
    min-width:200px;
}

.column img {
    width:100%;
    height:auto;
}
@media only screen and (max-width: 600px) {
  .column {
    width:50%;
    float:left;
    min-width:200px;
}
  
}
@media only screen and (max-width:400px) {
  .column {
    width:100%;
    float:left;
    min-width:200px;
}
  
}
<div class="container">
    <div class="column">
        <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />
    </div>
    <div class="column">
        <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />
    </div>
    <div class="column">
        <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />
    </div>
</div>

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 is the best situation to utilize $(document).ready()?

After observing that using $(document).ready(..) seems to introduce a noticeable delay in applying JavaScript effects, I've been contemplating the best approach. I know that simply placing the effect in a <script> tag may not work if the DOM isn ...

The fade effect on an element cannot be achieved using setTimeout

I'm trying to call a tooltip and hide it after 9 seconds, but for some reason, it's not working as expected. The issue I'm facing is that the "tooltip" only hides after it initially loads. It doesn't respond when I click on the "Touch ...

Is there a way to translate an array into an HTML grid layout?

I am currently working on mapping an array into image sources to create an image gallery. The issue I am facing is that it only maps down one column, and I am unsure how to make it fill the other columns as well. Any suggestions or tips would be greatly ap ...

angular.js:13920 Alert: [ngRepeat:dupes] Multiple occurrences in a repeater

I encountered an issue while attempting to parse a JSON file and map it in HTML. Here is the JavaScript code snippet: searhController.orderlogs.results = JSON.stringify(response.data); This is how it's implemented in Angular: <tr ng-hide="searh ...

The onClose event is indeed triggered by the Material-UI Popover component

Utilizing the Material-ui Popover component, I have encountered an issue with its opening state. I created a function called handlePopoverClose to manage the closing action when clicking outside of the 'Hover with a Popover' element. The intentio ...

What is causing the change in the watcher's source?

How can the source of a change to a watched property be detected in AngularJS? For instance, suppose I have the global value: let watchMe = 0; and it is being watched in a component as follows: $scope.$watch(() => { return watchMe; }, (newValue, o ...

Tips for obtaining the retrieved URL from an ajax call

How can I extract only the returned URL from an ajax request? I have tried implementing it like this: $.ajax({ type: "GET", dataType : "jsonp", async: false, url: $('#F ...

Storing the value from the INPUT field in the state of React is not permitted

I'm attempting to retrieve the user input value from the INPUT field and update it in the corresponding state. However, no matter what I type in the field, it doesn't get set to the state. createForm(x){ var dx = 'd'+x let da ...

Verify the tags associated with an element that has multiple tags

For a test I am conducting, I need to examine the tags and styles present in a specific element. The element, displayed on the page as bold, italic, and centered, looks like this: <p style="text-align: center;"> <em> <strong>TE ...

Navigating through JSON arrays with Node.js

I have been given the task of iterating through a complex JSON file that contains an array of JSON objects. I am finding it difficult to access the array object within the JSON file. Specifically, I need to access the "class-name" object from the JSON f ...

Mixing without Collections

After initially posting this question yesterday, I realized that I needed to clean up my code before proceeding. However, for my assignment, I am required to create a JavaScript quiz where the questions and answer choices are shuffled every time a user ret ...

Creating a CSS dropdown menu - Placing a before element in front of the select/option element

How can I position a CSS :before element in front of the foreground content? I am trying to move the text behind the gray area where the dropdown arrow is located. I have already attempted to set the z-index of the before element to 2 and the actual eleme ...

What are some methods to implement overflow:hidden for stacked images inside a div?

I currently have two images stacked on top of each other within a div element as shown below: <div class="container"> <img src="somepic.jpg" class="layer" /> <img src="otherpic.jpg" class="layer" /> </div> Styling is set u ...

Instructions for implementing tooltips on a pie chart slice when hovering with the mouse pointer, using the canvas

var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var cw = canvas.width; var ch = canvas.height; ctx.lineWidth = 2; ctx.font = '14px verdana'; var PI2 = Math.PI * 2; var myColor = ["Gr ...

Timing of JQuery FadeOut is incorrect

Here is some code I am working with: $(document).ready(function () { $("#full-btns").children().delay(4000).fadeOut("slow"); $('#full-btns').hover(function() { $('#full-btns').children().stop().animate({opacity:'100'} ...

Getting the text value from a table in JavaScript is a straightforward process. By using

I am working with a table displaying available hotel rooms and want to change the text color to green if the room is marked as "available." Is there a way to check the innerHTML of a td element to see if it contains the word "available"? var status = do ...

What is the best way to retrieve the ID of a <tr> or <td> element?

When working with JavaScript, I have created a dynamic table where each row is structured as follows: <td id="user[i]['id']">user[i]['name']</td> Within this structure, I am seeking to extract the value user[i]['id&ap ...

What is the process for accessing the theme spacing unit in MUI 5?

In previous iterations of MUI, accessing the theme spacing unit was possible through theme.spacing.unit, with a default output of 8. However, this property has been removed in MUI 5. I am having trouble finding documentation on how to access the theme sp ...

Problem with using Jquery DIY slider for multiple purposes

There seems to be an issue with the DIY Slider that I'm using. Whenever I attempt to use this slider more than once on a single page, the second one fails to function. I have exhausted all possible solutions that come to mind. $(".slider").diyslider ...

Implementing route prefix in a combination of express and react deployment

I'm currently facing a challenge while trying to deploy a react app that utilizes router functionality with express. The react app is expected to be accessible via the /dashboard route. At first glance, everything seems to be working smoothly on the ...