How to get an IFrame to be responsive in iOS Safari?
To make an IFrame responsive in iOS Safari, use width: 100%;
and encapsulate within a wrapper div that manages the aspect ratio. Set the height relative to width using padding-top
— 56.25%
suits a 16:9 ratio.
Here's the basic structure:
This piece of code makes the IFrame adaptable while keeping the aspect ratio intact.
Fine-tuning scrolling, avoiding overflow
Taming the scrolling behavior in IFrames involves overflow: auto;
along with -webkit-overflow-scrolling: touch;
for smoother scrolling in iOS. Wrapping the IFrame in a Div with these styles helps control overflow:
To avoid the beast that is auto-expansion, use the viewport width unit vw
for your IFrame's width:
Get the perfect fit with customization
For peculiar situations like double scrollbar issues on desktop, browser detection is your superhero. When on an iOS device, feel free to hide the outer scrollbar.
Sometimes, position: fixed;
is mandatory for IFrames within dynamic containers:
Correctly setting the height property of an IFrame for a fixed position prevents iOS Safari from taking the liberty to resize it. Use JavaScript to calculate and set the height.
The devil is in the detail: edge cases
Apple's no-scroll default influences IFrames. We trick it by directly adjusting the IFrame content width, resulting in better responsiveness.
JavaScript-oriented adaptations are invaluable. Use it to specify IFrame attributes responsibly:
Lastly, ensure cross-browser compatibility is on your checklist. Utilize caniuse.com
for evaluating viewport units support and CSS property adherence.
Was this article helpful?