简体   繁体   English

CSS 在 chrome 中测试时媒体查询不起作用

[英]CSS Media query not working when testing in chrome

My CSS media query is not working.我的 CSS 媒体查询不起作用。

.container-fluid {
  background-color: red;
  border: 2px solid blue;
}

@media (min-height: 500px) {
  .container-fluid {
    background-color: green;
    border: 2px solid black;
  }
}

I have also included我也包括了

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

in the head section在头部

Could you find out anything I did wrong here你能找出我在这里做错了什么吗

Really appreciate if anyone could help如果有人可以提供帮助,我真的很感激

Your style is supposed to be working.你的风格应该是有效的。 You might be confusing min-height and max-height您可能会混淆min-heightmax-height

If you want the style to change when the view has been resized down to 400px or less use max-height: 400px and if you want to change the style when the view has been resized above 400px then use min-height: 400px如果您希望在视图缩小到 400 像素或更少时更改样式,请使用max-height: 400px ,如果您想在视图大小调整到 400 像素以上时更改样式,请使用min-height: 400px

However然而

If your using a css library that is making use of .container-fluid your selector is actually working but it cannot overide the existing style, to fix this you can either do one of the ( or all of it ) following:如果您使用使用.container-fluid的 css 库,您的选择器实际上正在工作,但它不能覆盖现有样式,要解决此问题,您可以执行以下操作之一(或全部):

  1. The un-neat way, adding !important in your properties. un-neat方式,在您的属性中添加!important
  2. Putting your css after the css library has been loaded so your css has higher priority.将 css 放在 css 库已加载after ,因此您的 css 具有更高的优先级。 So <link href="LIBRARY FIRST"/> then <link href="your css"/>所以<link href="LIBRARY FIRST"/> then <link href="your css"/>
  3. Using the exact same selector the library used instead of using just .container-fluid { } , sometimes it's written as .something.something-2.container-fluid { } .使用与库使用的完全相同的选择器,而不是仅使用.container-fluid { } ,有时它被写为.something.something-2.container-fluid { } In CSS the longer the selector is the more priority it gets.在 CSS 中,选择器越长,它获得的优先级越高。

Here is a working example: Try to resize your browser这是一个工作示例:尝试调整浏览器大小

body {
  height: 100vw;
  width: 100vh;

  background-color: red;
  border:2px solid blue;
}

@media (min-height: 400px) {

  body {
    background-color: green;
    border:2px solid black;
  }
}

https://jsfiddle.net/masterpreenz/efxuw7mg/4/ https://jsfiddle.net/masterpreenz/efxuw7mg/4/

hope that helps希望有帮助

you have to use !important .if you override existing bootstrap class你必须使用!important如果你覆盖现有的引导程序 class

.container-fluid{
    background-color: red !important;
    border:2px solid blue !important;
}
@media(min-height:500px){
.container-fluid{
     background-color: green !important;
    border:2px solid black !important;
    }
}

or if you don't want to use !important try this let's assume you want to apply @media to div或者如果你不想使用!important试试这个让我们假设你想将@media应用于div

<div class="container-fluid new-fluid"></div>

you can use the following css您可以使用以下 css

.new-fluid{
    background-color: red;
    border:2px solid blue;
}
@media(min-height:500px){
    .new-fluid{
    background-color: green;
    border:2px solid black;
}
}

let me know if this worked让我知道这是否有效

If you are working on height then your code is working, Please check below code which is working on chrome如果您正在研究高度,那么您的代码正在运行,请检查以下适用于 chrome 的代码

 .container-fluid { background-color: red; border: 2px solid blue; height: 200px; } @media(min-height:500px) {.container-fluid { background-color: green; border: 2px solid black; height: 200px; } }
 <div class="container-fluid">my div</div>

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

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