css伪类选择器_伪类选择器

css伪类选择器

Pseudo-class selectors affect elements in certain states: for example, a:hover controls the appearance of links during a momentary touch on a mobile device, or on mouse hover. In contrast, pseudo-element selectors control the appearance of portions of an element’s content, such as the first line or letter of a paragraph.

伪类选择器会影响某些状态下的元素:例如, a:hover控制在移动设备或鼠标悬停的短暂触摸期间链接的外观。 相反, 元素选择器控制元素内容各部分的外观,例如段落的第一行或字母。

The table below covers all the current pseudo-class selectors in CSS, with links to appropriate articles on the selectors:

下表涵盖了CSS中所有当前的伪类选择器,并提供了有关选择器上相应文章的链接:

CSS Pseudo-class selectors
Pseudo-class selectorPurpose
:linkControls the appearance of an unvisited link. Strongly associated with the a tag, to the point at which the a selector used by itself before a style declaration accomplishes the same thing.
:visitedControls the appearance of visited links. Quite a few restrictions on styles in terms of what can be applied, due to security risks.
:hoverControls the appearance of an element while it is being hovered over with a mouse or other pointing device. While it is heavily associated with the a tag, CSS does not explicitly specify this association. In modern browsers, :hover can be applied to any arbitrary element, such as img:hover.
:activeThe appearance of content while the user is clicking on it, i.e. between the mouse button being held down and it being released. Rarely used, since the effect is usually transient.
:first-childSelects the first child element of a markup sequence. For example, ul li:first-child would specify that the style declaration applies to the first li element in any unordered list.
:last-childSelects the last child element in a sequence.
:nth-child(n) Selects children in an element sequence, in iterations of (n), where n is the word odd, even, a number, or a mathematical expression.
:nth-of-type(n) Selects the nth appearance of a particular element.
:focusSelects links that have been reached via keyboard navigation and form elements that are selected, such as text inputs and textareas.
:only-childSelects an element if it is the only child of a parent.
:first-of-typeSelects the first appearance of an element
:last-of-typeSelects the last appearance of an element.
:only-of-typeSelects an element if it is the only child of a particular type inside its parent.
:nth-last-child(n)Counts backwards from the end of a set of child elements.
:nth-last-of-type(n)Counts backwards from the end of a set of child elements to include a certain sequence of tags.
:not(S)Matches elements that are not (S).
:validThe state of a form element that is considered valid to a defined pattern of characters or numbers.
:invalidThe state of a form element with content that is considered invalid.
:checkedSelects form elements that are checked, such as radio buttons and checkboxes.
CSS伪类选择器
伪类选择器 目的
:link 控制未访问链接的外观。 与a标签紧密相关,以至于样式声明之前由其自身使用a选择器完成相同的操作。
:visited 控制访问链接的外观。 由于存在安全风险,因此在样式方面有很多限制。
:hover 在用鼠标或其他定点设备悬停元素时控制元素的外观。 尽管它与a标签紧密相关,但CSS并未明确指定此关联。 在现代浏览器中, :hover可以应用于任何任意元素,例如img:hover
:active 内容在用户单击时的外观,即在按住鼠标按钮和释放按钮之间。 很少使用,因为效果通常是短暂的。
:first-child 选择标记序列的第一个子元素。 例如, ul li:first-child将指定样式声明适用于任何无序列表中的第一个li元素。
:last-child 选择序列中的最后一个子元素。
:nth-child(n) (n)迭代中选择元素序列中的子代 ,其中n是单词oddeven ,数字或数学表达式。
:nth-of-type(n) 选择特定元素的第n个外观。
:focus 选择通过键盘导航已到达的链接以及所选的表单元素,例如文本输入和文本区域。
:only-child 选择一个元素(如果它是父元素的唯一子元素)。
:first-of-type 选择元素的首次出现
:last-of-type 选择元素的最后一个外观。
:only-of-type 如果元素是其父元素内特定类型的唯一子元素,则选择该元素。
:nth-last-child(n) 从一组子元素的末尾往前计算。
:nth-last-of-type(n) 从一组子元素的末尾向后计数,包括标签一定的顺序。
:not(S) 匹配不是 ( S )的元素。
:valid 被认为对定义的字符或数字模式有效的表单元素的状态。
:invalid 内容被视为无效的表单元素的状态。
:checked 选择选中的表单元素,例如单选按钮和复选框

Note that pseudo-selectors can be strung together: a:visited:hover, for example.

请注意,伪选择器可以串在一起:例如, a:visited:hover

翻译自: https://thenewcode.com/47/Pseudo-class-selectors

css伪类选择器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS伪类选择器是一种用于选择HTML元素的CSS选择器,它们不是基于元素的属性,而是基于元素在文档中的位置、状态或内容等特征进行选择。而CSS计数器是一种伪类选择器,它通过一个变量来设置,根据规则递增变量。可以用于为元素添加序号或计数器。下面是一些常见的CSS计数器伪类选择器: 1. :first-child:选择某个元素的第一个子元素。 2. :last-child:选择某个元素的最后一个子元素。 3. :nth-child():选择某个元素的一个或多个特定的子元素。 4. :nth-last-child():选择某个元素的一个或多个特定的子元素,从这个元素的最后一个子元素开始算。 5. :first-of-type:选择一个上级元素下的第一个同类子元素。 6. :last-of-type:选择一个上级元素的最后一个同类子元素。 7. :only-child:选择的元素是它的父元素的唯一一个子元素。 8. :only-of-type:选择一个元素是它的上级元素的唯一一个相同类型的子元素。 9. :nth-of-type():选择指定的元素。 10. :nth-last-of-type():选择指定的元素,从元素的最后一个开始计算。 CSS计数器可以通过设置变量和规则来实现元素的计数,可以用于为元素添加序号或计数器。例如,可以使用以下代码为每个元素添加一个计数器,并在每个元素前添加“Section <计数值>:”: ``` body { counter-reset: section; } .element:before { counter-increment: section; content: "Section " counter(section) ": "; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值