Problem with responsive images inside Flexbox

I encountered an issue with a code snippet involving a flex div containing an img:

html, body {
  height: 100%;
  margin: 0;
}
.container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: black;
}
.item {
  max-height: 100%;
  max-width: 100%;
}
<div class="container">
  <img src="https://i.sstatic.net/aHBI2.png" class="item">
</div>

While resizing the window vertically in Chrome and IE, I noticed that the image was only responsive in the vertical direction. The width did not adjust proportionally to the decreasing height. However, this problem did not occur in Firefox.

Have you come across this issue before?

http://jsfiddle.net/z2v9o9ew/1/

Answer №1

If the container is going to take up the entire screen size, consider using viewports units. Remember to set the height for both html and body tags as well.

Check out this example: http://jsfiddle.net/hsn43uzq/

html, body {
    height: 100%;
    margin: 0;
}
.container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: black;
}
.item {
    max-width: 100vw;
    max-height: 100vh;
}
<div class="container">
    <img src="http://dummyimage.com/500" class="item" />
</div>

Alternatively, you can use css transform instead of flex to center the image:

Example here: http://jsfiddle.net/5k84mfft/

html, body {
    height: 100%;
    margin: 0;
}
.container {
    width: 100%;
    height: 100%;
    background: black;
    position: relative;
}
.item {
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
<div class="container">
    <img src="http://dummyimage.com/500" class="item" />
</div>

Edit: Another solution is adding flex-direction: column;, which seems to work despite not fully understanding why it does the trick.

flex-direction: column;

See the updated demo here: Updated demo along with the comments section.

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

Creating margins between columns in Bootstrap can easily be accomplished by adjusting the padding or gutter settings

For my Bootstrap page, I'm trying to create 4 columns with a space of 5px between each one. I found a tutorial ( ) but it's not working as expected because the clickable area extends beyond the column due to the margin. Here is the link to my j ...

"Ensuring that the embedded PDF in Bootstrap is not taking up the

I'm encountering an issue with the embedded pdf on this page for sponsorships. The previous site version was in html4, and the embedding method used the object tag as shown below: <object data="pdf/GreekFestSponsorship2013.pdf" type="application/p ...

The counter up function pauses when scrolling and displays an error message

I keep getting this error whenever I try to scroll up towards my counter function -> "Uncaught TypeError: Cannot read property 'shift' of null at f (jquery.counterup.js:62)". Any suggestions on how to rectify this? Below is the code ...

Issues with NextJS CSS modules: unexpected behavior or not functioning as expected?

As a total amateur in web development, I kindly ask for your understanding :) I have been tinkering with NextJS and had set up a project some time ago. Using CSS modules sporadically, everything was functioning perfectly until just recently when I reopene ...

Matching Javascript Variables to CSS Styles

I am currently developing a small HTML page and I am utilizing JavaScript to retrieve the screen dimensions. However, I am facing an issue with passing this variable to a CSS style as shown below. How can I achieve this successfully? Additionally, I have ...

Saving an Echo Command in a Variable

I've set up an eye exam program and now I'm trying to figure out how to display the results only after the exam is completed. The issue is that when I echo the stored SESSION variables containing the results, they are displayed even on page refre ...

Styling with CSS3: positioning floating divs on top of a non-floated div

Checkout this fiddle here: http://jsfiddle.net/PA5T5/1/ I've got a setup with 3 divs - 2 are floated whereas one is not. The two floating divs are positioned on top of the non-floating div. I'm wondering if this approach is correct or if it&apos ...

Utilizing the enter key function for form submissions on lists in AngularJS

In my AngularJS project, I am working on an account details page where users can update their personal information. The interface allows for multiple phone numbers and email addresses to be added. Currently, the functionality works well with mouse input or ...

Steps for configuring UI-Router to open a new tab

I have a link on the homepage that directs users to a second page with two tabs. I want the second tab to be active by default so that users can see its content immediately. I tried using ui-sref, but it only takes me to the second page without activating ...

Column 1 with fixed headers

Is there a way to make sticky headings in a single column scroll without them overlapping? Currently, my h1s are overlapping when I scroll down. Update: I would like the headings to stack on top of each other as I scroll, instead of being pushed off scree ...

What is the best way to select just the innermost row of a nested table for emphasis?

I am working on a project with multiple nested tables and I am looking for a way to highlight the innermost row below the mouse pointer. How can I achieve this? Just to provide some context, I am using nested tables to present recursive tabular data, with ...

Tips for implementing CSS media queries on video posters

After implementing the solution from this helpful answer, I have successfully used video[poster]{object-fit:fill} in my css to prevent distorted poster images in html5 videos. Now, I am looking to use CSS to display different posters for a video based on s ...

Locate tags utilizing CSS selectors excluding their children elements

Is there a way to only find tags that meet certain conditions on the first level, without including their children or grandchildren? I am currently using Selenium with Python. <div id="example1"> <div id="example2"> </div> </ ...

Removing border of the top and bottom of jspdf pages

In my project, I am utilizing a combination of html2canvas, canvg, and jspdf to convert HTML content (which includes SVG graphs) into canvases for the purpose of creating a PDF file. To address some page-break issues, I have resorted to creating multiple c ...

Exploring the versatility of CSS variables in JavaFX

In need to style four buttons in a consistent way, but each one should have its own unique image. Three styles are required: one for normal state, and the others for hover and focused states. Using CSS for this task would involve a lot of repetition, as ...

The Bootstrap navbar-fixed-top is failing to function properly within the confines of my "content" div

My goal is to utilize the bootstrap navbar-fixed-top within my id="content" div. However, when I apply the navbar-fixed-top class like this: <nav class="navbar navbar-default navbar-fixed-top navbar-shadow"> The navbar ends up on top of everything ...

Bootstrap causing partial failure in PHP script

I encountered an issue while trying to style a PHP file named dashboard.php using Bootstrap. Initially, I only included some HTML code in the file. Upon testing it, I noticed that the styling within the 'ul' tag was incorrect. However, other clas ...

Transferring Information Between HTML Text Fields

Within my C# project on Visual Studio, I am utilizing an external HTML resource to populate web pages by passing in specific areas of the resource into the C# code. While this method works well for most inputs, I am facing a challenge when attempting to pa ...

Different body background color changes every second

I have a function called makeRandColor that generates a random color using RGB and template string literals. However, I am struggling to figure out how to make it work every second. I tried using setInterval but it doesn't seem to be functioning prope ...

Expanding canvas due to the utilization of column-count

I am currently working with chart.js and have a setup that includes two pie charts and a bar chart that I would like to display in a single row. In order to achieve this layout where the bar chart is larger than the pie charts, I implemented the following ...