简体   繁体   English

CSS:@media 查询的最大宽度不起作用

[英]CSS: max-width for @media query not working

(It works on other browsers but not chrome) (它适用于其他浏览器,但不适用于 chrome)

I want to apply a style only when the browser size is less than 1400px我只想在浏览器大小小于 1400px 时应用样式

with max-width not working max-width不起作用

@media only screen and (max-width:1400px)  {
.heading-left {
    left: -0.5%;
  }
}

with min-width its workingmin-width它的工作

@media only screen and (min-width:480px)  {
.heading-left {
    left: -0.5%;
   }
}

But also alters when browser width is above 1400px (I know thats how it works but max-width is not working)但是当浏览器宽度超过 1400px 时也会改变(我知道它是这样工作的,但 max-width 不起作用)

Fiddle for this小提琴为此

https://jsfiddle.net/j4Laddtk/ https://jsfiddle.net/j4Laddtk/

Have you tried adding the viewport in?您是否尝试过添加视口?

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

Working JSFiddle工作 JSFiddle

Viewport is used when rendering responsive pages and is therefore mostly used when dealing with mobile websites, but when dealing with media queries it helps tell the CSS what the actual device-width is. Viewport 在呈现响应式页面时使用,因此主要用于处理移动网站,但在处理媒体查询时,它有助于告诉 CSS 实际设备宽度是什么。

Is your browser zoom -ed at different than 100% level ?您的浏览器zoom 100%是否与100%不同? If so, zoom to 100% ( Ctrl+MouseWheel )如果是这样,放大到100% ( Ctrl+MouseWheel )

Try this method.试试这个方法。

This will target based on device这将基于设备定位

@media screen 
 and (max-device-width: 1400px) 
 and (min-device-width: 480px) 
{ 
   .heading-left {
    left: -0.5%;
   }

}

To target based on browser window area基于浏览器窗口区域定位

@media screen 
 and (max-width: 1400px) 
 and (min-width: 480px) 
{ 
   .heading-left {
    left: -0.5%;
   }

}

您需要在声明标准后放置 @media 查询

This worked for me这对我有用

@media screen and (max-width: 700px) and (min-width: 400px) { 
    .heading-left { left: -0.5%; }
}

If you've tried everything and you're still stuck, remember that media queries need to be at the bottom because CSS is applied from top-down.如果您尝试了所有方法但仍然卡住,请记住媒体查询需要位于底部,因为 CSS 是自上而下应用的。

If you have如果你有

.container {
  color: white;
}

and you want the font to be pink for screens less than 600px wide, your other media query needs to be below the original .container style.并且您希望屏幕宽度小于 600 像素时字体为粉红色,您的其他媒体查询需要低于原始 .container 样式。

.container {
  color: white;
}

@media only screen and (max-width: 600px) {
  .container {
    color: pink;
  }
}

So if your media queries are at the top the default colour of white will override the media query for pink.因此,如果您的媒体查询位于顶部,则白色的默认颜色将覆盖粉红色的媒体查询。

Another thing that can happen is that you do something really stupid like:可能发生的另一件事是你做了一些非常愚蠢的事情,比如:

@media only screen and (max-width: 1400)  { ... }

Make sure you put the px to identify what the quantity of your max-width is.确保放置 px 来确定最大宽度的数量。

@media only screen and (max-width: 1400px)  { ... }

Not that I've ever been stuck for an hour on something so simple..并不是说我在这么简单的事情上被困了一个小时..

@media only screen and (max-width: 1000px) {
  /*Don't forget to add meta viewport in your html*/
}

If it's not working try to inspect elements in the browser by navigating to the network in developer tools and toggling disable cache.如果它不起作用,请尝试通过在开发人员工具中导航到网络并切换禁用缓存来检查浏览器中的元素。

Sometimes it's not working because of the browser cache.有时由于浏览器缓存而无法正常工作。

the developer tool was saying that the size of the viewport is 1400px but the actual width the css was considering was more than 1400px. 开发人员工具说视口的大小是1400px,但css考虑的实际宽度超过1400px。 Because of this the media query isn't working 因此,媒体查询无法正常工作

This problem caused me several hours to figure it out with Bootstrap 3 when it just doesn't work.这个问题导致我在 Bootstrap 3 无法正常工作时花了几个小时来解决它。 The reason is in the header of each web page, it needs this meta view element.原因是在每个网页的标题中,它需要这个元视图元素。

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

More details https://www.w3schools.com/css/css_rwd_viewport.asp更多详情https://www.w3schools.com/css/css_rwd_viewport.asp

There is one thing I want to add here.我想在这里补充一件事。 (Only applicable if having different CSS files) --> If some values are not applying then check if your CSS file having the media queries is at the bottom inside the element or not. (仅适用于具有不同 CSS 文件的情况)--> 如果某些值不适用,则检查具有媒体查询的 CSS 文件是否位于元素内部的底部。 Always put the media queries CSS file (if made in a different file) at the bottom of all other CSS links.始终将媒体查询 CSS 文件(如果在其他文件中创建)放在所有其他 CSS 链接的底部。

For those of you, dear programmers, that none of the above helped let me suggest taking a look back at your html and the naming of your .classes.对于你们中的那些,亲爱的程序员,以上都没有帮助,让我建议您回顾一下您的 html 和 .classes 的命名。 Improving the classes name worked out for me.改进类名对我有用。

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

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