Ensure that the pop-up image stays within the window boundaries and maintains its original aspect

IMPORTANT UPDATE: After realizing there are numerous scenarios where my desired functionality may be needed with different specifications, I need to outline my specific case. Essentially, I am utilizing this image popup (view code here).

If you resize the window while a popup is open, you'll notice that the popup does not adjust its size accordingly, leading to a subpar user experience especially in landscape mode on smartphone devices.

I aim to make my popup resize based on both the width and height of the screen without distorting the aspect ratio of the image (maintaining its square shape).

These are the alterations I have implemented so far:

.focus {
  z-index: 10;
  max-width: 500px;
  max-height: 500px;
  display: none;
}

.focus.enabled .container {
  max-width: 500px;
  max-height: 500px;
}

Testing it via this link using Firebug makes the image responsive when reducing the width, but not when adjusting the height of the window. How can I ensure responsiveness in both dimensions while preserving the image's aspect ratio?

----------- Previous inquiry (archived for reference): ----------------

I want to confine an element (specifically a picture) with maximum dimensions of 500x500 within the browser window while maintaining its aspect ratio intact. Here is sample HTML:

<html>
  <head>
  </head>
  <body>
    <img src="myimage.png" class="image" />
  </body>
</html>

Accompanied by CSS:

.image { 
  max-height: 500px;
  max-width: 500px;
  height: 100%;
  width: 100%;
}

With this CSS, the image remains inside the window but warps when either dimension of the window shrinks below 500px. To preserve the ratio, one of the two 100% directives needs to be removed:

.image { 
  max-height: 500px;
  max-width: 500px;
  height: 100%;
  /*width: 100%;*/
}

However, removing one of these renders the ratio correct, but leads to cropping of the image when the window width falls below 500px! What straightforward CSS solution exists for this fundamental issue?

Answer №1

This highlights a practical application for vmin units :

Representing 1/100th of the minimum value between the height and the width of the viewport. (source : MDN)

See DEMO here

Relevant CSS :

img {
    width: 70vmin; 
    height: 70vmin;
    max-width: 500px; 
    max-height: 500px;
}

An important consideration when using these units is browser support, as they are not compatible with IE8- (more information on canIuse)

To ensure compatibility with IE9, use 'vm' instead of 'vmin', for example:

width:70vm; 
width: 70vmin; 
height:70vm;
height: 70vmin;

If vmin units cannot be used, there isn't an established method to maintain aspect ratio of a div based on height in CSS. However, maintaining aspect ratio based on width is achievable using padding techniques discussed in various posts including this reference.

For images, you can implement the CSS rules described above to uphold aspect ratio without restricting image size.

------PREVIOUS ANSWER------------------

If the natural dimensions of the image are 500x500px, specifying max-width/height as 500px is unnecessary.

Employ max-width/height set at 100% and leave width/height with the attribute auto to preserve image aspect ratio and prevent exceeding 100% or 500px width/height limits:

View DEMO here

HTML :

<img src="http://lorempixel.com/output/nature-q-c-500-500-5.jpg" alt="" />

CSS :

img {
    max-width:100%;
    max-height:100%;
    width:auto;
    height:auto;
}

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

Tips on centering font-awesome icons on a webpage

How can I align the icons in a row with center alignment? Here is the fiddle .ft-foot { float: left; width: 100%; padding: 3px 10px; background: #000; } .inclusion { color: white; padding: 5px 0 10px 0; display: block; let ...

Trigger the animation to run at regular intervals with an initial delay before starting and running on every iteration

I am trying to make an HTML button spin in a specific pattern. The button should start spinning after 5 seconds, then rotate for 2 seconds, stop for 5 seconds, and repeat this pattern indefinitely. I have searched for solutions but haven't found one ...

I'm looking to position the vibrant red box at the bottom of the screen, right under the navigation bar. However, every time I try to

I'm struggling to align the full-screen red box under the navigation bar. I've tried using flex properties but it's not working as expected. My goal is to have the red box at 100% width and be responsive. Could it be something with my nav b ...

Images with fixed positions will be displayed in one div and hidden in all others

I am struggling with integrating multiple images into my webpage in a fixed position. These images should only be visible within their designated divs and scroll along with the content, hiding behind other divs. Unfortunately, I can't seem to locate ...

Aligning a broad div within a narrower container div

I'm attempting to position a wide div at the center of a smaller one. Is it possible to achieve this layout? Here is what I have so far: HTML <body> <div id="full_container"> <div id="machine_mask"> <div id="machine_container"& ...

Change the color of child li to white when hovering over the parent li, and vice versa

Let me start off by explaining the issue I'm facing. Take a look at to get a better understanding of what I'm referring to. It needs to be a specific page, not the home page, so you can see the problem for yourself. Now, when you hover over the ...

What could be the reason for my new course not being recognized?

Looking to modify the behavior of a button where, when clicked, it hides a specific div element, changes its text display, and toggles between classes using addClass/removeClass for subsequent click events. Unfortunately, the intended functionality is not ...

The navigation bar spans the entire width of my webpage

I came across this cool man code at and I'm trying to make the menu bar stretch across the entire width of my page. The theme I'm using is called nvidia. You can download the original code by getting the zip file from the link above. Here&apos ...

Add a hyperlink alongside every row in the table

My table comprises three columns in each row, and I am looking to include a link next to each row. <table> <th> Name: </th> <th> Price: </th> <th> Description </th> ...

Exploring the Integration of Metro CSS 3 Form Validation within the MVC Framework

I am looking to implement the form validator feature from Metro CSS 3 in my MVC 5 application. Below is a snippet of my code: @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html. ...

Position the site at the center of the screen for perfect alignment

As I work on building a website, I am striving to achieve perfect center alignment both horizontally and vertically on the screen. However, despite my efforts, I have only managed to center it horizontally so far. After thorough research, the solutions I ...

How can I activate horizontal overflow within a vertically scrollable container?

I created a CSS layout using TailwindCSS to showcase my current issue. While I used Tailwind Playground for quick layout building, feel free to provide your solution using plain CSS. Here is the code and result: https://play.tailwindcss.com/8xynVWIRC7 Fo ...

Changing your logo image as you scroll, using a unique flip animation

Looking to add some animation to my navigation bar similar to the one on . I have successfully implemented a fixed navigation effect in my current code, but I am struggling to achieve the flip logo animation upon scrolling - Any suggestions or assistance ...

What factors contribute to the poorer performance of SVG rendering compared to PNG rendering?

I conducted a comparison of two images across various browsers. One image is an SVG while the other is a PNG format. Here are my findings: You can view the demo on JSFiddle here: http://jsfiddle.net/confile/2LL5M/ This is the code snippet I utilized: ...

In IE11, the fixed position does not depend on the parent container

In Chrome and Firefox, when a transform is defined in the parent container, the child's position will inherit from the parent container. However, this behavior does not occur in IE. So, how can I ensure that it works in IE11 so that the child's ...

How to Deactivate Navigation Tabs in AnythingSlider?

I am encountering some issues with the css and js while using the AnythingSlider tool. Specifically, I want to modify the navigation tabs in a way that certain tabs will either remain unchanged or become inactive based on a ColdFusion conditional. For ins ...

Can you provide guidance on centering a "frame" and aligning elements within it using CSS?

In creating my webpage, I am looking to design a virtual frame that is centered on the page. Within this frame, I want to be able to align various elements like images, text, and more using CSS properties. Let me explain with an example: I envision being ...

What is the best method to create a gap between the header and table body in Angular Material version 13?

Is there a way to create space (margin) between the header and body in an Angular Material table? I initially attempted to use a solution found here, but it was unsuccessful. Check out the demo here: https://stackblitz.com/edit/angular-ivy-anphrw?file=sr ...

a pair of liquid divs aligned next to each other

I am attempting to line up two fluid divs next to each other. You can see a preview of the current layout here. My goal is to have two flexible divs side by side. This is the code I currently have: .images { border: 1px solid red; position:absolute; ...

Having difficulty resizing the image to fit the container correctly

I am struggling to adjust the size of an image within a container. In my setup, there are two columns - the left column is fixed at 280px and the right column expands to fill the remaining space. The image is placed in the right column, but its dimensions ...