为什么这么称呼伪类?

发布于 2024-07-30 19:19:18 字数 78 浏览 11 评论 0原文

a:hover

为什么叫“伪类”呢?

与“类”的概念有什么相似之处吗?

a:hover

Why is it called a "pseudo-class"?

Are there any similarities with the concept of "class"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

未央 2024-08-06 19:19:18

在 CSS 术语中,类是一个以句号开头的选择器,例如,

.foo { ... }

它将以以下形式使用。

<div class="foo">

“类”的使用更多的是指“具有共同特征并与其他事物不同的事物的集合或类别”按种类或质量”,而不是借用面向对象的术语。

伪类“不完全是一个真实的类”,因为用户代理定义何时和/或多少内容符合资格(例如 :hover:active 等)。

In CSS terms, a class is a selector that starts with a full stop, e.g.

.foo { ... }

It would be used in the form

<div class="foo">

This use of "class" is more in the sense "a set or category of things having a common characteristic and differentiated from others by kind or quality", rather than borrowing from OO terminology.

A pseudo class is "not quite a real one" as the user agent defines when and/or how much content qualifies (like :hover, :active, etc).

猫九 2024-08-06 19:19:18

这是,因为你没有成功; 浏览器“创建”了它,并允许您对其进行设置以更改链接处于该状态时的外观。

It's pseudo because you didn't make it; the browser 'created' it and allows you to set it to change the look of the link when it's in that state.

永言不败 2024-08-06 19:19:18

来自 w3c CSS2 选择器规范

CSS 引入了伪元素和伪类的概念,以允许基于文档树外部的信息进行格式化。

  • 伪元素创建超出文档语言指定的有关文档树的抽象。 例如,文档语言不提供访问元素内容的第一个字母或第一行的机制。 CSS 伪元素允许样式表设计者引用这些原本无法访问的信息。 伪元素还可以为样式表设计者提供一种将样式分配给源文档中不存在的内容的方法(例如,:before 和:after 伪元素可以访问生成的内容)。

  • 伪类根据名称、属性或内容以外的特征对元素进行分类; 原则上无法从文档树推断出的特征。 伪类可以是动态的,即当用户与文档交互时元素可能获取或丢失伪类。 例外情况是“:first-child”(可以从文档树中推导出来)和“:lang()”(在某些情况下可以从文档树中推导出来)。

基本上,伪类是您可以附加样式的东西,但您永远不会在 HTML 中自己打印它。 此外,根据用户与 UI 的交互,可以获取和丢失伪类。

From the w3c CSS2 selector spec:

CSS introduces the concepts of pseudo-elements and pseudo-classes to permit formatting based on information that lies outside the document tree.

  • 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.

So basically, a pseudo-class is something you can attach a style to, but you never print it out yourself in the HTML. Also, a pseudo-clas can be aquired and lost depending on user interaction with the UI.

孤君无依 2024-08-06 19:19:18

随着 CSS2/3 允许更复杂的元素规则(例如 input[type=checkbox] 等),术语伪类似乎越来越过时。

然而,伪类是唯一(或多或少)的 CSS 标识符使用属性选择器等,大多数浏览器倾向于遵循页面加载时所有元素的状态,并且所做的任何更改都会被忽略,但是使用伪类时,它们实际上会在伪类发生变化时更改样式。 。

因此,考虑到该特定定义,它们是类,因为该规则适用于共享相同“状态”的任何元素,因此可以被视为“类”,但它是伪的,因为它不是“类” t 是一个真正的属性定义的类,并且因为在查看页面的任何给定时间,“类”可能为真,也可能不为真,

我认为,对于某些基于 UI 的伪类(我是这样)也很有趣。特别考虑 :hover)在任何给定时间只有一个元素可以真正拥有该“类”,因此根据我上面的定义,它几乎更像是一个伪 id。

With CSS2/3 allowing for more sophisticated element rules (things like input[type=checkbox] and the like, the term pseudo-class seems more and more dated.

However, pseudo-classes are the only CSS identifiers that (more or less) reliably change with user interactions. With attribute selectors and what not, most browsers tend to go with the state of all elements on page load and any changes made are ignored. But with pseudo-classes, they actually change the style when the pseudo-class becomes true (or untrue).

So with that specific definition in mind, they are classes because the rule applies to any elements that share the same "state" and thus can be considered of a "class", but it's pseudo because it isn't a true attribute-defined class and because the "class" may or may not be true at any given time the page is viewed.

It's also interesting to note, I think, that with certain UI-based pseudo-classes (I'm thinking specifically of :hover) only one element at any given time can really have that "class" so it's almost more of a pseudo-id, based on my above definition.

紙鸢 2024-08-06 19:19:18

我想认为它们被称为伪类,因为它们没有由作者明确定义,而是至少从作者甚至用户代理的角度来看是一种逻辑或心理构造。

遵循理解 HTML 文档时经常使用的家庭比喻,您可以想象一些元素会自己思考,比如我是第一个孩子,我是一个 div,也是第四个孩子(意味着我的位置也是偶数)...对于像悬停这样由用户引起的事情,我们可以想象,当 a 标签或任何元素悬停在其上时,它会立即分组到已触摸的事物中,并且将应用相应的样式......并且如果可以将鼠标悬停在上面同时存在两个元素,他们会同时认为我们已被触摸,因此属于该类(悬停在事物上),因此两者都具有相同的风格

I would like to think they are called pseudo classes because they werent explicitly defined by the author but rather are a sort of logical or mental construct at least from the perspective of the author or even the user agent.

following the family metaphor often employed in understanding HTML documents you can imagine some elements thinking to themselves things like well am the first-child, i'm a div and also the 4th child(meaning my position is also even)...and as for things like hovering that are user induced one can imagine that when the a tag or whatever element is hovered on that it instantly gets grouped among things that have been touched and the corresponding style will be applied...and if its possible to hover over two elements at the same time that they would simultaneously think to themeselves we have been touched and so belong to the class(hovered over things) and hence both take on the same styles

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文