Adjust the size of images that are aligned horizontally using HTML

I am facing a simple issue that I just can't seem to solve. My problem involves two images aligned horizontally within a div container. Each image has specified width, minimum and maximum widths, while the parent div's width is set to 100%. How can I make both images scale down proportionally when the parent width is reduced to 50% based on the screen width? Here is an example:

<!-- screen resized to 110px in width -->
<div style="width:100%; display:block">
  <!-- actual image width is 200px -->
  <img src="i.png" style="width:100%; min-width:100px; max-width:200px">
  <img src="i.png" style="width:100%; min-width:100px; max-width:200px">
</div>

The current setup causes the second image to be pushed down if the parent container cannot accommodate it horizontally. Setting white-space to nowrap keeps them inline but does not scale them down. Can anyone provide some assistance with this issue?

Answer №1

Are you looking for a similar solution?

Here is the demo on jsFiddle: http://jsfiddle.net/jplahn/4h9Vy/

Feel free to adjust the width of the parent container in the provided jsFiddle.

Snippet of HTML code:

<div id="parent">
  <!-- the actual image width is set to 200px -->
  <img class="image" src="i.png" >
  <img class="image" src="i.png" >
</div>

Corresponding CSS styles:

.image {
    display: block;
    float: left;
    max-width: 200px;
    width: 50%;
    position: relative;
}

#parent {
    overflow: hidden;
    width: 100%;
    display: block;
}

Answer №2

To ensure the images maintain a minimum width of 100px, it is important to note that if the width of the div falls below 200px (specifically 110px in this case), the second image will move to the next line. If you wish to reduce their size, consider removing the min-width: 100px property from the images and instead setting their width to 50%. This way, with a width of 100%, they will adjust according to the parent container.

Furthermore, there is an opportunity for optimization in your code. The rules width:100%; display:block are redundant as these are default settings for all block elements, including div.

Feel free to experiment with the suggested modifications here: http://jsfiddle.net/TMyr9/2/

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

Calculating the number of checked checkboxes using PHP and HTML

Greetings! I am just starting to learn php and I have a question about how to count the number of 'checkbox' items that are checked when I click on a submit button. Here is an example: <input type = "checkbox" value = "box" name = "checkbox1 ...

Utilize the atan2() function to rotate a div so that it follows the movement of the mouse

I am trying to create an effect where the mouse aligns with the top of a div and the div rotates as the mouse moves. The rotation should happen when the top part of the div is in line with the mouse cursor. My goal is to achieve this using the atan2 functi ...

Is the jQuery popup malfunctioning? It appears to be stuck within a div

Currently, I am in the process of developing a website at and I am facing an issue with the hover type menu. Whenever I click on the arrows next to the logo, instead of fully opening up, the menu seems to be getting stuck and is not expanding entirely. I ...

The order in which jQuery is loaded can impact the layout of

I am facing an issue with my jQuery code that loads status to a profile page using a ul with li status items. The each() function retrieves items from a JSON callback, and the load() function is supposed to ensure that the image is available before creatin ...

`inline-block elements are centered when displayed in Firefox`

I have a question regarding formatting h2 and h3 elements to display as inline-block. Below is the code snippet: <div id="content" class="content-width"> <h2 class="headline"> My Headline </h2> <h3 class="subheadline"> My S ...

What is the method for setting the I-bar cursor when text is being hovered over with custom cursors in use?

Currently, I have implemented a custom cursor set on my website in order to override the unattractive AA cursors found on most modern Operating Systems. Although I have successfully applied the custom pointer/cursor, I am encountering some challenges wit ...

Add class or id dynamically when clicked

How can I dynamically change the CSS color of a roller-banner div based on user input? I have a codepen setup where I want to capture the class of the clicked element (e.g. colour3), and then apply that as an ID or class to the roller-banner div. Codepen ...

Is it possible to use traditional form submission after dynamically adding an HTML control with JQuery?

I just need some clarification! If I have added an HTML control (input, dropdown, etc.) after the page has loaded using jQuery, can I still submit a form using the traditional method (HTML submit button)? Or do I have to submit all HTML controls using an ...

Tips on triggering ng-click event before the page finishes loading

Is there a way to have the "edit" button appear in a "clicked" state when the page first loads? <div class="buttons" ng-show="!rowform.$visible" style="width: 20%"> <button class="btn btn-primary" ng-click="rowform.$show()">edit</butt ...

The foundation grid system is not being implemented

How come my div is not affected by the foundation grid system: render: function(){ var {todos,showCompleted,searchText} = this.state; var filteredTodos = TodoAPI.filterTodos(todos,showCompleted,searchText); return ( <div> ...

The navigation bar isn't turning into a hamburger menu - using bootstrap and Flask

My mobile responsive navbar is not rendering correctly on smaller screens. Check it out here: I am using Bootstrap Material Design with Flask and this is the code snippet for my nav-bar: <!-- Navbar --> <nav class="navbar navbar-expand-lg na ...

Shifting image from center to corner as you scroll

Hello there, I'm new to this so please bear with me for any obvious mistakes, thank you :) My goal is to have an image move from the center of the screen to the corner of the screen as you start scrolling. I've made some progress on this, but I& ...

Refresh the CSS inheritance and hover effects

Facing an issue with my web project where I am using CSS. At times, I need to reset the inheritance from the parent class for certain elements. Although I have defined some classes to style my elements, I want to completely reset all styles except for hove ...

I am looking to enhance my email subscription form by incorporating a URL into the subscription button, all while ensuring that the email subscription function in the HTML remains fully operational

Is it possible to maintain the functionality of an email subscription form while adding a URL to the subscription button in HTML? I need help modifying the code of the button. ...

The background image is not displaying as expected

Despite my familiarity with html/css, I am encountering an issue while experimenting with background-image:url(). Strangely, nothing seems to show up. My html is quite basic for testing purposes, and I have confirmed that my image path name is correct. Des ...

IE displays div height differently compared to Firefox and Chrome

Having trouble adjusting the height of a div tag in different browsers? Need help fixing this issue? Check out the CSS code snippet below and visit the site here for more information. #payment{ width: 265px; border: 1px solid #cecece; border ...

Items within a shared container are currently displaying stacked on top of each other

I am facing an issue with my bike images and manufacturer names appearing on top of each other instead of next to each other. Despite using "col-md-12", the grid columns are not aligning horizontally as expected. How can I adjust the code so that each imag ...

Tips for creating gaps between list items in a collapsible navbar

I am attempting to add spacing between the li items in my collapsible navbar However, the issue arises when I slightly minimize the browser window, causing the navbar to collapse incorrectly. See image below: https://i.sstatic.net/TdKb4.png Below is t ...

Picture failing to display in Gmail email message after sending it from ASP.NET using C# NET.Mail as the email body

After generating the QR code, I faced an issue where it was not displaying in the Gmail message despite appearing correctly in HTML visualizer. Here is the code snippet that generates the QR code and tries to send it via email: public void generate_qrcode ...

Here is a guide on determining the date variance in PHP by leveraging jQuery

I have been attempting to use the jQuery change function to calculate the variance between two dates, but I am encountering a problem as the code seems to be running smoothly without returning any results... <input type="text" name="datte1" class="form ...