css优先级
很早以前就知道对同一个元素设置样式时应用样式的优先级是
内联样式>页内样式>大于外联样式
id选择器>class选择器>标签选择器
很简单 一笔带过
但事实上 如果是做前端开发的 还需要更了解详细些
1. !important 是拥有绝对最高优先级的 (设置此值后无论id选择器还是内联样式都无法撼动它)
.set_size{width:inherit !important;}
2.如果不小心在写样式时将外联样式放在了自己单独指定的页内样式前面 外联样式优先级将高于页内样式 这主要是引入css库时需要注意的
但此时内联样式的优先级仍高于外联样式的类选择器优先级
页内样式的id选择器仍高于外联样式的类选择器
此时 内联>外联id>页内id>外联类选择>页内类选择
3.*通配符选择器,会选择页面所有的元素 相当于对页面的每一个元素都应用了一次此样式
很强大,但不好 正确的使用姿势应该是与后代选择器配合使用 如
.foot_thin *{color:blue;}
正常情况下
内联>页内id>外部id>页内类选择>外部类选择......
后代选择器优先级高于类选择器
p span{color:red;}
子选择器优先级和后代选择器相等(在写法相同的情况下)
p > span{color:blue;}
同一css文件中此时是red还是blue 取决于谁写在谁的上一行 在后面的优先级不会更高 但浏览器后渲染 定下来了后面的样式
优先级会叠加
p.is > span{color:black;} 此时优先级会加上类选择器的优先级
id>后代选择
256个类选择器干掉一个id选择器
http://www.zhangxinxu.com/study/201208/256-class-fire-an-id.html
绝不忘记目的( never forgotten purpose )
需要了解css优先级是因为我们需要准确的精简冗余低的便利的可重用的高效的选择元素 从而应用样式 构建一个棒棒的web站点(当然性能优化这方面的东西很大重头戏在后台)
但是这不代表运行在前端的东西就不用考虑性能 完全不去考虑前端的性能,这合适吗?
答案显而易见
效率从高到低为
id选择器(#myid)
类选择器(.myclassname)
标签选择器(div,h1,p)
相邻选择器(h1+p)
子选择器(ul 后代选择器(li a)
通配符选择器(*)
属性选择器(a[rel="external"])
伪类选择器(a:hover,li:nth-child)
但写css的前端,大家基本不会业余到用id选择器
而伪类是有必要的而常用的
更多up可读
http://stevesouders.com/
http://www.tuicool.com/articles/INbuiq
http://csswizardry.com/2011/09/writing-efficient-css-selectors/ or http://www.w3cplus.com/css/css-selector-performance
http://www.jianshu.com/p/268c7f3dd7a6

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.


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

Atom editor mac version download
The most popular open source editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
