Photos inside a flexbox do not resize correctly

When placing 2 images inside a flexbox with the column direction, I anticipated that the images would resize accordingly when adjusting browser window size. However, this is not the case.

Regardless of the CSS styles applied, I am unable to maintain the original ratio of the images as desired.

Although resizing the window horizontally works as intended, vertical adjustments do not trigger the expected image response.

In addition, the images extend outside of the flexbox div area. How can I prevent this from happening?

#box{
    width: 100%;
    height: 100%;
}
#container {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 30%;
    background-color: red;
}
#photo {
    width: 100%;
    height: auto;
}
<div id="box">
  <div id="container">
    <img id="photo" src="1.JPG">
    <img id="photo" src="2.JPG">
  </div>
</div>

Answer №1

In situations like this, I often opt to set the image as a background image using CSS and utilize background-size:contain;

Here's an example:

#box{
    width: 100vw;
    height: 100vh;
}
#container {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 30%;
    background-color: red;
}
.photo {
    width: 100%;
    height: 100%;
    background-repeat: no-repeat;
    background-size:contain;
}



<div id="box">
  <div id="container">
    <div class="photo" style="background-image: url('https://picsum.photos/200/300/?random')"></div>
    <div class="photo" style="background-image: url('https://picsum.photos/300/200/?random')"></div>
  </div>
</div>

https://jsfiddle.net/vbanj8st/

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

Ways to stop button from wrapping inside a div

While working on a search page, I encountered an issue where the submit button overlaps with the text input when zooming in due to lack of space. Is there a solution to prevent this overlapping and keep the layout intact? HTML <input type="text> &l ...

Assign a background image to a button using an element that is already present on the page

Is there a way to set the background-image of a button without using an image URL? I am hoping to use an element already in the DOM as the background-image to avoid fetching it again when the button is clicked. For example, caching a loading gif within the ...

Generate a blank image using the canvas.toDataURL() method

I've been attempting to take a screenshot of this game, but every time I try, the result is just a blank image. var data = document.getElementsByTagName("canvas")[0].toDataURL('image/png'); var out = document.createElement('im ...

Deactivate the dropdown menu without triggering any event

I am working on a JSP page and I need to find a way to disable a dropdown list without using an onclick method or event in the dropdown menu itself. <% boolean condition = true; if(condition){ // Here, I need to access 'plc1' ...

Utilizing the ternary operator in Angular HTML templates

Using the ternary operator in HTML can simplify your code: <div> {{ showStringOneFlag ? 'Display String 1' : 'Display String 2' }} </div> This approach could save you from setting a string variable multiple times in JavaSc ...

What is the process for including icons on buttons?

I have been researching how to use Font Awesome software to incorporate icons into my HTML elements, but I am uncertain about the implementation process for my specific case. Currently, I have created a basic webpage with buttons and I would like to includ ...

The performance of my Wordpress site is being hindered by the slowdown caused by Admin

I have a Wordpress website focused on winter sports vacations in Iceland: link Every plugin, WordPress core, and theme are up to date. Using Woocommerce for my online store. However, activating the Woocommerce plugin causes the site to slow down signific ...

I'm encountering an issue trying to launch bigbluebutton HTML5. I attempted to initiate it using "npm start" after installing it with "meteor npm install," but unfortunately, it failed to

I've been attempting to launch the Development environment for bigblubutton-html5, but unfortunately, I encountered an error message after executing this command: npm start Please refer to the tutorial I used: Here's the error message that was ...

Is it possible to customize the border spacing for individual cells?

My table resembles the one shown in this example I am looking to eliminate the gap between each "Previous" and "Current" cell pairs while still maintaining spacing between rows and other columns. Is there a way to achieve this? ...

Adjust the left and right margins while maintaining the vertical margin spacing

Is there a way to apply horizontal margin while inheriting vertical margin without explicitly setting margin-left and margin-right? I was hoping for something like margin: inherit 20px; to be effective. Take a look at the example here: https://jsfiddle.ne ...

Execute the JavaScript [ window:print() ] function once the webpage has finished loading along with any

I am facing an issue with my web page that has around 10 Google Analytics charts. I want to trigger a print dialog box once the page is fully loaded, including all the charts and JavaScript. Currently, the problem is that the print dialog box opens after t ...

retrieve the index from the chosen elements

Hey there! I'm facing a situation where I have a div with several child elements. Using jQuery, I want to find the index of a specific element within a certain selector. <div> <div class="red"></div> <div class="red"></ ...

Why use @media (max-width:-1) in your code?

While reviewing CSS code created by another department, I noticed an interesting media descriptor: @media (max-width:-1) { ...standard css stuff here } The value of max-width cannot be less than 0, so what could be the purpose of this? Perhaps it was ...

Exploring jQuery's ability to retrieve CSS values

Is there a way to use jQuery to read a specific CSS value from a class? The situation is as follows: HTML: <div class="box"></div> CSS: .box { margin-top: 100px; } I want to determine if the .box class has a margin-top of 100px in order t ...

Upon the initial gallery load, the images are appearing on top of each

Currently, I am working on creating a gallery page for my website using the Isotop filtering plugin. To view the live gallery, you can visit: carroofies.com/gallery The main issue I am encountering is with the initial load of the page, where the images ov ...

The Iframe is malfunctioning and not displaying properly

Thank you for the insights on header issues. I have a follow-up question: Is it possible to identify header problems that prevent me from displaying content in an iframe just by looking at the link? If so, can I display a different page for those problemat ...

What is the best way to style multiple classes within the same element (specifically an ordered list ol

Using CSS counters, I have created various styles for an ordered list <ol> by applying different classes and adjusting the styles accordingly. Now, I want the counters to adjust based on screen size, displaying smaller numbers on smaller screens. To ...

Tips for presenting PHP code in a Bootstrap 3 col-sm-4 layout for each set of 3 database entries in a single row

My PHP program retrieves 3 customer records from the database. I want to display the results in a Bootstrap grid view with 3 columns of col-sm-4 in one row. Currently, it displays all the results in a single vertical line. Another requirement is to show ...

Is it required to double click checkboxes to uncheck them?

My checkboxes require 2 clicks to be unchecked, even though one click is enough to check them. This issue occurs in all 7 checkboxes and there is no onchange() function or similar in TS. import { Component, OnInit, Inject } from '@angular/core&apos ...

Instructions on making a Popup appear after a specified duration of mouse inactivity on a webpage

Hello, I am new to web development and could use some guidance on creating a popup window for users to enter their username and contact number. It should be able to store this information in a database and activate after the user has been idle for about 10 ...