简体   繁体   English

CSS:应用多个媒体查询

[英]CSS: Applying Multiple Media Queries

I have two .css files - one for desktop, and one for (the majority of) mobile devices. 我有两个.css文件-一个用于桌面,一个用于(大多数)移动设备。 However, the CSS code applies to several @media conditions - and I cannot get them to work across all devices. 但是,CSS代码适用于多个@media条件-我无法让它们在所有设备上都能正常工作。

For example, for iPhone, the @media query looks something like: 例如,对于iPhone,@ @media查询看起来类似于:

@media only screen and (min-device-width : 360px) and (max-device-width : 480px){
    /* iPhone styling */
}

If I wish to apply the same styles to a HTC One, I need to use: 如果我希望将相同的样式应用于HTC One,则需要使用:

@media only screen and (-webkit-min-device-pixel-ratio: 3) and (max-device-width:1920px){
    /* HTC One styling */
}

If I have two identical .css files - one with the first condition and one with the second - the website displays exactly how I want on both devices. 如果我有两个相同的.css文件-一个具有第一个条件,一个具有第二个条件-网站将在两个设备上准确显示我想要的方式。 However, when making a change this means that I need to edit multiple files. 但是,进行更改时,这意味着我需要编辑多个文件。

I have tried combining the condition like so: 我试过像这样组合条件:

@media only screen and (min-device-width : 360px) and (max-device-width : 480px),
@media only screen and (-webkit-min-device-pixel-ratio: 3) and (max-device-width:1920px) {
    /* Generic mobile device styling */
}

but when testing on both the HTC and the iPhone, only the iPhone appears with my desired styling. 但是在HTC和iPhone上进行测试时,只有iPhone出现我想要的样式。

How can I get the same .css styling to apply to multiple @media conditions? 如何才能将相同的.css样式应用于多个@media条件?

According to this documentation, you shouldn't repeat @media after the coma. 根据文档,您不应该在昏迷后重复@media The following code should work: 下面的代码应该工作:

@media only screen and (min-device-width : 360px) and (max-device-width : 480px),
only screen and (-webkit-min-device-pixel-ratio: 3) and (max-device-width:1920px) {
    /* Generic mobile device styling */
}

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

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