2014-08-30 34 views
0

我看到一些寫響應這個樣子,方式查詢

@media screen and (min-width:991px) and (max-width:1200px){ 
    /* styles */ 
} 

@media screen and (min-width:767px) and (max-width:990px){ 
    /* styles */ 
} 

@media screen and (min-width:480px) and (max-width:766px){ 
    /* styles */ 
} 

我寫像這樣

@media screen and (max-width:991px){ 
    /* styles */ 
} 

@media screen and (max-width:767px){ 
    /* styles */ 
} 

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

像每當設計打破我寫的最多的是大小相同的事情寬度:規則,我得到一個完全響應式設計。但是,這是正確的方式做的方法

+0

在媒體查詢之外做所有對全部視口大小都適用的東西真的好多了,然後使用最小寬度並添加浮點數等,然後將最小寬度從最小到最大。並使用流體CSS。這將媒體查詢保持在最低限度。 – Christina 2014-08-30 06:12:55

回答

0

500px爲例,在第一種方式中,只有第二個屬性(border)將適用於:

/* 500 is not between 767 and 990, so this rule will ignore */ 
@media screen and (min-width:767px) and (max-width:990px){ 
    .elem { 
     background: red; 
    } 
} 

/* 500 is between 480 and 766, this rull will apply */ 
@media screen and (min-width:480px) and (max-width:766px){ 
    .elem { 
     border: 10px solid green; 
    } 
} 

jsFiddle Demo

但是在第二種方式中,這兩個規則將適用:

/* 500 is smaller than 991, this rull will apply */ 
@media screen and (max-width:991px){ 
    .elem { 
     background: red; 
    } 

} 

/* 500 is smaller than 767, this rull will apply */ 
@media screen and (max-width:767px){ 
    .elem { 
     border: 10px solid green; 
    } 
} 

jsFiddle Demo

+0

沒關係,所以這個方法也可以用,對。如果我想保留一個樣式,不要寫這個樣式,如果我想覆蓋一個樣式,寫一個新的媒體屏幕。並且這種類型的編碼會影響加載時間在暴徒設備..? – 2014-08-30 06:18:44

+0

兩者都是正確的,可以使用。這取決於你的情況。 – dashtinejad 2014-08-30 06:20:48