AtoZ CSS Screencast: CSS Pseudo Elements
Core points
- CSS pseudo-elements are elements on the page that are not present in HTML code, and they can be operated using any CSS style applied to other elements. The
- and
:before
pseudo-elements in:after
CSS can be used to generate page content, including text, images, attribute values, and counters. They can also be used to create complex shapes with minimal marking. - The various pseudo-elements in CSS include
:first-line
,:first-letter
,:selection
,:before
,:after
and . These can be used to style specific parts of an element, insert content before and after the content of an element, and style the first letter or first line of a block-level element, etc.
Video Explanation
(Loading in the player...) This video is part of the AtoZ CSS series. You can find other content in the series here.Text record
The CSS pseudo-element is an element on the page that does not appear in HTML code.
They can be operated with any CSS style applied to any other element.
:before
Two special pseudo-elements—:after
and
In this section, we will learn:
- Five different pseudo-elements
- Generate text, image attribute values and counters from CSS
- How to create complex shapes with minimal markup
Pseudo-element
There are five pseudo-elements in CSS:
-
:first-line
-
:first-letter
-
:selection
-
:before
-
:after
These are distinguished from pseudo-classes by double colons, but for simplicity, they are usually written using single colons in CSS.
:first-line
Here is a long reference to the placeholder text block. I can change the color of the first line of the text using :first-letter
, which applies even after the text is reformed. I can create an initial caps effect by setting the style of :selection
with floating and larger font sizes. I can also change the color of the selected text using
:before
I can use the :after
and content
pseudo-elements to add large quotes before and after the block reference. Text is generated from the
blockquote { position: relative; border-left: 5px solid #66d9ef; padding: 1em 1em 1em 2em; } blockquote p:first-line { color: #cc3f85; } blockquote p:first-letter { float: left; font-size: 4em; margin-right: 0.5em; } ::selection { background: #cc3f85; color: #fff; } blockquote:before { content: "“"; position: absolute; top: 0; left: -0.6em; font-size: 8em; font-family: Georgia; } blockquote:after { content: "”"; bottom: -0.25em; right: -0.5em; line-height: 1rem; } blockquote:before, blockquote:after { position: absolute; color: #66d9ef; font-size: 8em; font-family: Georgia; }
Create content
:before
Using :after
and
We have seen how to add text content to the page, but we can also add images, attribute values, counters, or empty strings, just access these two extra elements. <🎜>
Adding an image is similar to adding a background image using url()
. Here, use url()
as the value of the content
attribute. I actually prefer to use background images and access the pseudo-element by creating an empty string for content
. This provides more control over the image, as all commonly used properties such as background-position
, background-repeat
and background-size
are available.
blockquote { position: relative; border-left: 5px solid #66d9ef; padding: 1em 1em 1em 2em; } blockquote p:first-line { color: #cc3f85; } blockquote p:first-letter { float: left; font-size: 4em; margin-right: 0.5em; } ::selection { background: #cc3f85; color: #fff; } blockquote:before { content: "“"; position: absolute; top: 0; left: -0.6em; font-size: 8em; font-family: Georgia; } blockquote:after { content: "”"; bottom: -0.25em; right: -0.5em; line-height: 1rem; } blockquote:before, blockquote:after { position: absolute; color: #66d9ef; font-size: 8em; font-family: Georgia; }
You can also use the content
attribute to inject the value of the HTML attribute into the page. When creating a print stylesheet, I like to add the following code snippet to output the link's URL so that they can be read from the page:
li:before { content: url(star.png); display: inline-block; vertical-align: middle; margin-right: 0.5em; }
This will add links after the link text for any link that is not an internal link or hash link.
The last special case of generating content is the value of the insertion counter variable. I've found this to be very useful in numbering complex lists of legal terms and conditions.
Here is a series of titles, and below is a series of nested lists. I want each chapter title to have a number and each list item is a child number for each chapter.
For each h2
, I will increment a "section" counter; for each list item, I will increment a "item" counter. Before each chapter title, I will output the value of the chapter counter; before each list item, I will output the value of the item counter. Additional strings can be added between counters to create complex numbering systems. The simplified form of this method can be used to control the style of numbers or bullets in a list.
a[href]:not([href*="#"]):after { content: attr(href); }
Shape
As each element on the page can have two "extra" elements and can be styled according to our preferences, various complex shapes can be created.
While thinking about the demo example, I saw a reference to the shape on CSS-Tricks; one of them stands out and I will step by step introduce how it works. Let's make yin and yang symbols with a single element.
h2 {counter-increment: section;} ul {counter-reset: item;} li {counter-increment: item;} h2:before { content: counter(section) " - "; } li:before { content: counter(section) "." counter(item); }
Starting with the box, you can use border-radius
to convert it into a circle. Two colored semicircles can be created using border-bottom
which is equal to the height of the circle. Two points are created by creating two circles with pseudo-elements and placing them with position:absolute
. Using borders that match the semicircle color, you can create two circular endpoints of the symbol. If you ask me, it's cool.
I like using pseudo-elements; you can do a lot with them and add various visuals to the page without messing up the markup.
Frequently Asked Questions about CSS Pseudo-Elements
What are the different types of CSS pseudo-elements?
CSS pseudo-element is used to style specific parts of an element. There are several types of pseudo-elements, including ::before
, ::after
, ::first-letter
, ::first-line
, ::selection
, ::backdrop
, ::placeholder
, ::before
and ::after
. Each pseudo-element is targeted at a different part of the element. For example, ::first-letter
and ::first-line
are used to insert content before and after element content, while
are used to style the first letter or first line of a block-level element. ::before
::after
How to use
and pseudo-elements? ::before
::after
content
and
blockquote { position: relative; border-left: 5px solid #66d9ef; padding: 1em 1em 1em 2em; } blockquote p:first-line { color: #cc3f85; } blockquote p:first-letter { float: left; font-size: 4em; margin-right: 0.5em; } ::selection { background: #cc3f85; color: #fff; } blockquote:before { content: "“"; position: absolute; top: 0; left: -0.6em; font-size: 8em; font-family: Georgia; } blockquote:after { content: "”"; bottom: -0.25em; right: -0.5em; line-height: 1rem; } blockquote:before, blockquote:after { position: absolute; color: #66d9ef; font-size: 8em; font-family: Georgia; }
Can I use pseudo-elements to style the form input? ::placeholder
li:before { content: url(star.png); display: inline-block; vertical-align: middle; margin-right: 0.5em; }
How to style the first letter of a paragraph using pseudo-element? ::first-letter
a[href]:not([href*="#"]):after { content: attr(href); }
Can I use multiple pseudo-elements on the same element? ::before
::after
Yes, you can use multiple pseudo-elements on the same element. For example, you can use ::before
and ::after
on the same element to insert content before and after its content. However, it is important to remember that the order of pseudo-elements is.
pseudo-element.
Does all browsers support pseudo-elements?
Most modern browsers support pseudo-elements. However, older versions of some browsers may not support all pseudo-elements. Before using pseudo-elements, it is best to check browser compatibility.
What is the difference between pseudo-elements and pseudo-classes?
Both pseudo-elements and pseudo-classes are used to apply styles to elements based on certain conditions. However, they are used for different purposes. A pseudo-class is used to set its style when an element is in a specific state, such as when the mouse is hovering over it or gaining focus. On the other hand, pseudo-elements are used to style specific parts of an element.
Can I use pseudo-elements with JavaScript?
<🎜>Pseudo-elements are not part of the DOM, so they cannot be accessed or manipulated directly using JavaScript. However, you can change the style applied to a pseudo-element by changing the style of the parent element using JavaScript. <🎜>How to use ::selection
pseudo-element?
::selection
Pseudo-element is used to change the appearance of the user-selected text. For example, you can change the background color and text color of the selected text. Here is an example:
blockquote { position: relative; border-left: 5px solid #66d9ef; padding: 1em 1em 1em 2em; } blockquote p:first-line { color: #cc3f85; } blockquote p:first-letter { float: left; font-size: 4em; margin-right: 0.5em; } ::selection { background: #cc3f85; color: #fff; } blockquote:before { content: "“"; position: absolute; top: 0; left: -0.6em; font-size: 8em; font-family: Georgia; } blockquote:after { content: "”"; bottom: -0.25em; right: -0.5em; line-height: 1rem; } blockquote:before, blockquote:after { position: absolute; color: #66d9ef; font-size: 8em; font-family: Georgia; }
Can I animate pseudo-elements?
Yes, you can animate pseudo-elements using CSS animations or transitions. However, remember that not all properties can be animate. For example, you can animate the opacity or transformation of a pseudo-element, but you cannot animate the content
attribute.
The above is the detailed content of AtoZ CSS Screencast: CSS Pseudo Elements. 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



If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

The article discusses using CSS for text effects like shadows and gradients, optimizing them for performance, and enhancing user experience. It also lists resources for beginners.(159 characters)

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:
