Why is a pseudo-class so called?

前端 未结 5 577
甜味超标
甜味超标 2020-12-11 04:45
a:hover

Why is it called a \"pseudo-class\"?

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

相关标签:
5条回答
  • 2020-12-11 05:02

    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.

    0 讨论(0)
  • 2020-12-11 05:05

    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.

    0 讨论(0)
  • 2020-12-11 05:11

    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

    0 讨论(0)
  • 2020-12-11 05:17

    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.

    0 讨论(0)
  • 2020-12-11 05:26

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

    0 讨论(0)
提交回复
热议问题