search
HomeWeb Front-endCSS TutorialHide CSS for browsers: Dugu Nine Swords_Experience exchange

针对浏览器隐藏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
This Isn't Supposed to Happen: Troubleshooting the ImpossibleThis Isn't Supposed to Happen: Troubleshooting the ImpossibleMay 15, 2025 am 10:32 AM

What it looks like to troubleshoot one of those impossible issues that turns out to be something totally else you never thought of.

@keyframes vs CSS Transitions: What is the difference?@keyframes vs CSS Transitions: What is the difference?May 14, 2025 am 12:01 AM

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

Using Pages CMS for Static Site Content ManagementUsing Pages CMS for Static Site Content ManagementMay 13, 2025 am 09:24 AM

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

The Ultimate Guide to Linking CSS Files in HTMLThe Ultimate Guide to Linking CSS Files in HTMLMay 13, 2025 am 12:02 AM

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment