Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the pseudo elements of CSS

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the pseudo elements of CSS". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the pseudo elements of CSS".

What are pseudo elements?

Pseudo-elements and pseudo-classes are two concepts that are easy to be confused by beginners of CSS.

Pseudo element: a keyword attached to the end of the selector that allows you to modify the style for a specific part of the selected element. For example: before,::after,::first-letter and so on. It is to create an "element" content that does not exist and attach styles.

Pseudo class: no element content is created, but existing elements in certain states are selected and styles are attached. For example: first-child,:active,:focus and so on.

Pseudo-elements and pseudo-classes are so easily confused because they are similar in effect and written in the same way. But in fact, in order to distinguish between the two, CSS3 has clearly specified that pseudo-classes are represented by a colon, while pseudo-elements are represented by two colons.

: Pseudo-classes / / pseudo class:: Pseudo-elements / / pseudo element

However, because of compatibility problems, most of them are still unified single colons. However, we should try our best to form good habits and distinguish between the two when writing.

Let's introduce these five pseudo elements one by one.

1::first-letter

Style the first character of the first line of a block-level element, and there is nothing else in front of this character (such as a picture or an inline table), so it only works for block-level elements. This pseudo element is often used in blog posts, such as:

:: first-letter

It is easier to implement it with pseudo-classes:

P {width: 500px; line-height: 1.5;} h3 + p::first-letter {color: white; background-color: black; border-radius: 2px; box-shadow: 3px 3px 0 red; font-size: 250%; padding: 6px 3px; margin-right: 6px; float: left;}

It would be troublesome to think about it if you don't use this pseudo class to achieve the same effect.

2::first-line

Similar to:: first-letter, it is used to style the first line of text or block-level elements.

: first-line {color: blue; text-transform: uppercase; / * WARNING: these styles don't work * / / * many attributes are invalid in:: first-line pseudo elements * / margin-left: 20px; text-indent: 20px;}

:: first-line {

3::selection

Sets the style when the element is highlighted by the user. There are only three style attributes that are valid for this pseudo element:

Color

Background properties (background-color, background-image, etc.)

Text-shadow

:: selection

4::backdrop

This pseudo-element is a bit fresh. It is a box the same size as viewport and acts as a background when the page is in full-screen mode. For example, use it to set the background color of full-screen video:

Video::backdrop {background-color: # 448;}

:: backdrop

5::placeholder

Or the input prompt text, the default is gray, you can customize the text style.

Input::placeholder {color: red; font-size: 1.2em; font-style: italic;}

:: placeholder

Thank you for your reading, these are the contents of "what are the pseudo elements of CSS?" after the study of this article, I believe you have a deeper understanding of what the pseudo elements of CSS have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report