Resize image inside modal window

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Mastering Responsive Images: Resizing Media Inside Modal Windows When building modern web applications, especially those involving interactive elements like modals, ensuring that media scales correctly across all devices—from large desktops to small mobile screens—is not just a design choice; it’s a fundamental requirement for good user experience (UX). As a senior developer, I frequently encounter situations where fixed pixel dimensions fail to deliver true responsiveness. You are trying to display branding images within a modal window, and while setting an explicit `height` works on desktop, it causes overflow or awkward scaling on mobile devices. This is a classic CSS challenge that requires shifting from absolute sizing to relative, fluid sizing. This post will detail the most effective, modern CSS techniques to ensure your images resize gracefully inside any container, including modal windows. ## The Pitfall of Fixed Sizing The issue you are facing stems from using fixed pixel values (e.g., `height: 250px;`). This tells the browser to reserve that exact amount of space regardless of the viewport size. On a small mobile screen, if the image itself is wider than the available width, it will either stretch awkwardly or cause horizontal scrolling, breaking the modal's layout. As you noted, relying solely on inline styles for this purpose is insufficient when dealing with complex responsive layouts. We need CSS properties that instruct the browser how to manage space dynamically. ## The Solution: Embracing Fluid CSS Properties The solution lies in leveraging CSS properties designed specifically for fluid scaling. The most effective combination for making an image responsive within its parent container is using `max-width` and ensuring the image scales proportionally. ### 1. Using `max-width: 100%` (The Essential Fix) The single most important property for making images responsive is setting `max-width: 100%`. This rule tells the image that it should never exceed the width of its immediate parent container. If the parent modal body has a certain width, the image will shrink to fit perfectly within that boundary without overflowing. ### 2. Controlling Aspect Ratio with `height` (or Flexbox/Grid) If you need the height to adjust automatically based on the width while maintaining the original aspect ratio, you should let the browser calculate it naturally. If you still need to control the vertical space—perhaps setting a maximum height for aesthetic reasons—you can combine this with the above rule. Here is how we would apply these principles to your modal structure: ```html