CSSDiner :练习 CSS 选择器的游戏式网站,网站:CSSDiner: https://flukeout.github.io/

有些关卡有多种方法,第一种标准方法按照右侧指定的格式,其他举一反三;很多其实复杂化了,看看就好

# CSS 选择器

选择器例子例子描述
.class.intro选择 class="intro" 的所有元素。
.class1.class2.name1.name2选择 class 属性中同时有 name1 和 name2 的所有元素。
.class1 .class2.name1 .name2选择作为类名 name1 元素后代的所有类名 name2 元素。
#id#firstname选择 id="firstname" 的元素。
**选择所有元素。
elementp选择所有 <p> 元素。
element.classp.intro选择 class="intro" 的所有 <p> 元素。
element,elementdiv, p选择所有 <div> 元素和所有 < p> 元素。
element elementdiv p选择 <div> 元素内的所有 <p> 元素。
element>elementdiv > p选择父元素是 <div> 的所有 < p> 元素。
element+elementdiv + p选择紧跟 <div> 元素的首个 <p> 元素。
element1~element2p ~ ul选择前面有 <p> 元素的每个 <ul> 元素。
[attribute][target]选择带有 target 属性的所有元素。
[attribute=value][target=_blank]选择带有 target="_blank" 属性的所有元素。
[attribute~=value][title~=flower]选择 title 属性包含单词 "flower" 的所有元素。
[attribute|=value][lang|=en]选择 lang 属性值以 "en" 开头的所有元素。
[attribute^=value]a[href^="https"]选择其 src 属性值以 "https" 开头的每个 <a> 元素。
[attribute$=value]a[href$=".pdf"]选择其 src 属性以 ".pdf" 结尾的所有 <a> 元素。
[attribute*=value]a[href*="w3schools"]选择其 href 属性值中包含 "abc" 子串的每个 <a> 元素。
:activea:active选择活动链接。
::afterp::after在每个 <p> 的内容之后插入内容。
::beforep::before在每个 <p> 的内容之前插入内容。
:checkedinput:checked选择每个被选中的 <input> 元素。
:defaultinput:default选择默认的 <input> 元素。
:disabledinput:disabled选择每个被禁用的 <input> 元素。
:emptyp:empty选择没有子元素的每个 <p> 元素(包括文本节点)。
:enabledinput:enabled选择每个启用的 <input> 元素。
:first-childp:first-child选择属于父元素的第一个子元素的每个 <p> 元素。
::first-letterp::first-letter选择每个 <p> 元素的首字母。
::first-linep::first-line选择每个 <p> 元素的首行。
:first-of-typep:first-of-type选择属于其父元素的首个 <p> 元素的每个 <p> 元素。
:focusinput:focus选择获得焦点的 input 元素。
:fullscreen:fullscreen选择处于全屏模式的元素。
:hovera:hover选择鼠标指针位于其上的链接。
:in-rangeinput:in-range选择其值在指定范围内的 input 元素。
:indeterminateinput:indeterminate选择处于不确定状态的 input 元素。
:invalidinput:invalid选择具有无效值的所有 input 元素。
:lang(language)p:lang(it)选择 lang 属性等于 "it"(意大利)的每个 <p> 元素。
:last-childp:last-child选择属于其父元素最后一个子元素每个 <p> 元素。
:last-of-typep:last-of-type选择属于其父元素的最后 <p> 元素的每个 <p> 元素。
:linka:link选择所有未访问过的链接。
:not(selector):not(p)选择非 <p> 元素的每个元素。
:nth-child(n)p:nth-child(2)选择属于其父元素的第二个子元素的每个 <p> 元素。
:nth-last-child(n)p:nth-last-child(2)同上,从最后一个子元素开始计数。
:nth-of-type(n)p:nth-of-type(2)选择属于其父元素第二个 <p> 元素的每个 <p> 元素。
:nth-last-of-type(n)p:nth-last-of-type(2)同上,但是从最后一个子元素开始计数。
:only-of-typep:only-of-type选择属于其父元素唯一的 <p> 元素的每个 <p> 元素。
:only-childp:only-child选择属于其父元素的唯一子元素的每个 <p> 元素。
:nth-child(odd)p:nth-child(odd)选择属于其父元素的奇数个子元素的每个 <p> 元素。
:nth-child(even):nth-child(even)选择属于其父元素的偶数个子元素的每个 <p> 元素。
:nth-child(2n+3):nth-child(2n+3)选择属于其父元素的 2n+3 个子元素的每个 <p> 元素。
:optionalinput:optional选择不带 "required" 属性的 input 元素。
:out-of-rangeinput:out-of-range选择值超出指定范围的 input 元素。
::placeholderinput::placeholder选择已规定 "placeholder" 属性的 input 元素。
:read-onlyinput:read-only选择已规定 "readonly" 属性的 input 元素。
:read-writeinput:read-write选择未规定 "readonly" 属性的 input 元素。
:requiredinput:required选择已规定 "required" 属性的 input 元素。
:root:root选择文档的根元素。
::selection::selection选择用户已选取的元素部分。
:target#news:target选择当前活动的 #news 元素。
:validinput:valid选择带有有效值的所有 input 元素。
:visiteda:visited选择所有已访问的链接。

# CSS 选择器优先级

总结排序:!important > 行内样式 > ID 选择器 > 类选择器 > 标签 > 通配符 > 继承 > 浏览器默认属性

  • !important 规则最重要,大于其它规则
    行内样式规则,加 1000
    对于选择器中给定的各个 ID 属性值,加 100
    对于选择器中给定的各个类属性、属性选择器或者伪类选择器,加 10
    对于选择其中给定的各个元素标签选择器,加 1
    如果权值一样,则按照样式规则的先后顺序来应用,顺序靠后的覆盖靠前的规则

  • 注意:通用选择器(*)、子选择器(>)和相邻同胞选择器(+)并不在这四个等级中,所以他们的权值都为 0。 权重值大的选择器其优先级也高,相同权重的优先级又遵循后定义覆盖前面定义的情况。

# Level 1

# 标准方法

plate

# 其他方法

*

# Level 2

# 标准方法

bento

# 其他方法

*:nth-child(odd)

# Level 3

# 标准方法

#fancy

# 其他方法

plate:first-child
plate:nth-child(1)

# Level 4

# 标准方法

plate apple

# 其他方法

apple:first-child
plate>apple

# Level 5

# 标准方法

#fancy pickle

# 其他方法

bento+plate pickle

# Level 6

# 标准方法

.small

# 其他方法

apple.small
apple:nth-child(2),plate apple:first-child  /* 复杂化了 */

# Level 7

# 标准方法

orange.small

# Level 8

# 标准方法

bento orange.small

# Level 9

# 标准方法

plate,bento

# Level 10

# 标准方法

*

# Level 11

# 标准方法

plate *

# 其他方法

plate>*
plate orange,pickle,plate apple

# Level 12

# 标准方法

plate+apple

# Level 13

# 标准方法

bento~pickle

# Level 14

# 标准方法

plate>apple

# Level 15

# 标准方法

orange:first-child

# 其他方法

plate orange:first-child

# Level 16

# 标准方法

plate *:only-child

# 其他方法

plate :only-child
apple,plate pickle

# Level 17

# 标准方法

.small:last-child

# 其他方法

apple,pickle
apple.small,pickle.small
#fancy *:last-child , plate + pickle /* 逐渐复杂哈哈 */

# Level 18

# 标准方法

plate:nth-child(3)

# 其他方法

plate:nth-last-child(2)
plate:nth-last-of-type(2)

# Level 19

# 标准方法

bento:nth-last-child(3)

# 其他方法

bento:nth-child(2)
bento:nth-of-type(1)
bento:nth-last-of-type(2)

# Level 20

# 标准方法

apple:first-of-type

# 其他方法

apple:nth-child(2)
orange+apple

# Level 21

# 标准方法

plate:nth-of-type(even)

# 其他方法

plate:nth-of-type(2n)

# Level 22

# 标准方法

plate:nth-of-type(2n+3)

# 其他方法

plate:has(apple)  /* 一般不建议用,是 jQuery 里的 */

# Level 23

# 标准方法

apple:only-of-type

# 其他方法

plate apple.small:only-of-type

# Level 24

# 标准方法

.small:last-of-type

# 其他方法

orange:last-of-type , apple:last-of-type

# Level 25

# 标准方法

bento:empty

# 其他方法

bento:nth-child(1),bento:nth-child(4)

# Level 26

# 标准方法

apple:not(.small)

# Level 27

# 标准方法

[for]

# Level 28

# 标准方法

plate[for]

# Level 29

# 标准方法

[for="Vitaly"]

# 其他方法

bento:nth-child(3)
bento:nth-of-type(2)
bento:nth-last-child(2)
bento:nth-last-of-type(1)

# Level 30

# 标准方法

[for^="Sa"]

# 其他方法

[for^="S"]
plate,plate+bento
plate,bento:first-of-type

# Level 31

# 标准方法

[for$="ato"]

# 其他方法

[for$="to"]
[for$="o"]
bento,plate

# Level 32

# 标准方法

[for*="obb"]

# 其他方法

[for*="o"]
[for*="b"]
bento:nth-child(odd)
更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Lavender 微信支付

微信支付

Lavender 支付宝

支付宝