Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Media queries not working inside an iframe

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?

like image 487
Aniket Avatar asked Dec 01 '14 11:12

Aniket


People also ask

Do media queries work in iframes?

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.

How do you fix media query not working?

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 !

Can you put anything in an iframe?

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.

Are iframes good practice?

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.


2 Answers

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;     } } 
like image 141
NoobEditor Avatar answered Oct 08 '22 18:10

NoobEditor


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">
like image 25
Kiko Avatar answered Oct 08 '22 19:10

Kiko