网站链接:

https://www.w3.org/TR/CSS21/selector.html

CSS支持的所有选择器:

选择器 grouping

当几个选择器共享同一部分属性时,选择器可以放到同一组里。

下列两种css定义等价:

h1 { font-family: sans-serif }
h2 { font-family: sans-serif }
h3 { font-family: sans-serif }
h1, h2, h3 { font-family: sans-serif }

Universal selector

If the universal selector is not the only component of a simple selector, the “*” may be omitted.

下列这些css定义是等价的:

  1. *[lang=fr] and [lang=fr] are equivalent.
  2. *.warning and .warning are equivalent.
  3. *#myid and #myid are equivalent.

Descendant selectors

<html>
<style>
p {color: red;
}div p {color: blue;
}
</style>
<p>Parent p</p>
<div>
<span>1</span>
<span>2</span>  </div><div id = "jerry" actions tool="spa">
<p>3</p>
<p>4</p>    </div>
</html>

嵌套在div里的p变成了蓝色。

descendent 选择器和属性选择器搭配工作:

div p *[href] {color: blue;
}

匹配在div和p嵌套中的任意元素,且这些元素要包含href属性。

Child selectors

匹配直接子节点。

<html>
<style>
div>p {color: red;
}div p *[href] {color: blue;
}
</style><p>Parent p</p>
<div>
<span>1</span>
<span>2</span>  </div><div id = "jerry" actions tool="spa">
<p href="1"><span href="2">inside p </span>
</p>
<p>4</p>    </div>
</html>

必须是div的直接子节点p才行:

Adjacent sibling selectors

Adjacent sibling selectors have the following syntax: E1 + E2, where E2 is the subject of the selector. The selector matches if E1 and E2 share the same parent in the document tree and E1 immediately precedes E2, ignoring non-element nodes (such as text nodes and comments).

例子:

p + div {color: green;
}

修饰的是 + 号后面的div节点:

attribute selector

分为以下四种:

  • [att]

Match when the element sets the “att” attribute, whatever the value of the attribute.

  • [att=val]
    Match when the element’s “att” attribute value is exactly “val”.

  • [att~=val]
    Represents an element with the att attribute whose value is a white space-separated list of words, one of which is exactly “val”. If “val” contains white space, it will never represent anything (since the words are separated by spaces). If “val” is the empty string, it will never represent anything either.

  • [att|=val]
    Represents an element with the att attribute, its value either being exactly “val” or beginning with “val” immediately followed by “-” (U+002D). This is primarily intended to allow language subcode matches (e.g., the hreflang attribute on the a element in HTML) as described in BCP 47 ([BCP47]) or its successor. For lang (or xml:lang) language subcode matching, please see the :lang pseudo-class.

例子:

<html>
<style>div[att] {color: blue;
}div[att=red] {color: red;
}div[att~=green] {color: green;
}div[att|=lime] {color: lime;
}</style><div att>attribute
</div><div att="red">red
</div><div att="red green">green
</div><div att="lime">lime
</div><div att="lime-jerry">lime-Jerry
</div></html>

效果:

另一个例子:

The following rule will match for values of the “lang” attribute that begin with “en”, including “en”, “en-US”, and “en-cockney”:

*[lang|="en"] { color : red }

class 选择器

class选择器和attribute选择器可以互换:

<html>
<style>div.title{color: blue;
}div[class='red'] {color: red;
}div.green {color: green;
}</style><div class='title'>title
</div><div class='red'>red
</div><div class=green>green
</div>
</html>

效果:

div.green.red {color: lime;
}

如果出现多个., 这些由多个.连接起来的class名称是AND的关系,即必须同时包含这些class才匹配:

  • Pseudo-elements create abstractions about the document tree beyond those specified by the document language. For instance, document languages do not offer mechanisms to access the first letter or first line of an element’s content. CSS pseudo-elements allow style sheet designers to refer to this otherwise inaccessible information. Pseudo-elements may also provide style sheet designers a way to assign style to content that does not exist in the source document (e.g., the :before and :after pseudo-elements give access to generated content).
  • Pseudo-classes classify elements on characteristics other than their name, attributes or content; in principle characteristics that cannot be deduced from the document tree. Pseudo-classes may be dynamic, in the sense that an element may acquire or lose a pseudo-class while a user interacts with the document. The exceptions are ‘:first-child’, which can be deduced from the document tree, and ‘:lang()’, which can be deduced from the document tree in some cases.

Neither pseudo-elements nor pseudo-classes appear in the document source or document tree. 二者都不会出现在document tree里。

例子:

<html>
<style>div > p:first-child { color: red; }</style><div><p>1</p><p>2</p><p>3</p>
</div>
</html>

效果:

:hover, :active, and :focus

如果a标签没有href属性,则无法接受hover或者focus事件:

<html>
<style>a:focus { color: yellow }
a:focus:hover { color: red }</style><div>
<a >1</a>
<a >2</a>
</div>
</html>

点击tab键无法focus:

加上href属性后:

上图是focus属性激活的外观,color:yellow

鼠标放上去后,激活hover,color:red

更多Jerry的原创文章,尽在:“汪子熙”:

css 选择器学习笔记相关推荐

  1. CSS选择器学习笔记

    在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. "CSS" 列指示该属性是在哪个 CSS 版本中定义的.(CSS1.CSS2 还是 CSS3.) 选择器 例子 例子 ...

  2. CSS入门学习笔记(案例+详解)

    CSS入门学习笔记 一.CSS简介 1.什么是CSS? 2.为什么使用CSS? 3.CSS的作用 二.CSS语法 1.CSS基础语法 2.CSS注释语法 3.CSS应用方法 三.CSS选择器 1.元素 ...

  3. CSS选择器速记笔记

    去年我学jQuery的时候,曾经做过一点选择器(selector)的笔记. 这几天拿出来看了一下,发现很多都忘记了.所以,我决定把它们贴在这里,方便以后查看.这对其他朋友应该也是有用的,毕竟选择器是制 ...

  4. 阿ken的HTML、CSS的学习笔记_CSS3选择器(笔记四)

    你好,我是阿Ken 恭迎您能前来拜访 虽还未踏入社会尚处象牙塔内 却总总陷入迷茫不知所措 有时很渴望有一个信仰能够成为我的精神支柱促使我一路坚持走下去 但苦于一直未果 后来短暂接触了一下这社会环境 也 ...

  5. CSS入门学习笔记+案例(1)

    CSS入门学习 一.CSS简介 1.什么是CSS CSS:Cascading Style Sheet 层叠样式表 是一组样式设置的规则,用于控制页面的外观样式 2.为什么使用CSS 实现内容与样式的分 ...

  6. CSS(3)学习笔记——持续更新

    本篇皆是本人长期记录并整理出来的笔记,如有记录得不对的地方,欢迎探讨.记录的很少,将不断学习不断补充. 1 选择器 CSS(3)中提供的选择器手册(w3school):http://www.w3sch ...

  7. HTML,CSS,JavaScript学习笔记--导航

    陆陆续续花了一个月的时间,把前端三大件HTML,CSS,JavaScript的基本语法学完了,感觉还是收益颇丰的.但是其中的知识远远不止这些,以后学习的过程中在进行补充吧. HTML,CSS学习笔记 ...

  8. html、css基础---学习笔记

    title: VScode编辑同步Markdown文档到印象笔记 tags: Blog,Elegent notebook: Elegent 超文本标记语言 超文本 : 文本内容 + 非文本内容 所谓H ...

  9. html,CSS,JavaScript学习笔记

    <html> <head><title>练习1.1</title> </head> <body><p align='cen ...

最新文章

  1. 【c语言】蓝桥杯算法提高 算术运算
  2. 新成立的Scala中心将重点关注教育和Scala社区
  3. IDEA---Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found
  4. Scrapy介绍及入门
  5. tomcat配置与优化
  6. ASP.NET Core 实战:基于 Jwt Token 的权限控制全揭露
  7. Java——UPD输出及优化再优化
  8. android matrix 缩放,android – 如何获取任意矩阵的缩放值?
  9. 大学计算机vfp最新考试题库,大学计算机vfp考试选择题题库.doc
  10. 从数据库中导出数据库文档
  11. 湖南大学计算机学院张柏杨,王树林-湖大信息科学与工程学院
  12. Usage of API documented as @since 1.8+”报错的解决办法
  13. 如何使用谷歌colab
  14. 如何做一个不被喷的产品经理?
  15. 熊猫烧香 给人的深思
  16. CSDN 学习勋章获得攻略
  17. bboss-elasticsearch--使用/教程/实例
  18. HBase :HBase高级shell管理命令
  19. RPC 框架基本了解
  20. 【Kernel】如何从kernel中获取cred结构体中的value

热门文章

  1. 4 .2 .4 配置存储系统
  2. mysql源代码安装
  3. Android自定义AlertDialog
  4. 扩展easyui tree的两个方法 获取实心节点
  5. 【设计模式系列】行为型模式之Mediator模式
  6. sql 无法删除当前数据库,因为当前数据库正在使用
  7. 没想到咱也算得上是先富起来的那一拨
  8. 【Task5(2天)】模型调参
  9. c/c++ 阻塞和非阻塞,fcntl应用
  10. SecureCRT同时向多个终端发送命令