Home Web Front-end CSS Tutorial What is the difference between pseudo-classes and pseudo-elements in css? The difference between :before and ::before

What is the difference between pseudo-classes and pseudo-elements in css? The difference between :before and ::before

Oct 26, 2018 pm 04:35 PM
::before css Pseudo element Pseudo class

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才生效 */ }
    Copy after login

    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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

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.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

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-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

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

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

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.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

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.

See all articles