常见css hack有三种:1、条件hack,语法“”;2、属性前缀hack,语法“selector{
?属性名:属性值 ?;}”;3、选择器前缀hack,语法“ selector{ 样式代码 }”。

本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。
CSS hack 是通过在 CSS 样式中加入一些特殊的符号,让不同的浏览器识别不同的符号 (什么样的浏览器识别什么样的符号是有标准的,CSS hack 就是让你记住这个标准),,以达到应用不同的 CSS 样式的目的。
CSS hack 是因为现有浏览器对标准的解析不同,为了兼容各浏览器,所采用的一种补救方法.。为了兼容低版本浏览器器的 CSS 解析 BUG,不得不把代码写的很绕, 直接增加了维护成本。
CSS hack 是一种类似作弊的手段, 以欺骗浏览器的方式达到兼容的目的, 是用浏览器的兼容性差异来解决浏览器的兼容性问题。
常用的css hack有三种:条件hack、属性hack、选择符hack
1、条件hack
语法:
<!--[if <keywords>? IE <version>?]> HTML代码块 <![endif]-->
取值:
if条件共包含6种选择方式:是否、大于、大于或等于、小于、小于或等于、非指定版本
- 是否:指定是否IE或IE某个版本。关键字:空
- 大于:选择大于指定版本的IE版本。关键字:gt(greater than)
- 大于或等于:选择大于或等于指定版本的IE版本。关键字:gte(greater than or equal)
- 小于:选择小于指定版本的IE版本。关键字:lt(less than)
- 小于或等于:选择小于或等于指定版本的IE版本。关键字:lte(less than or equal)
非指定版本:选择除指定版本外的所有IE版本。关键字:!
如不想在非IE中看到某区域,可这样写:
<!--[if IE]> <p>你在非IE中将看不到我的身影</p> <![endif]-->
if条件6种选择方式的使用示例
是否,示例代码:
<!--[if IE]>
<style>
.test{color:red;}
</style>
<![endif]-->
大于,示例代码:
<!--[if gt IE 6]>
<style>
.test{color:red;}
</style>
<![endif]-->
大于或等于,示例代码:
<!--[if gte IE 6]>
<style>
.test{color:red;}
</style>
<![endif]-->
小于,示例代码:
<!--[if lt IE 7]>
<style>
.test{color:red;}
</style>
<![endif]-->
小于或等于,示例代码:
<!--[if lte IE 7]>
<style>
.test{color:red;}
</style>
<![endif]-->
非指定版本,示例代码:
<!--[if ! IE 7]>
<style>
.test{color:red;}
</style>
<![endif]-->
2、属性前缀hack
语法:selector{
取值:
_:选择IE6及以下。连接线(中划线)(-)亦可使用,为了避免与某些带中划线的属性混淆,所以使用下划线(_)更为合适。
*:选择IE7及以下。诸如:(+)与(#)之类的均可使用,不过业界对(*)的认知度更高
\9:选择IE6+
\0:选择IE8+和Opera
[;property:value;];:选择webkit核心浏览器(Chrome,Safari)。IE7及以下也能识别。中括号内外的3个分号必须保留,第一个分号前可以是任意规则或任意多个规则[;color:#f00;]; 与 [color:#f00;color:#f00;]; 与 [margin:0;padding:0;color:#f00;]; 是等价的。生效的始终是中括号内的最后一条规则,所以通常选用第一种写法最为简洁。
说明:
选择不同的浏览器及版本
- 尽可能减少对CSS Hack的使用。Hack有风险,使用需谨慎
- 通常如未作特别说明,本文档所有的代码和示例的默认运行环境都为标准模式。
一些CSS Hack由于浏览器存在交叉认识,所以需要通过层层覆盖的方式来实现对不同浏览器进行Hack的。如下面这个例子:
.test{
color:#090\9; /* For IE8+ */
*color:#f00; /* For IE7 and earlier */
_color:#ff0; /* For IE6 and earlier */
}
3、选择器前缀hack
语法:
说明:
选择不同的浏览器及版本
- 尽可能减少对CSS Hack的使用。Hack有风险,使用需谨慎
- 通常如未作特别说明,本文档所有的代码和示例的默认运行环境都为标准模式。
- 一些CSS Hack由于浏览器存在交叉认识,所以需要通过层层覆盖的方式来实现对不同浏览器进行Hack的。
* html .test{color:#090;} /* For IE6 and earlier */
* + html .test{color:#ff0;} /* For IE7 */
.test:lang(zh-cn){color:#f00;} /* For IE8+ and not IE */
.test:nth-child(1){color:#0ff;} /* For IE9+ and not IE */
(学习视频分享:web前端入门)
The above is the detailed content of What are the common css hacks?. For more information, please follow other related articles on the PHP Chinese website!
The Benefits of React: Performance, Reusability, and MoreApr 15, 2025 am 12:05 AMReact’s popularity includes its performance optimization, component reuse and a rich ecosystem. 1. Performance optimization achieves efficient updates through virtual DOM and diffing mechanisms. 2. Component Reuse Reduces duplicate code by reusable components. 3. Rich ecosystem and one-way data flow enhance the development experience.
React: Creating Dynamic and Interactive User InterfacesApr 14, 2025 am 12:08 AMReact is the tool of choice for building dynamic and interactive user interfaces. 1) Componentization and JSX make UI splitting and reusing simple. 2) State management is implemented through the useState hook to trigger UI updates. 3) The event processing mechanism responds to user interaction and improves user experience.
React vs. Backend Frameworks: A ComparisonApr 13, 2025 am 12:06 AMReact is a front-end framework for building user interfaces; a back-end framework is used to build server-side applications. React provides componentized and efficient UI updates, and the backend framework provides a complete backend service solution. When choosing a technology stack, project requirements, team skills, and scalability should be considered.
HTML and React: The Relationship Between Markup and ComponentsApr 12, 2025 am 12:03 AMThe relationship between HTML and React is the core of front-end development, and they jointly build the user interface of modern web applications. 1) HTML defines the content structure and semantics, and React builds a dynamic interface through componentization. 2) React components use JSX syntax to embed HTML to achieve intelligent rendering. 3) Component life cycle manages HTML rendering and updates dynamically according to state and attributes. 4) Use components to optimize HTML structure and improve maintainability. 5) Performance optimization includes avoiding unnecessary rendering, using key attributes, and keeping the component single responsibility.
React and the Frontend: Building Interactive ExperiencesApr 11, 2025 am 12:02 AMReact is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent
React and the Frontend Stack: The Tools and TechnologiesApr 10, 2025 am 09:34 AMReact is a JavaScript library for building user interfaces, with its core components and state management. 1) Simplify UI development through componentization and state management. 2) The working principle includes reconciliation and rendering, and optimization can be implemented through React.memo and useMemo. 3) The basic usage is to create and render components, and the advanced usage includes using Hooks and ContextAPI. 4) Common errors such as improper status update, you can use ReactDevTools to debug. 5) Performance optimization includes using React.memo, virtualization lists and CodeSplitting, and keeping code readable and maintainable is best practice.
React's Role in HTML: Enhancing User ExperienceApr 09, 2025 am 12:11 AMReact combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.
React Components: Creating Reusable Elements in HTMLApr 08, 2025 pm 05:53 PMReact components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools






