【前端面试题1】伪类与伪元素有什么区别

伪类与伪元素的区别:

1.伪类使用单冒号,而伪元素使用双冒号。如 :hover 是伪类,::before 是伪元素

2.伪元素会在文档流生成一个新的元素,但伪元素本身并不是DOM元素,并且可以使用 content 属性设置内容


CSS伪类与伪元素选择器列举

伪元素

  • ::before
  • ::after
  • ::selection
  • ::placeholder
  • ::marker

伪类

  • :hover
  • :first-child
  • :last-child
  • :nth-child
选择器 例子 例子描述
:active a:active 选择活动的链接。
:checked input:checked 选择每个被选中的 元素。
:disabled input:disabled 选择每个被禁用的 元素。
:empty p:empty 选择没有子元素的每个元素。
:enabled input:enabled 选择每个已启用的 元素。
:first-child p:first-child 选择作为其父的首个子元素的每个元素。
:first-of-type p:first-of-type 选择作为其父的首个元素的每个元素。
:focus input:focus 选择获得焦点的 元素。
:hover a:hover 选择鼠标悬停其上的链接。
:in-range input:in-range 选择具有指定范围内的值的 元素。
:invalid input:invalid 选择所有具有无效值的 元素。
:lang(language) p:lang(it) 选择每个 lang 属性值以 "it" 开头的元素。
:last-child p:last-child 选择作为其父的最后一个子元素的每个元素。
:last-of-type p:last-of-type 选择作为其父的最后一个元素的每个元素。
:link a:link 选择所有未被访问的链接。
:not(selector) :not§ 选择每个非元素的元素。
:nth-child(n) p:nth-child(2) 选择作为其父的第二个子元素的每个元素。
:nth-last-child(n) p:nth-last-child(2) 选择作为父的第二个子元素的每个元素,从最后一个子元素计数。
:nth-last-of-type(n) p:nth-last-of-type(2) 选择作为父的第二个元素的每个元素,从最后一个子元素计数
:nth-of-type(n) p:nth-of-type(2) 选择作为其父的第二个元素的每个元素。
:only-of-type p:only-of-type 选择作为其父的唯一元素的每个元素。
:only-child p:only-child 选择作为其父的唯一子元素的元素。
:optional input:optional 选择不带 "required" 属性的 元素。
:out-of-range input:out-of-range 选择值在指定范围之外的 元素。
:read-only input:read-only 选择指定了 "readonly" 属性的 元素。
:read-write input:read-write 选择不带 "readonly" 属性的 元素。
:required input:required 选择指定了 "required" 属性的 元素。
:root root 选择元素的根元素。
:target #news:target 选择当前活动的 #news 元素(单击包含该锚名称的 URL)。
:valid input:valid 选择所有具有有效值的 元素。
:visited a:visited 选择所有已访问的链接。
相关推荐
我是Superman丶20 小时前
Element UI 表格某行突出悬浮效果
前端·javascript·vue.js
恋猫de小郭21 小时前
你的代理归我了:AI 大模型恶意中间人攻击,钱包都被转走了
前端·人工智能·ai编程
xiaokuangren_21 小时前
前端css颜色
前端·css
java1234_小锋21 小时前
Java高频面试题:Springboot的自动配置原理?
java·spring boot·面试
hoiii18721 小时前
C# 基于 LumiSoft 实现 SIP 客户端方案
前端·c#
anOnion21 小时前
构建无障碍组件之Meter Pattern
前端·html·交互设计
小码哥_常1 天前
Spring Boot配置diff:解锁配置管理新姿势
前端
xiaoye37081 天前
Spring 中高级面试题
spring·面试
小码哥_常1 天前
告别onActivityResult!Android数据回传的3大痛点与终极解决方案
前端
hhcccchh1 天前
1.2 CSS 基础选择器、盒模型、flex 布局、grid 布局
前端·css·css3