CSS Media Query Won't Work [duplicate]

a 夏天 提交于 2020-03-17 08:35:46

问题


I keep trying to do a media query in my CSS doc doing something like:

@media screen and (max-device-width: 480px) { /*css here*/}

but it won't work when I test it on an iPhone. I've tried changing the sizes and using the portrait/landscape feature, but still nothing. What am I doing wrong?


回答1:


I always call mine when I link the CSS in the head of the HTML.

An example from a current page I'm working on:

<link rel="stylesheet" type="text/css" media="screen and (min-device-width: 320px) and (max-device-width: 500px)" href="css/mobile.css" />
<link rel="stylesheet" type="text/css" media="screen and (min-device-width: 501px)" href="css/main.css" />

This selects the CSS doc that will be loaded right from the start instead of selecting styles after the CSS document is loaded (reduces number of HTTP requests)

When doing this within the CSS document itself, you should generally replace max-device-width with max-width.




回答2:


Check that you have

<meta name="viewport" content="width=device-width, initial-scale=1.0">

in the head of your doc

Good luck




回答3:


use "max-width" instead of "max-device-width" which is fixed per device.




回答4:


this is a samples media query css

/************************************************************************************
smaller than 980
*************************************************************************************/
@media screen and (max-width: 980px) {
your css here
}

/************************************************************************************
smaller than 650
*************************************************************************************/
@media screen and (max-width: 650px) {
your css here
}
/************************************************************************************
smaller than 560
*************************************************************************************/
@media screen and (max-width: 480px) {
your css here
}

Hard to understand as you have not provided the code..but the common mistake people do it by not adding this meta data

<meta name="viewport" content="width=device-width, initial-scale=1">



回答5:


I'm not sure but I think the max-device-width of an iphone is 640px. Give it a try.




回答6:


@media only screen and (min-width : 480px) {

}

It seems to work fine in both Android and iPhone.



来源:https://stackoverflow.com/questions/18207076/css-media-query-wont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!