Core points
: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. :first-line
, :first-letter
, :selection
, :before
, :after
and 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:
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
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
::before
::after
How to use 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.
Does all browsers support pseudo-elements?
What is the difference between pseudo-elements and pseudo-classes?
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. <🎜>::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; }
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!