CSS Center an iFrame Horizontally and Vertically
- Joel Lipman
- Cascading Stylesheets
- Hits: 13296
So this is a quick article to demo how to center an iframe horizontally and vertically in a screen/viewport in pure CSS (no JavaScript allowed). This CSS centers it by its center point rather than the top/bottom/left/right outline.
Why?
On a mobile, a client's site uses an external page embedded by iframe. When the app within the iframe has an alert message, it popups a div at the centre of its app. The alert message is always at the center of the iframe but if the iframe has a height of 2000 pixels, the iframe gets aligned to the top of the parent...
How?
We're going to use a touch of CSS and instead of determining heights and alignment with JS.