其中哪一個是最佳實踐方面的「更好的CSS代碼」?我應該在CSS還是重複規則中重寫以前的值?
A - 改寫:
@media only screen and (min-width: 480px) and (max-width: 960px) {
h1, h2, .header, .widget, .copyright {
position: static;
width: 100%;
height: auto;
/* a lot more styles may be here */
}
.copyright {position: relative;}
}
乙 - 重複:
@media only screen and (min-width: 480px) and (max-width: 960px) {
h1, h2, .header, .widget {
/* CLASS .copyright is missing here to avoid rewriting */
position: static;
width: 100%;
height: auto;
/* a lot more styles may be here */
}
.copyright {position: relative; width: 100%; height: auto; /* a lot more of the SAME styles may be here */}
}
能否改寫產生一定的負面影響(圖表一)?渲染速度較慢,舊版瀏覽器出現問題..?我不知道爲什麼,但對我來說似乎並不合適 - 儘管這樣做更容易。
請注意,示例A中存在一個小的拼寫錯誤,因爲第一個規則與另一個示例更爲具體。除此之外,A顯然是更好的選擇,你需要更少的標記來實現相同的結果。 – Nit
謝謝你的錯字警告。我也拓寬了這個問題。 –