简体   繁体   English

在台式机和电话的CSS中使用@media查询

[英]using @media queries in CSS for desktop and phone

I am developing a website which will be responsive in that different CSS styles are applied depending on the width of the users' browser. 我正在开发一个网站,该网站将根据用户浏览器的宽度而采用不同的CSS样式,从而做出响应。

the method chosen was to use CSS media queries. 选择的方法是使用CSS媒体查询。

my problem is this: when I use the following code 我的问题是:当我使用以下代码时

@media screen
and (min-width: 200px)
and (max-width: 800px)
{
    #example
    {
        background: purple;
    }
}

this works when I resize the window on my PC, but is not recognised by my phone whose resolution is within the limits. 当我在PC上调整窗口大小时,此方法有效,但分辨率在限制范围内的手机无法识别。

perhaps more perculiarly, when I use the following code 当我使用以下代码时,也许会更加细致

@media screen
and (min-device-width: 200px)
and (max-device-width: 800px)
{
    #example
    {
        background: purple;
    }
}

this has the inverse effect: displays on phone, but not on PC. 这具有相反的效果:显示在手机上,而不显示在PC上。

as far as I have read there is no scope for an "OR" operator for something along the lines of the following to be valid 据我所读,没有“ OR”运算符的范围,按照以下内容有效

@media screen
and (
        ((min-width: 200px) and (max-width: 800px))

        | / || / OR

        ((min-device-width: 200px) and (max-device-width: 800px))
    )
{
    #example
    {
        background: purple;
    }
}

so my question is this: is there a way test responsive CSS on both desktop and phone simultaneously? 所以我的问题是:是否可以同时在台式机和手机上测试响应CSS?

I have tried so many combinations: using @media only screen , using Android, Firefox and Chrome browsers on the phone, but to no avail, the result is always the same. 我尝试了很多组合: @media only screen使用@media only screen ,在电话上使用Android,Firefox和Chrome浏览器,但无济于事,结果总是一样。

The only way I can think to do this at the moment is to create two separate stylesheets, one for desktop and one for phone, but this would mean updating two stylesheets every time I wanted to view changes in the browser, which is impractical and counters the idea of responsiveness. 我目前认为做到这一点的唯一方法是创建两个单独的样式表,一个用于桌面,一个用于电话,但这意味着每次我想在浏览器中查看更改时都更新两个样式表,这是不切实际的,并且是不可行的响应的想法。

I looked into using the orientation: landscape/portrait target, but as far as I can make out this would again involve writing two sets of CSS. 我研究了使用orientation: landscape/portrait目标,但据我所知,这将再次涉及编写两套CSS。

One last consideration is that the website is currently using pure CSS; 最后一个考虑因素是该网站当前正在使用纯CSS。 so no javascript, user-agent determination nor server-side scripting at this point in time. 因此,目前尚无JavaScript,用户代理确定或服务器端脚本编写。

This must be possible so any insights will be appreciated. 这必须是可能的,以便任何见识将受到赞赏。 I'm sure someone will have had the same problem and enlighten me. 我敢肯定有人会遇到同样的问题并启发我。

Try using a viewport meta tag: 尝试使用视口元标记:

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

Place that into the head section of your page. 将其放入页面的头部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM