Creating a dynamic webpage: Adjusting image sizes to match changes in outer div dimensions

Currently, I am working on a web page that will feature several small images. My goal is to display these images in two columns and have them adjust their size as the window dimensions change.

Feel free to check out my jsFiddle:

Upon resizing the window, you may notice that the images move around to accommodate different column numbers based on the window width. However, I actually want the images to resize so that there are consistently two columns displayed at all times.

Initially, I thought setting the image width to 45% would do the trick... but it seems more complex than I anticipated.

Here's the snippet of code from the Fiddle:

<div id="Thumbs">
<ul>
    <li>
        <img src="http://goo.gl/LXa6K">
    </li>
    <li>
        <img src="http://goo.gl/LXa6K">
    </li>
    <li>
        <img src="http://goo.gl/LXa6K">
    </li>
    <li>
        <img src="http://goo.gl/LXa6K">
    </li>
    <li>
        <img src="http://goo.gl/LXa6K">
    </li>
    <li>
        <img src="http://goo.gl/LXa6K">
    </li>
</ul>

-

#Thumbs ul {
list-style: none;
}
#Thumbs ul li {
float:left;
margin: 10px 0 0 5%;
}
#Thumbs ul li img {
/*
How can I properly resize these images?
*/
}

Answer №1

Make sure to adjust the height and width CSS properties to 100%.

#Thumbs ul li img {
    height: 100%;
    width: 100%;
}

For a visual reference, check out the modifications here.

Observe the difference in display by comparing these screenshots:

Answer №2

To ensure the images fill up the entire parent container, try setting their size to 100%:

#Thumbs ul li img {
    width: 100%;
    height: 100%;
}

Disclaimer: This code has not been tested yet!

Answer №3

http://jsfiddle.net/johnkoht/jHm8w/3/

One way to improve the layout is by setting the image width to 100%:

#thumb ul li img {
  width: 100%;
}

The li styles have been updated as follows:

#Thumbs ul li {
  display: inline-block;
  margin: 10px 0 0 5%;
  width: 44.6%;
}

Although the reasoning behind the original li margins of margin: 10px 0 0 5%; is unclear, I have retained it and made adjustments to display and width. To align the right column to the right side, you can add:

#thumbs ul li:nth-child(2n+2) {
  float: right;
}

I hope these changes prove useful to 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

Struggling with setting margins effectively in Bootstrap for the desired layout

I am attempting to create two columns with space between them, but whenever I try to add a margin-right, one of the columns goes down. Can someone please assist me? <div class=row> <div class="boxi col-6"> Content here ...

Incorporate an image icon into an Angular grid

Currently, I am in the process of building a web application using Angular. The main goal is to create a grid and color specific cells based on data input. Below is the snippet of my HTML code: <mat-grid-list cols="10"> <mat-grid-tile * ...

Creating Sliding Panels: A Step-by-Step Guide

I am new to JavaScript and jQuery, and I am looking to create a panel system similar to Spotify's design. Here is a description of what I am trying to achieve: When a user clicks on an artist or song/album on Spotify, a panel slides in from the righ ...

The functionality of both P and H1 tags seems to be malfunctioning

Can someone help me with changing the font family and size of my paragraphs and headings (p and h1)? Any assistance would be greatly appreciated. Feel free to review my code for any other errors as it has been a while since I last coded. html, body { ma ...

Issues arise with the functionalities of FireFox related to transformations and transitions

I've been working on my CSS and here is what I have: img.buttonImg { -webkit-transition: all 0.5s ease-in-out; -moz-transition: all 0.5s ease-in-out; -o-transition: all 0.5s ease-in-out; -ms-transition: all 0.5s ease-in-out; transition: ...

Is it possible to utilize ng-show in conjunction with display:none?

Check out my plunker. I'm facing an issue where even if the ng-show is set to "false", there is always a brief display for a few milliseconds when I refresh the page. I understand that this happens because Angular hasn't loaded yet, so I tried us ...

The problem arises when trying to use the Jquery click event on buttons that have been generated

I've encountered an issue where I can create components dynamically, but the click event doesn't seem to work on buttons that are dynamically generated. Check out the code snippet below: HTML file: <div class="merge_sections">&l ...

Encountering difficulties with image processing on a web page

Recently, I've been experimenting with uploading an image and converting it to a grayscale version on my webpage. Oddly enough, the javascript code works perfectly when tested locally but fails to generate the grayscale image once integrated onto the ...

Show the div as a sidebar exclusively on mobile devices by utilizing bootstrap

Is there a way to have a div act as an overlay sidebar only on mobile devices, but function as a normal div on all other devices? Looking for a solution similar to the following sample code: <div class="row"> <div class="col-lg-3" id="sidebar ...

Display the element only when the input is in a selected state using CSS

Looking for a way to display an element when an input field is selected without using JavaScript? Preferably, the solution should not involve placing the element within the input field. Unfortunately, targeting the element in CSS with the :active selector ...

"Trouble arises when attempting to render a basic Vue.Draggable component within a nested ul

Problem with Nesting After receiving feedback from @Amaarrockz, I attempted to create a sandbox to address the issue I'm facing. However, the problem persists. Here's the code snippet: <div id="app"> <ul> <li ...

CSS for Repeating Header Image

My header image is repeating at the top of the page, but it's not displaying in full width and leaving blank space on the sides. How can I fix this issue? Here's my current code: <!DOCTYPE html> <style> .bgimg{ ...

Conceal Element without Eliminating from the Design

Need a solution: I have specific icons to display for certain elements, but not all. However, when hiding the icon, I want the spacing of the element to stay consistent. Is there a method to conceal an image within an element while preserving its allocat ...

The simple method for incorporating line feed in <s:textarea>

Hey there, I'm currently utilizing struts 2 UI and I seek a means to restrict the number of characters in each row to only 16. Additionally, I want to impose a maximum length limit of 32 characters. Despite my attempts using the 'row' and &a ...

The dropdown menu with Bootstrap's <li> sub menu is not expanding as expected

<li class="dropdown"> <a href="#" class="dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false">Utilitas<span class="c ...

Incorporate SCSS capabilities into a Vue project

The default content of my packages.json file includes the following: "postcss": { "plugins": { "autoprefixer": {} }} Whenever I try to compile using <style lang='scss'>, it doesn't work automatically like it does for Typescript. I ...

The padding on this button seems to be arbitrary and nonsensical

I attempted to create a button with a red border that is positioned at the bottom and center, but the padding seems to be interfering with my design. It appears that with the border, the width extends to 100% which doesn't make sense. I have tried va ...

Tips on vertically centering a div within another div

Greetings, I wanted to share the code snippet I am currently using: https://jsfiddle.net/hbahar95/27/ .text.ellipsis { position: relative; font-size: 14px; color: black; font-family: sans-serif; width: 250px; /* You can adjust this as needed ...

Ensure that the content remains at the center of the screen, but is dynamic and changes

I have a unique concept I want to experiment with, but I'm unsure of the best approach to take. My idea involves centering content on the screen and instead of having it scroll off and new content scroll in, I would like it to stay centered and smoot ...

Loading a Threejs model: "The CORS policy has blocked access to XMLHttpRequest from origin 'null' - How can I test this locally? Or should I simply upload it?"

Experimenting with three.js locally on a single HTML page, I am interested in exploring loading and manipulating 3D object files. Here is the code snippet that I am currently using: var loader = new THREE.AMFLoader(); loader.load( '. ...