Home Web Front-end CSS Tutorial An introduction to pseudo-elements in CSS and their differences from pseudo-classes

An introduction to pseudo-elements in CSS and their differences from pseudo-classes

Mar 08, 2017 pm 02:30 PM

Pseudo-elements

We know that with the further improvement of CSS specifications, more and more new CSS pseudo-elements have been added, but in daily development, we commonly use The browser support situation that is more optimistic is before and after. But what we use in daily development are: after {content: ”;} to clear floats and add an element (take care of the use of a single colon in IE8 browser). But what are the possible values ​​​​of content?
1. String: content: “a string” - Note: Special characters must be encoded in unicode;
2. Picture: content: url(/path/to/benjamin.png) – The picture is inserted in its original size, Cannot be resized. Because the image supports gradients, you can use gradient effects on pseudo elements;
3. No characters: content: "" - This is more useful for clearing floats and setting background images. We can set the width and width of the background image. height, we can even use the background-size attribute to adjust the size of the background image;
4. Counter: content: counter(li)- Before :marker appears, it is more useful for setting the list serial number style; see the following code for details:

ol {   
    countercounter-reset: li;   
}   
ol>li {   
    position: relative;   
    padding-left: 2em;   
    line-height: 30px;   
    list-style: none;   
}   
ol>li:before {   
    position: absolute;   
    top: 8px;   
    left: 0;   
    height: 16px;   
    width: 16px;   
    line-height: 16px;   
    text-align: center;   
    content: counter(li);   
    countercounter-increment: li;   
    border-radius: 50%;   
    background-color: #ccc;   
    font-size: 12px;   
    color: #efefee;   
}
Copy after login

PS: We cannot set content: "

Benjamin

", it will not be parsed as HTML code snippets, but will be parsed as a string;
5. content: attr(attrName)
content can use the attr function to obtain the attribute value, which is especially convenient when used in pseudo classes. See the following code:

<style type="text/css">   
    .list li {   
        list-style: none;   
        margin-bottom: 20px;   
    }   
    .list li span {   
        vertical-align: middle;   
    }   
    .list li:before {   
        content: attr(data-index);   
        display: inline-block;   
        width: 20px;   
        height: 20px;   
        text-align: center;   
        color: #fff;   
        vertical-align: middle;           
        background-color: #f00;   
        border-radius: 50%;   
    }   
</style>   
<ul class="list">   
    <li data-index="1"><span>专注前端开发和用户体验</span></li>   
    <li data-index="2"><span>专注前端开发和用户体验</span></li>   
    <li data-index="3"><span>专注前端开发和用户体验</span></li>   
    <li data-index="4"><span>专注前端开发和用户体验</span></li>   
    <li data-index="5"><span>专注前端开发和用户体验</span></li>   
</ul>
Copy after login

Having said the previous words, let’s talk about the bugs encountered in IE:
Bug description: When using pseudo classes to implement "+"/"-" image switching, it is achieved by adding and removing the opened class, but in IE8 The effect is weird and cannot be rendered correctly. It is normal in other browsers:

.plus {   
    position: relative;   
    display: inline-block;   
    vertical-align: top;   
    width: 20px;   
    height: 20px;   
    margin-right: 24px;   
    border: 1px solid #fdaa47;   
    border-radius: 3px;   
    overflow: hidden;   
}   
/* 横向 */
.plus:before {   
    content: &#39;&#39;;   
    position: absolute;   
    top: 10px;   
    left: 3px;   
    width: 14px;   
    height: 1px;   
    background-color: #fdaa47;   
    display: block;   
}   
/* 纵向 */
.plus:after {   
    display: block;   
    content: &#39;&#39;;   
    width: 1px;   
    height: 14px;   
    background-color: #fdaa47;   
    position: absolute;   
    left: 10px;   
    top: 3px;   
}   
.opened:after {   
    top: -30px;   
}
Copy after login

When using addClass('opened') and removeClass('opened'), switch between addition and subtraction Time: The effect in IE8 browser is not as expected, and some styles cannot be covered. The current solution is as follows:

<p class="parent">   
    <i class="plus"></i>   
</p>   
<script type="text/javascript">   
$(&#39;.parent&#39;).on(&#39;click&#39;, function() {   
    var $i = $(this).find(&#39;i&#39;),   
        className = $i.attr(&#39;class&#39;);   
    className = /opened/.test(className) ? &#39;plus&#39; : className +&#39; opened&#39;;   
    $i.replaceWith(&#39;<i class="&#39;+ className +&#39;""></i>&#39;);   
});   
</script>
Copy after login

pseudo-class and Similarities and differences of pseudo-elements
1. W3C CSS 2.1 Selectors
There is no distinction between pseudo-classes and pseudo-elements. They all use a colon
For example,
pseudo-class: first-child,
Pseudo-element: first-line
PS: The specification clearly mentions the writing order of several pseudo-classes of a link:
Note that the A:hover must be placed after the A:link and A :visited rules, since otherwise the cascading rules will hide the 'color' property of the A:hover rule. Similarly, because A:active is placed after A:hover, the active color (lime) will apply when the user both activates and hovers over the A element.

2. CSS Selectors Level 3
This specification makes a distinction between pseudo-classes and pseudo-elements. Pseudo-classes use a single colon. Elements start with a double colon.
For example
Pseudo class: first-child
Pseudo elements::first-line, ::first-letter, ::before, ::after
CSS 3 is added on the basis of CSS2.1 There are many pseudo-classes: target, UI element status pseudo-classes: checked, etc., structural pseudo-classes: nth-child(), etc. For details, please refer to the specification.

3. CSS Selectors Level 4 draft
This draft adds many new pseudo-classes, such as those related to input control status, value status, and value verification. Pseudo-classes, tree-structured pseudo-classes, grid-structured pseudo-classes, etc.

4. CSS Pseudo-Elements Module Level 4——W3C First Public Working Draft, 15 January 2015
Added some pseudo-elements, such as:
Selecting Highlighted Content: the ::selection, ::spelling-error, and ::grammar-error pseudo-elements,
Placeholder Input: the ::placeholder pseudo-element.

5. Common applications
Pseudo-class:
1) a link style
2) Interlaced color change
Pseudo-element:
1 ) The most common use of pseudo-element after is to clear floats,
.fix{*zoom:1;}
.fix:after,.fix::after{display: block; content: “clear”; height: 0 ; clear: both; overflow: hidden; visibility: hidden;}
2) letter-spacing+first-letter to hide button text
3) First line and first letter style

The above is the detailed content of An introduction to pseudo-elements in CSS and their differences from pseudo-classes. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Demystifying Screen Readers: Accessible Forms & Best Practices Demystifying Screen Readers: Accessible Forms & Best Practices Mar 08, 2025 am 09:45 AM

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

Adding Box Shadows to WordPress Blocks and Elements Adding Box Shadows to WordPress Blocks and Elements Mar 09, 2025 pm 12:53 PM

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let&#039;s look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

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

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

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

Classy and Cool Custom CSS Scrollbars: A Showcase Classy and Cool Custom CSS Scrollbars: A Showcase Mar 10, 2025 am 11:37 AM

In this article we will be diving into the world of scrollbars. I know, it doesn’t sound too glamorous, but trust me, a well-designed page goes hand-in-hand

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

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

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

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

What the Heck Are npm Commands? What the Heck Are npm Commands? Mar 15, 2025 am 11:36 AM

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.

See all articles