I have in a webpage which is responsive. It makes use of media queries.
But the same page loaded in an iframe is not working.
For example, consider to my webpage, I have given background color as red when the screen-width is less than 640px.
@media only screen and (max-device-width : 640px) { .header-text { background-color: red; } }
So when I load that window in a new tab and make the browser width less than 640px, background color becomes red.
But the same window when loaded in an iframe of 320*480 dimensions does not have background color red.
How to make media queries work in an iframe?
Media queries located inside the IFRAME will evaluate media attributes of the parent frame, not the IFRAME (so eg width will refer to the width of the parent frame's viewport, not the viewport of the IFRAME) Styles defined in the parent frame also apply to the iframe.
This may be the reason why your media queries aren't working. That is to say, you may have declared CSS within your HTML document. So if this is the case, simply go through the HTML document and remove the CSS declared inline. Alternatively, you can override the inline CSS with !
Iframes are most often used to embed specific content from one web page — like a video, form, document, or even a full web page — within a different web page. This is a powerful capability in HTML — you can take any content from any website (with permission) and place it on your own site to enhance your content.
Google recommends refraining from creating iframes. At there Webmasters Help Forum, Google clearly stated that iframes may cause problems for them: IFrames are sometimes used to display content on web pages. Content displayed via iFrames may not be indexed and available to appear in Google's search results.
Try it this way, instead of device
just target the width
Change
@media only screen and (max-device-width : 640px) {
to
@media only screen and (max-width : 640px) {
Also, it is more advised to target both min and max view-port width if you want to avoid considering order of media query declarations in your style-sheet
@media screen and (min-width: 320px) and (max-width: 640px) { .header-text { background-color: blue; } }
Include the meta bellow into the HTML which loads your iframe and it should work, at least on android devices.
<meta name="viewport" content="width=device-width, initial-scale=1">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With