search
HomeWeb Front-endCSS TutorialWhat is the difference between pseudo-classes and pseudo-elements in css? The difference between :before and ::before

The content of this article is to introduce to you what is the difference between pseudo-classes and pseudo-elements in CSS? The difference between :before and ::before. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Pseudo class is used to select information outside the DOM tree, or information that cannot be represented by simple selectors. The former includes those elements that match the specified status, such as :visited, :active; the latter includes those elements in the DOM tree that meet certain logical conditions, such as :first- child, :first-of-type, :target.

(Equivalent to a special class selector, used to add some special effects)

Pseudo element is a virtual element that is not defined in the DOM tree. Unlike other selectors, it does not take the element as the smallest selection unit, it selects the specified content of the element. For example, ::before represents the content before the selection element, that is, ""; ::selection represents the selected content of the selection element.

(Equivalent to a special element (p, span), which can be used to store some special styles or content)

In CSS3 , there are also differences in syntax between pseudo-classes and pseudo-elements. The pseudo-elements are modified to start with ::. However, due to historical reasons, browsers continue to support pseudo-elements starting with :, but it is recommended to write them in the standard format starting with ::.

  • pseudo-class

##:nth-last-of-type (n)Select the nth element of a certain type that is the penultimate element of its parent element3##:optionalSelect a form element that is optional, that is, there is no "required" attribute:read- only:read-write:root
  • Pseudo-Element

Selector Meaning CSS
:active Select the element that is being activated 1
:hover Select the element that is hovered by the mouse 1
##:link Select unvisited elements 1
:visited Select the visited element 1
:first-child Select the element that is the first child element of its parent element 2
:lang Select elements with the specified lang attribute 2
:focus Select the element with keyboard input focus 2
:enable Select each enabled element 3
:disable Select each disabled element 3
:checked Select each selected element 3
:target Select the current anchor element 3
:first-of-type Select An element that is the first child element of a certain type of its parent element 3
:last-of -type Select the element that is the last child element of a certain type of its parent element 3
:only-of-type Select the element that is the only child element of a certain type that is the only child element of its parent element 3
:nth-of-type(n) The selection satisfies its parent Element of the nth child element of a certain type 3
:only-child Select the element that is the only child element of its parent element 3
:last-child The selection satisfies the last element of its parent Element of element 3
:nth-child(n) Select elements that are the nth child element of their parent element 3
:nth- last-child(n) Select the element that is the n-th child element from the bottom of its parent element 3
:empty Select elements that have no child elements 3
:in-range Select elements whose values ​​are within the specified range 3
##:out-of-range Select elements whose values ​​are not within the specified range 3
:invalid Select elements whose values ​​are invalid 3
:valid #Select the value that satisfies the requirement to be valid Value element 3
:not(selector) Select elements that do not satisfy the selector 3
##3
Select form elements with "readonly" 3
Select form elements without "readonly" 3
Select the root element 3
Selector Meaning CSS
::first-letter Select the first word of the specified element 1
::first-line Select the first line of the specified element 1
::after In front of the content of the specified element Insert content 2
::before at the specified Insert content after the content of the element 2
##::selection Select the content selected by the user in the specified element 3

##The difference between:before and ::before

  1. The two writing methods are equivalent, and both represent pseudo elements.

  2. :before is the writing method of CSS2, ::before is the writing method of CSS3.

  3. ::before has better compatibility than ::before, but it is recommended to use ::before

    # in H5 development

##Note:

    Pseudo elements must be used together with the content attribute
  1. Pseudo elements will not appear in the DOM, so they cannot be operated through js. They are just added to the CSS rendering layer
  2. # #Special effects of pseudo-elements usually use:hover pseudo-class style to activate
  3. .test:hover::before { /* 这时animation和transition才生效 */ }

    Reference address: https://www.cnblogs.com/ammyben/p /8012747.html

##https://blog.csdn.net/yangxiaoyanger/article/details/79712180

The above is the detailed content of What is the difference between pseudo-classes and pseudo-elements in css? The difference between :before and ::before. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software