空个符号 css,在CSS选择器中使用空格或大于符号>

不,他们是完全不同的,使用>选择一个子元素,而使用空格将选择任何级别的嵌套元素.

例如…

在选择器中使用␣/空格…

This will be selected
This will be selected as well

所以在这里,具有空格的选择器将在父元素的任何嵌套级别定位div.

Demo(使用␣/空格)

.welcome div {

font-size: 20px;

color: #f00;

}

使用>

This won't be selected
This will be selected

而在这里,选择器将定位您的div,该元素是具有.welcome的元素的子元素,但它不会选择嵌套在段标签内的div,因为它是外部div的孙(但不是子)).

Demo 2(使用>)

.welcome > div {

font-size: 20px;

color: #f00;

}

从MDN:(对于␣)

The ␣ combinator (that’s meant to represent a space,or more

properly one or more whitespace characters) combines two selectors

such that the combined selector matches only those elements matching

the second selector for which there is an ancestor element matching

the first selector. Descendant selectors are similar to child

selectors,but they do not require that the relationship between

matched elements be strictly parent-child.

从MDN :(对于>)

The > combinator separates two selectors and matches only those elements matched by the second selector that are direct children of elements matched by the first. By contrast,when two selectors are combined with the descendant selector,the combined selector expression matches those elements matched by the second selector for which there exists an ancestor element matched by the first selector,regardless of the number of “hops” up the DOM.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 元素选择(Element Selector):使用HTML元素的标签名作为选择,例如`p`、`div`等。 2. 类选择(Class Selector):以`.`符号开头,后面跟上类名,例如`.my-class`。 3. ID选择(ID Selector):以`#`符号开头,后面跟上ID名,例如`#my-id`。 4. 属性选择(Attribute Selector):根据元素的属性来选择元素,例如`[type="text"]`可以选择所有type属性值为"text"的元素。 5. 后代选择(Descendant Selector):使用空格将多个选择连接起来,表示选取某个元素的后代元素,例如`div p`表示选取所有嵌套在`div`内的`p`元素。 6. 子元素选择(Child Selector):使用大于号`>`将父元素和子元素连接起来,表示只选择直接子元素,例如`ul > li`表示选取所有直接嵌套在`ul`内的`li`元素。 7. 相邻兄弟选择(Adjacent Sibling Selector):使用加号`+`将相邻的兄弟元素连接起来,表示选取紧跟在某个元素后面的兄弟元素,例如`h1 + p`表示选取紧跟在`h1`后面的`p`元素。 8. 通用兄弟选择(General Sibling Selector):使用波浪号`~`将兄弟元素连接起来,表示选取与某个元素有相同父元素且在其后的所有兄弟元素,例如`h1 ~ p`表示选取`h1`后的所有`p`元素。 9. 伪类选择(Pseudo-class Selector):表示元素的特殊状态,例如`:hover`表示鼠标悬停时的状态。 10. 伪元素选择(Pseudo-element Selector):表示元素的特定部分,例如`::before`表示在元素内容前插入一个虚拟的元素。 这是一些常见的CSS选择,用于选择HTML文档的元素并应用样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值