2012-04-21 89 views
7

我有一个使用@media查询的网站,但它似乎没有设置它们。他们总是保持默认风格。CSS媒体查询和!重要

例如:

#div1 { background:red} 

@media screen and (max-height:912px) { 
#div1{background:blue} 
} 

将始终与背景坚持:红色,除非我用!important但我的媒体查询中我的造型这么多的选择。我是否需要设置每个选择器样式!important

+0

您的代码在我的系统上正常工作。我已经在Mac上测试过Firefox,Chrome和Safari。你的测试环境是什么? – Anji 2012-04-21 19:41:02

+0

对不起,延迟回复。我试图找出它为什么不起作用。这是它:http://jsfiddle.net/TUL6h/3/尝试通过调整大小使颜色变蓝。它从来没有发生过 – jQuerybeast 2012-04-21 21:47:11

+0

它只会选择css部分中最后一个的颜色。 – jQuerybeast 2012-04-21 21:48:03

回答

2

它适用于我。

看看那个:http://jsfiddle.net/TUL6h/1/ - 背景是红色的,但是当你改变结果部分的高度时,它会在某个点上变成蓝色。

你试过什么浏览器?

+0

对不起,延迟回复。我试图找出它为什么不起作用。这是它:http://jsfiddle.net/TUL6h/3/尝试通过调整大小使颜色变蓝。它从未发生 – jQuerybeast 2012-04-21 21:47:20

+1

这是正确的,但它是正确的行为,因为'背景:绿色'覆盖'背景:蓝色'。你应该写这样的:http://jsfiddle.net/TUL6h/5/来实现这一点。 – MarcinJuraszek 2012-04-22 08:36:07

+0

这不起作用。这可能是浏览器问题吗? – 2013-12-04 13:34:05

0

不一定,重要用于覆盖默认的CSS即覆盖默认样式中使用属性。所以,如果媒体查询中有新的属性,你就不必一起使用它。

0

!@media查询中包含的重要代码的使用是向页面元素添加动态样式属性的有效方式,同时保留“默认”css以保持简单的维护。

例子:

<html> 
    <head> 
    <style> 
    @media all and (max-width: 768px) { 

    /* CSS styles targeting screens of smaller sizes and/or resolutions (IE phones and tablets) */ 

     #semantically-named-element p 
     { width: 60% !important; 
     background: rgba(0,0,255,1)!important; 
     margin: auto !important; } 

     #semantically-named-element span 
     { display: none !important; } 

    } 
    /* Look ma no media queries needed for default CSS below! Very easy to discern default styles, when the default code is the stuff not wrapped in queries. Period. */  

    /* Default CSS style attributes for any viewports that do not fit into the low or high resolution/screen size parameters set above and below. Also the fallback for browsers that still disregard CSS media queries */ 
    #semantically-named-element p 
     { width: 90%; 
      background: rgba(255,0,0,1); 
      margin: auto; } 

    #semantically-named-element span 
     { display: block; 
      background: rgba(0,0,0,0.8); 
      color: #FFF; 
      text-align: center;} 
    @media all and (min-width: 968px) { 
    /* CSS styles targeting very large or very high resolution viewports at the 769 pixels wide "break-point" */ 
     #semantically-named-element p 
     { width: 80% !important; 
     background: rgba(0,255,0,1)!important; 
     margin: auto !important; } 

     #semantically-named-element span 
     { display: block !important; 
     background: rgba(0,0,0,0.8)!important; 
     color: #FFF !important; font-size: 200%; } 
    } 
    </style> 
    </head> 

    <body> 
     <div id="semantically-named-element"> 
     <p> Usage of !important inside of the code contained within your @media queries is an effective way to add dynamic style properties to your pages elements, while leaving your 'default' css intact for easy maintenance.<span>hidden until tablet greater 768 viewport pixel width</span></p> 
     </div> 
    </body> 
</html> 
0

综观//jsfiddle.net/TUL6h/55/你的例子: 您需要更改媒体查询的顺序。 max-height:510px; 包括 max-height:500px; 为了显示500px的更特殊情况,它必须先排在第一位。

+0

jsfiddle.net/TUL6h/55不会是我的。矿被创建到5个。其余的来自这里的用户。 – jQuerybeast 2013-10-31 18:09:26

3

我有同样的问题。我意识到媒体查询必须放在样式表的最后(即使您在工作流中使用/导入多个文件)。