Home  >  Article  >  Web Front-end  >  Hide CSS for browsers: Dugu Nine Swords_Experience exchange

Hide CSS for browsers: Dugu Nine Swords_Experience exchange

WBOY
WBOYOriginal
2016-05-16 12:08:322081browse

针对浏览器隐藏CSS之独孤九剑

Hide CSS for Browsers

  我们都知道浏览器之间是有差异的,很多人在使用XHTML+CSS方式制作网页的时候都曾为此无比头痛。要在这些差异的影响下工作需要很多的技巧,“隐藏CSS”就是其中很重要的一种技巧(甚至是最重要的)。这种技巧的主要思想是,用某些方式对某些浏览器隐藏某些重载的CSS规则。

  这篇文章并不是想教你如何使用这种技巧,它面向的是有一定CSS基础的设计开发者,我假定你曾经使用过某种隐藏CSS规则的方法。这里是想把可用的方法与被隐藏规则的浏览器都列出来,方便大家在平时工作时随时查阅。

  在这张大表里列出了九种方法,我就称其为“独孤九剑”,希望能帮上你的忙:-D

  注:此表格原载 w3development.de

Browser @import url("...") @import url(...) @import "..." media="" @media comment attribute child Tantek
Amaya 5.1 Win x           x x x
IE 3 Win x x x     x x x x
IE 4 Win x x x   x x x x x
IE 5 Win           x x x x
IE 5.5 Win             x x x
IE 6 Win             x x  
IE 4.01 Mac x       x x x x x
IE 4.5 Mac x         x x   x
IE 5 Mac         x x x    
Konqueror 2.1.1         x        
Mozilla 1.0                  
Netscape 4.x x x x x x   x x x
Netscape 6.01                  
Netscape 6.1                  
Netscape 6.2                  
Opera 3.60 Win             x    
Opera 4.02 Win                  
Opera 5.02 Win                  
Opera 5.12 Win                  
Opera Tech Preview 3 Mac                  

My first reaction after reading this list is: Mozilla and Opera are really gods. Only one rule will take effect on the oldest version of Opera, so when we talk about hidden rules later, we basically ignore Mozilla and Opera. The nine "swordsmanship" moves are listed below, with my personally recommended usage occasions also noted next to them. Note that what I recommend may not necessarily match your needs. Please check the table above before using it.

@import url("")
@import url("global.css")

Hide rules for browsers with older versions (version number less than 5).

@import url()
@import url(global.css)

Hidden rules for browsers with older versions of the Windows platform (version number less than 5).

@import ""
@import "global.css"

Hidden rules for browsers with older versions of the Windows platform (version number less than 5).

media=""
<link href="global.css" type="text/css" rel="stylesheet" media="all">

Hide rules for Netscape 4.x.

@media
@media all {
... /* Rules that need to be hidden */
}

If you only consider NN and IE, you will only hide the rules for version 4.x.

comment
#anySelector/* */ { color:#f00; }

This is a precision-guided missile. Only hide rules for IE5 and below. So if you want to define rules separately for IE5 and IE5.5, then rely on it.

attribute
p[id] { color: #0f0; }

Another precision-guided missile. If you don't care about older browsers, you can hide the rules only from IE. For more information about attribute selectors see W3C documentation.

child
p>span { color: #00f; }

Basically the same as above, except that some versions of IE on the Mac platform support this rule.

Tantek
p#tantek {
voice-family: "\"}\""; /* Some browsers have parsing bugs */
voice-family: inherit; /* Rules below will be ignored */
color: #f00;
}

The famous hidden CSS trick hides rules from all "non-modern" browsers.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn