Responsive media and safe areas
Definition and purpose
Responsive images should fit their containing box without unintended distortion. Safe-area insets can reserve room near a display cutout or other unsafe viewport edge.
Shrink an image
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a, #b { background: lightblue; } #p { width: 120px; } img { max-inline-size: 100%; block-size: auto; } </style> </head> <body> <div id="p"> <img id="a" alt="Blue rectangle" width="200" height="100" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='100'%3E%3Crect width='200' height='100' fill='blue'/%3E%3C/svg%3E"> </div> </body></html>The 2:1 image becomes 120 by 60 pixels.
Syntax and mental model
max-inline-size: 100% and block-size: auto allow an image to shrink while keeping its intrinsic aspect ratio in horizontal writing. HTML width and height describe the intrinsic ratio before loading. For edge-to-edge designs use env(safe-area-inset-top, 0px) with a base spacing, for example max(12px, env(safe-area-inset-top, 0px)). Mobile edge-to-edge behavior may require viewport-fit=cover in the viewport meta tag.
Keep ordinary edge padding
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a, #b { background: lightblue; } #p { padding-top: 12px; padding-top: max(12px, env(safe-area-inset-top, 0px)); } </style> </head> <body> <div id="p"> <div id="a">A</div> <div id="b">B</div> </div> </body></html>In a rectangular desktop viewport the tested top inset is zero; the minimum still applies.
Common mistakes
Do not equate a CSS fallback argument with a minimum inset. An available environment variable can be zero, so use max() to preserve ordinary spacing.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Input capabilities and preferences. Next lesson: Container size queries.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Responsive media and safe areas — knowledge check
Five questions on responsive media and safe areas.
What preserves the image ratio while shrinking?
What does max-inline-size: 100% constrain?
What is an env fallback used for?
How can ordinary padding be preserved?
Can desktop emulation prove physical cutout behavior?