


Pseudo-classes and pseudo-elements in CSS and the differences between them
This article mainly introduces the detailed explanation of pseudo-classes and pseudo-elements in CSS and the differences between them. In fact, CSS3 standardizes a simple and crude method, that is, using a pseudo-class before It is represented by a colon, and two colons are used before the pseudo element, so that it is not easily confused. Friends in need can refer to
The concept and function of CSS pseudo-class
#CSS pseudo-classes (Pseudoclasses) are the bolts of selectors, used to specify the state of one or related selectors. Their form is selector:pseudoclass{property:value;}, simply use a half-width English colon (:) to separate the selector and pseudo-class. CSS Many suggestions are not supported by browsers, but there are four CSS pseudo-classes that can be used safely for connections.
1.link is used for access connections.
2.visited is used on already visited connections.
3.hover is used for the connection on which the mouse cursor is placed.
4.active is used for connections that gain focus (for example, being clicked).
For example:
ExampleSourceCode a:link{ color:red } a:visited{ color:green } a:hover{ color:blue } a:active{ color:orange }
Although CSS gives you bypass control, using different colors to indicate that accessed connections are A good habit because many users still expect this. CSS pseudo-classes (except hover) are not commonly used, and I'm afraid they are not as commonly used in the past. Therefore, it is not as useful as it used to be. But if you can collect users' opinions, you will find that it should be used.
Traditionally, connection text is blue and visited connections are purple. Perhaps, this is the most effective and useful color. However, with the widespread development of CSS, this color is no longer commonplace, and users no longer assume that connections must be blue or purple.
You should also be able to use the hover pseudo-class on other elements besides connections. Unfortunately, InternetExplore does not support this. This is really a huge annoyance.
Pseudo-elements
In fact, pseudo-elements in CSS do not exist in HTML, and they are usually used for special processing of certain elements. Only then will commonly used pseudo-element be used:
:
:first-lineYou can specify the style of the first line of the p element
:
:first-letter You can specify the style of the first word of the p element
::selection
Define the effect of user highlight
:
:beforeIn the element Insert content before
:
:afterInsert content after the element
Example HTML
<p> 臣亮言,先帝創業未半<br> 每天,天剛亮時,我母親便把我喊醒,叫我披衣坐起。我從不知道她醒來坐了多久了。 </p>
##first-line and first-letter
/* 第一行樣式 */ p::first-line { color: red; } /* 開頭第一個字樣式 */ p::first-letter { font-size: 30px; } selection // 反白後的效果 p::selection { background: red; color: #FFF; } // Firefox p::-moz-selection { background: red; color: #FFF; }
The difference between pseudo-class and pseudo-element:First, read
w3c for both Definition: 1.CSS pseudo-classes are used to add special effects to certain selectors. 2.CSS pseudo-elements are used to add special effects to certain selectors.
Two points can be made clear. The first is that both are related to the selector, and the second is to add some "special" effects. The special thing here is that the two describe things that other css cannot describe.
Pseudo-class type
Pseudo-element type
Difference
:first- child
and pseudo-element:first-letter to compare. p>i:first-child {color: red}
<p>
<i>first</i>
<i>second</i>
</p>
If we do not use pseudo-classes and want to achieve the above effect , you can do this:
.first-child {color: red} <p> <i class="first-child">first</i> <i>second</i> </p>
That is, we add a class to the first child element, and then define the style of this class. Then let's look at adding styles to the first letter for the element:
p:first-letter {color: red} <p>I am stephen lee.</p>
So if we don't use Pseudo elements, what should be done to achieve the above effect?
.first-letter {color: red} <p><span class='first-letter'>I</span> am stephen lee.</p>
That is, we add a span to the first letter, and then add a style to the span.
The effect of pseudo-class can be achieved by adding an actual class, while the effect of pseudo-element needs to be achieved by adding an actual element. This is why one of them is called pseudo-class and the other is That's why it's called a pseudo-element.
总结
伪元素和伪类之所以这么容易混淆,是因为他们的效果类似而且写法相仿,但实际上 css3 为了区分两者,已经明确规定了伪类用一个冒号来表示,而伪元素则用两个冒号来表示。
:Pseudo-classes ::Pseudo-elements
但因为兼容性的问题,所以现在大部分还是统一的单冒号,但是抛开兼容性的问题,我们在书写时应该尽可能养成好习惯,区分两者。
The above is the detailed content of Pseudo-classes and pseudo-elements in CSS and the differences between them. 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

AI Hentai Generator
Generate AI Hentai for free.

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



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.

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-

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.

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 use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

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.
