What is the difference between pseudo-classes and pseudo-elements?

后端 未结 2 1330
你的背包
你的背包 2020-12-04 23:35

What is the different between div::after {} and div:after {} ? When do we have to use :: over :?

D

2条回答
  •  醉酒成梦
    2020-12-04 23:58

    From https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Pseudo-classes_and_pseudo-elements

    Pseudo-class :

    A CSS pseudo-class is a keyword, preceded by a colon (:), added to the end of selectors to specify you want to style the selected elements, and only when they are in certain state. For example, you might want to style an element only when it is being hovered over by the mouse pointer, or a checkbox when it is disabled or checked, or an element that is the first child of its parent in the DOM tree.

    Examples:

    • :active
    • :checked
    • :nth-child()
    • :first
    • :hover

    Pseudo-elements ::

    Pseudo-elements are very much like pseudo-classes, but they have differences. They are keywords, this time preceded by two colons (::), that can be added to the end of selectors to select a certain part of an element.

    Examples:

    • ::after
    • ::before
    • ::first-letter
    • ::first-line
    • ::selection
    • ::backdrop

    As stated by @stephanmg:

    In practice ::before is used as :before and ::after is used as :after because of browser compatibility. Both are pseudo-elements, but may look like pseudo classes. This might be confusing if you read CSS code.

提交回复
热议问题