Table of Contents
Definition
Difference
History
Note
Summary
Home Web Front-end CSS Tutorial About the differences and precautions between pseudo-classes and pseudo-elements in CSS3

About the differences and precautions between pseudo-classes and pseudo-elements in CSS3

May 21, 2017 pm 03:29 PM

In CSS Pseudo classes and pseudo elements are easily confused

Today let’s talk about the difference between pseudo classes and pseudo elements


Definition

First let’s take a look at the definitions of pseudo-classes and pseudo-elements
w3cThis is how they are explained

  • Pseudo-class: used to add special effects to certain selectors

  • Pseudo-element: used to add special effects to certain selectors

To be honest, maybe my Chinese is not good, I think these two sentences are equivalent:-)
Can't see any difference at all
They all add "add" to some selectors Stunt"

The standard has such a sentence, which is translated as follows

CSS introduces the concepts of pseudo-classes and pseudo-elements in order to implement the document-based Formatting of information outside the tree

This is more abstract. In fact, it means supplementary elements that we cannot select through class, id, etc.

Difference

We need an example to understand this difference

<p>
    <em>This</em>
    <em>is a text</em></p>
Copy after login
Copy after login

What if we want the font color of the first em tag to turn red?
It is very simple to use the pseudo-classes we are familiar with

em:first-child {    color: red;}
Copy after login

But what do we do if there are no pseudo-classes?
This is what we need to do for the first em Tag adding class

<p>
    <em class="first-child">This</em>
    <em>is a text</em></p>
Copy after login
em.first-child {    color: red;}
Copy after login

can achieve the same effect


<p>
    <em>This</em>
    <em>is a text</em></p>
Copy after login
Copy after login

This is still an example
Now I want the first letter of this paragraph to turn red
How to do it
This time we need to use pseudo elements

p::first-letter {    color: red;}
Copy after login

Similarly assume that pseudo elements do not exist
At this time we can only nest span tags to achieve

<p>
    <em><span>T</span>his</em>
    <em>is a text</em></p>
Copy after login
p span {    color: red;}
Copy after login

Seeing this, I believe everyone has understood why one is called a pseudo-class and the other is called a pseudo-element
The effect of the pseudo-class can be achieved by adding actual classes
The effect of the pseudo-element can be By adding actual elements to achieve
Their essential difference isWhether abstraction creates new elements

History

Pseudo-classes were originally used to represent Dynamic elements (typical anchor pseudo-classes link, visited, hover, active)
It has been extended in the CSS2 standard so that although it exists logically, it does not need to be identified in the DOM tree
The pseudo-element represents a certain Although the child elements of an element exist logically, they do not exist in the DOM tree

Although their concepts are easily confused by us
, it does not affect our normal use

I am in CSS3 As mentioned in the introduction and usage summary of the selector
Pseudo classes can only use ":"
And pseudo elements can use either ":" or "::"
Here I will explain why


The standard in CSS3 is that pseudo-classes use a single colon ":"
and pseudo-elements use double colons "::" (to avoid confusion)
But before that, whether it was a pseudo-class or Pseudo-elements all use a single colon ":"
So in order to ensure compatibility with pseudo-elements, both methods of using pseudo-elements are possible
But low versions of IE have double-colon compatibility issues
So people who wrote styles in the past are not familiar with pseudo-classes and pseudo-elements simply use a single colon
causing this confusion to continue

Note

When using pseudo-classes and pseudo-elements
There is one thing to pay special attention to
Pseudo classes are just like real classes and can be used in combination
There is no upper limit on the number, as long as they are not mutually exclusive
For example,

em:first-child:hover {    color: red;}
Copy after login

This is completely possible
But note, here It is the relationship of "and"
That is to say, it must satisfy both the "first-child" first child element
and the "hover" cursor suspension


Pseudo elements must be strict The multiple
pseudo-elements can only appear once in a selector, and can only appear at the end
(Some students misunderstood here, so I made a modification)
Like the following The styles cannot take effect

p::first-letter:hover {  /*错误的写法:伪元素不是末尾*/
    color: red;}
Copy after login
p::first-letter::selection {  /*错误的写法:伪元素出现了多个*/
    color: red;}
Copy after login

One more thing about their priority
When calculating the weight
Pseudo-class and class priority are the same
Pseudo elements have the same priority as tags

Summary

  • Pseudo classes and pseudo elements are both used to add special effects to the selector

  • The essential difference between pseudo-classes and pseudo-elements is whether abstraction creates new elements

  • Pseudo-classes can be superimposed as long as they are not mutually exclusive

  • Pseudo elements can only appear once in a selector, and can only appear at the end

  • The priorities of pseudo classes and pseudo elements are the same as the priorities of classes and tags respectively

The above is the detailed content of About the differences and precautions between pseudo-classes and pseudo-elements in CSS3. 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

Video Face Swap

Video Face Swap

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

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 achieve wave effect with pure CSS3? (code example) How to achieve wave effect with pure CSS3? (code example) Jun 28, 2022 pm 01:39 PM

How to achieve wave effect with pure CSS3? This article will introduce to you how to use SVG and CSS animation to create wave effects. I hope it will be helpful to you!

Use CSS skillfully to realize various strange-shaped buttons (with code) Use CSS skillfully to realize various strange-shaped buttons (with code) Jul 19, 2022 am 11:28 AM

This article will show you how to use CSS to easily realize various weird-shaped buttons that appear frequently. I hope it will be helpful to you!

How to hide elements in css without taking up space How to hide elements in css without taking up space Jun 01, 2022 pm 07:15 PM

Two methods: 1. Using the display attribute, just add the "display:none;" style to the element. 2. Use the position and top attributes to set the absolute positioning of the element to hide the element. Just add the "position:absolute;top:-9999px;" style to the element.

How to implement lace borders in css3 How to implement lace borders in css3 Sep 16, 2022 pm 07:11 PM

In CSS, you can use the border-image attribute to achieve a lace border. The border-image attribute can use images to create borders, that is, add a background image to the border. You only need to specify the background image as a lace style; the syntax "border-image: url (image path) offsets the image border width inward. Whether outset is repeated;".

It turns out that text carousel and image carousel can also be realized using pure CSS! It turns out that text carousel and image carousel can also be realized using pure CSS! Jun 10, 2022 pm 01:00 PM

How to create text carousel and image carousel? The first thing everyone thinks of is whether to use js. In fact, text carousel and image carousel can also be realized using pure CSS. Let’s take a look at the implementation method. I hope it will be helpful to everyone!

Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3 Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3 Nov 20, 2023 am 11:20 AM

Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3. The specific code example is as follows: HTML code: &lt;divid="container"&gt;&lt;divclass="item"&gt ;First child element&lt;/div&gt;&lt;divclass="item"&

Why do pseudo-elements fail? Why do pseudo-elements fail? Nov 21, 2023 pm 05:13 PM

Reasons for pseudo-element failure: 1. Selector issues; 2. Style conflicts; 3. Inheritance issues; 4. Syntax errors; 5. Browser compatibility issues, etc. Detailed introduction: 1. Selector problem, the selector of the pseudo element may be incorrect, resulting in the target element not being selected; 2. Style conflict, if there is a style conflict in CSS, the pseudo element may become invalid; 3. Inheritance problem, Pseudo elements may not inherit certain style attributes; 4. Syntax errors. If there are syntax errors in CSS, the pseudo elements may fail; 5. Browser compatibility issues, etc.

css3 what is adaptive layout css3 what is adaptive layout Jun 02, 2022 pm 12:05 PM

Adaptive layout, also known as "responsive layout", refers to a web page layout that can automatically recognize the screen width and make corresponding adjustments; such a web page can be compatible with multiple different terminals instead of making a specific version for each terminal. . Adaptive layout was born to solve the problem of mobile web browsing, and can provide a good user experience for users using different terminals.

See all articles