CSS3 Media Queries are not working

2019-06-22 11:44发布

I've recently been developing a mobile version for a website, and quite simply want to be able to change the CSS file if the browser is detected to be on a mobile device. You can view my previous attempts here, but I have now decided that the most feasible way of getting this done is to focus on Media Queries.

The problem I have here is that the media queries just don't work at all. It always loads the default stylesheet. Here is the code I have:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link href="../css/style.css" id="stylesheet" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" media="only screen and (max-width: 480px), only screen and (max-device-width: 480px)" href="../css/style_mob.css" />

Have I missed something with this code? I've tried using the many tutorials offered for this but none seem to help me. I've really hit a dead end here so any help would be massively appreciated.

4条回答
相关推荐>>
2楼-- · 2019-06-22 12:12

Completing the conditional with min-device-width worked for me on iOS. Note the word device. It did work fine on android even without the word device, however.

查看更多
手持菜刀,她持情操
3楼-- · 2019-06-22 12:14

If you have initial-scale=1.0 and maximum-scale=1.0, then it's a bit redundant to have user-scalable=0 as well. Having maximum-scale set to equal initial-scale means the user can't zoom in at all because they start at the maximum zoom.

查看更多
孤傲高冷的网名
4楼-- · 2019-06-22 12:32

The default stylesheet doesn't have any attributes that would stop it from being loaded.

You could give it media="screen and (min-width: 481px)", but that would lead to the danger, that older browsers won't load it either.

Usually it's best, to have the default stylesheet load and just make sure, that the mobile stylesheet overrides anything you don't need from the default stylesheet.

查看更多
爷的心禁止访问
5楼-- · 2019-06-22 12:32

i used bootstrap in a press site but it does not worked on IE8, i used css3-mediaqueries.js javascript but still not working. if you want your media query to work with this javascript file add screen to your media query line in css

here is an example :

<meta name="viewport" content="width=device-width" />
<script type="text/javascript" src="css3-mediaqueries.js"></script>

<style>
     @media screen and (max-width:900px) {}
     @media screen and (min-width:900px) and (max-width:1200px) {}
     @media screen and (min-width:1200px) {}
</style>

<link rel="stylesheet" type="text/css" href="bootstrap.min.css">

css Link line as simple as above line.

查看更多
登录 后发表回答