为什么我的 CSS3 媒体查询无法在移动设备上运行?

发布于 2024-12-11 21:13:25 字数 350 浏览 0 评论 0 原文

在 styles.css 中,我使用媒体查询,两者都使用以下变体:

/*--[ Normal CSS styles ]----------------------------------*/

@media only screen and (max-width: 767px) {

    /*--[ Mobile styles go here]---------------------------*/
}

当我缩小窗口时,网站将大小调整为我在常规浏览器(Safari、Firefox)中想要的布局,但是,移动布局不是根本没有在手机上显示。相反,我只看到默认的 CSS。

有人能指出我正确的方向吗?

In the styles.css, I am using media queries, both of which use a variation of:

/*--[ Normal CSS styles ]----------------------------------*/

@media only screen and (max-width: 767px) {

    /*--[ Mobile styles go here]---------------------------*/
}

The sites resize to the layout I want in a regular browser (Safari, Firefox) when I shrink the window, however, the mobile layout isn't shown at all on a phone. Instead, I just see the default CSS.

Can anyone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(22

静谧 2024-12-18 21:13:25

所有这三个都是有用的提示,但看起来我需要添加一个元标记:

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

现在它似乎可以在 Android (2.2) 和 iPhone 中工作...

All three of these were helpful tips, but it looks like I needed to add a meta tag:

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

Now it seems to work in both Android (2.2) and iPhone all right...

勿挽旧人 2024-12-18 21:13:25

不要忘记在媒体查询上方添加标准 CSS 声明,否则查询也将无法工作。

.edcar_letter{
    font-size:180px;
}

@media screen and (max-width: 350px) {
    .edcar_letter{
        font-size:120px;
    }
}

Don't forget to have the standard css declarations above the media query or the query won't work either.

.edcar_letter{
    font-size:180px;
}

@media screen and (max-width: 350px) {
    .edcar_letter{
        font-size:120px;
    }
}
谢绝鈎搭 2024-12-18 21:13:25

我怀疑关键字 only 可能是这里的问题。我使用这样的媒体查询没有任何问题:

@media screen and (max-width: 480px) { }

I suspect the keyword only may be the issue here. I have no issues using media queries like this:

@media screen and (max-width: 480px) { }

愁杀 2024-12-18 21:13:25

我在新闻网站中使用了 bootstrap,但它在 IE8 上不起作用,我使用了 css3-mediaqueries.js javascript 但仍然不起作用。如果您希望媒体查询使用此 javascript 文件,请在 css 中将屏幕添加到媒体查询行,

这里是一个示例:

<meta name="viewport" content="width=device-width" />


<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">
<script type="text/javascript" src="css3-mediaqueries.js"></script>

css 链接行与上面的行一样简单。

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" />


<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">
<script type="text/javascript" src="css3-mediaqueries.js"></script>

css Link line as simple as above line.

彼岸花似海 2024-12-18 21:13:25

今天我也遇到了类似的情况。媒体查询不起作用。过了一会儿,我发现“and”后面的空格不见了。
正确的媒体查询应该如下所示:

@media screen and (max-width: 1024px) {}

Today I had similar situation. Media query did not work. After a while I found that space after 'and' was missing.
Proper media query should look like this:

@media screen and (max-width: 1024px) {}
萤火眠眠 2024-12-18 21:13:25

css代码的顺序也很重要,例如:

@media(max-width:600px){
  .example-text{
     color:red;
   }
}
.example-text{
   color:blue;
}

上面的代码由于执行顺序而无法工作。需要写成如下:

.example-text{
   color:blue;
}
@media(max-width:600px){
  .example-text{
     color:red;
   }
}

The sequential order of css code also matters, for example:

@media(max-width:600px){
  .example-text{
     color:red;
   }
}
.example-text{
   color:blue;
}

the above code will not work because of the execution order. Need to write as following:

.example-text{
   color:blue;
}
@media(max-width:600px){
  .example-text{
     color:red;
   }
}
揽月 2024-12-18 21:13:25

始终以 px 或 rem 等单位提及最大宽度和最小宽度。这对我来说已经解决了。如果我写的内容没有单位而只有数字值,浏览器将无法读取媒体查询。例子:
这是错误的
@media only 屏幕和(最大宽度:950)

这是正确的
@media only 屏幕和(最大宽度:950px)

Always mention max-width and min-width in some unit like px or rem. This figured it out for me. If I write it without the unit and only the number value, browser can't read the media queries. example:
this is wrong
@media only screen and (max-width:950)
and
this is right
@media only screen and (max-width:950px)

沩ん囻菔务 2024-12-18 21:13:25

在 html 的 head 部分添加以下标签

Add Below tag in html's head section

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

随波逐流 2024-12-18 21:13:25

OP 的代码片段显然使用了正确的注释标记,但 CSS 可能会以渐进的方式中断 - 因此,如果存在语法错误,则之后的所有内容都可能会失败。有几次,我依赖于可靠的来源,这些来源提供了不正确的注释标记,从而破坏了我的样式表。由于OP只提供了一小部分代码,我建议如下:

确保所有CSS注释都使用此标记/* ... */< /code> -- 根据 MDN,这是正确的 css 注释标记

验证你的 CSS 带有 linter 或安全的在线验证器。 这是 W3 的一个

更多信息:
我去检查 Bootstrap 4 中最新推荐的媒体查询断点,最后直接从他们的文档中复制了样板。几乎每个代码块都标有 javascript 风格的注释 //,这破坏了我的代码,并且只给了我一些神秘的编译错误来进行故障排除,这在当时超出了我的想象,让我感到悲伤。

IntelliJ 文本编辑器允许我使用 ctrl+/ 热键注释掉 LESS 文件中的特定 css 行,这很棒,除了它默认在无法识别的文件类型上插入//。它不是免费软件,而且 less 是相当主流的,所以我信任它并使用它。这破坏了我的代码。有一个首选项菜单可以教它为每种文件类型正确的注释标记。

The OP's code snippet clearly uses the correct comment markup but CSS can break in a progressive way — so, if there's a syntax error, everything after that is likely to fail. A couple times I've relied on trustworthy sources that supplied incorrect comment markup that broke my style sheet. Since the OP provided just a small section of their code, I'd suggest the following:

Make sure all of your CSS comments use this markup /* ... */ -- which is the correct comment markup for css according to MDN

Validate your css with a linter or a secure online validator. Here's one by W3

More info:
I went to check the latest recommended media query breakpoints from bootstrap 4 and ended up copying the boiler plate straight from their docs. Almost every code block was labeled with javascript-style comments //, which broke my code — and gave me only cryptic compile errors with which to troubleshoot, which went over my head at the time and caused me sadness.

IntelliJ text editor allowed me to comment out specific lines of css in a LESS file using the ctrl+/ hotkey which was great except it inserts // by default on unrecognized file types. It isn't freeware and less is fairly mainstream so I trusted it and went with it. That broke my code. There's a preference menu for teaching it the correct comment markup for each filetype.

水溶 2024-12-18 21:13:25

我最近也遇到了这个问题,后来发现是因为我在 and( 之间没有加空格。
这是错误

@media screen and(max-width:768px){
}

然后我将其更改为此以纠正它

@media screen and (max-width:768px){
}

I encountered this issue recently too, and I later found out it was because I didn't put a space between and and (.
This was the error

@media screen and(max-width:768px){
}

Then I changed it to this to correct it

@media screen and (max-width:768px){
}
入怼 2024-12-18 21:13:25

如果浏览器缩放级别不正确,也可能会发生这种情况。您的浏览器窗口缩放应为 100%。在 Chrome 中,使用 Ctrl + 0 重置缩放级别。

It may also happen if the browser zoom level is not correct. Your browser window zoom should be 100%. In Chrome use Ctrl + 0 to reset the zoom level.

自此以后,行同陌路 2024-12-18 21:13:25

将另一个答案扔进戒指。如果您尝试使用 CSS 变量,那么它会悄然失败。

@media screen and (max-device-width: var(--breakpoint-small)) {}

CSS 变量在媒体查询中不起作用(按设计)。

Throwing another answer into the ring. If you're trying to use CSS variables, then it will quietly fail.

@media screen and (max-device-width: var(--breakpoint-small)) {}

CSS variables don't work in media queries (by design).

暮年慕年 2024-12-18 21:13:25

我以前从未见过的奇怪原因:如果您在媒体查询之外使用“父>子”选择器(在 Firefox 69 中),它可能会破坏媒体查询。我不确定为什么会发生这种情况,但对于我的场景来说,这不起作用...

@media whatever {
    #child { display: none; }
}

但是添加父级以匹配页面上的其他一些 CSS,这有效...

#parent > #child { display: none; }

似乎指定父级应该不重要,因为id 非常具体,不应该有歧义。也许这是 Firefox 的一个错误?

Weird reason I've never seen before: If you're using a "parent > child" selector outside of the media query (in Firefox 69) it could break the media query. I'm not sure why this happens, but for my scenario this did not work...

@media whatever {
    #child { display: none; }
}

But adding the parent to match some other CSS further up the page, this works...

#parent > #child { display: none; }

Seems like specifying the parent should not matter, since an id is very specific and there should be no ambiguity. Maybe it's a bug in Firefox?

只涨不跌 2024-12-18 21:13:25

重要的是@media屏幕必须位于css的末尾

It is important that the @media screen must be at the end of the css

愁杀 2024-12-18 21:13:25

我根据情况使用几种方法。
在同一个样式表中,我使用:@media(最大宽度:450px),或者单独确保标题中的链接正确。我查看了你的 fixmeup,你发现了一系列令人困惑的 css 链接。它在 HTC Desire S 上也按您所说的方式运行。

I use a few methods depending.
In the same stylesheet i use: @media (max-width: 450px), or for separate make sure you have the link in the header correctly. I had a look at your fixmeup and you have a confusing array of links to css. It acts as you say also on HTC desire S.

生死何惧 2024-12-18 21:13:25
@media all and (max-width:320px)and(min-width:0px) {
  #container {
    width: 100%;
  }
  sty {
    height: 50%;
    width: 100%;
    text-align: center;
    margin: 0;
  }
}

.username {
  margin-bottom: 20px;
  margin-top: 10px;
}
@media all and (max-width:320px)and(min-width:0px) {
  #container {
    width: 100%;
  }
  sty {
    height: 50%;
    width: 100%;
    text-align: center;
    margin: 0;
  }
}

.username {
  margin-bottom: 20px;
  margin-top: 10px;
}
猥琐帝 2024-12-18 21:13:25

由于仅不是拼写错误,对我不起作用
@media screen and(max-width: 930px) 需要在 (and) & 之间留有空间左括号@media screen 和(最大宽度:930px)

due to only not typo mistake not work for me
@media screen and(max-width: 930px) require sopace between the (and) & opening bracket @media screen and (max-width: 930px)

秋叶绚丽 2024-12-18 21:13:25

您唯一需要的解决办法是:

只需在 .CSS 文件末尾获取所有媒体查询

它有效,尝试一下

The Only Fix You All Need Is :

Just Take All The Media Queries At The End Of A .CSS File

It Works, Try It

以歌曲疗慰 2024-12-18 21:13:25

对我来说,我指定了 max-height 而不是 max-width

如果是你,那就去改变吧!

    @media screen and (max-width: 350px) {    // Not max-height
        .letter{
            font-size:20px;
        }
    }

For me I had indicated max-height instead of max-width.

If that is you, go change it !

    @media screen and (max-width: 350px) {    // Not max-height
        .letter{
            font-size:20px;
        }
    }
∝单色的世界 2024-12-18 21:13:25

对于遇到同样问题的每个人,请确保您确实写了“120px”,而不是仅写了“120”。这是我的错误,它让我发疯。

For everyone having the same issue, make sure you actually wrote "120px" instead of only "120". This was my mistake and it drove me crazy.

苍景流年 2024-12-18 21:13:25

好吧,就我而言,宽度值后面的 px 丢失了......有趣的是, W3C验证器甚至没有注意到这个错误,只是默默地忽略了这个定义。

Well, in my case, the px after the width value was missing ... Interestingly, the W3C validator did not even notice this error, just silently ignored the definition.

菊凝晚露 2024-12-18 21:13:25

我遇到了同样的问题,结果发现我的媒体查询顺序错误。它们应该在 CSS 中从最宽到最小来定义

I was having this same problem and it turns out my media queries were in the wrong order. They should be defined from widest to smallest in the CSS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文