


What 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
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 | ||||||||||||||||||
Select the nth element of a certain type that is the penultimate element of its parent element | 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 | ||||||||||||||||||
Select a form element that is optional, that is, there is no "required" attribute | ##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
- The two writing methods are equivalent, and both represent pseudo elements.
- :before is the writing method of CSS2, ::before is the writing method of CSS3.
- ::before has better compatibility than ::before, but it is recommended to use ::before
# in H5 development
- Pseudo elements must be used together with the content attribute
-
# #Special effects of pseudo-elements usually use:hover pseudo-class style to activate
-
Reference address: https://www.cnblogs.com/ammyben/p /8012747.html.test:hover::before { /* 这时animation和transition才生效 */ }
Copy after login
##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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
