Deep understanding of the use of ::before/:before and ::after/:after

Deep understanding of the use of ::before/:before and ::after/:after

Part 1: Basics

1. Unlike pseudo-classes such as :active and :hover, they are all pseudo-elements.

2. The :before/:after pseudo-elements were proposed in CSS2. The ::before/::after is the way of writing in CSS3. It is proposed again to use two colons to represent pseudo-elements to distinguish pseudo-classes.

3. They are used after a selector in CSS to add decorative content, because this can achieve semantics. If HTML is used to add some nodes without actual content or auxiliary sample text, they are meaningless.

4. They have a unique attribute content, in which the added content is an inline element by default.

5. The created pseudo-element is above the element it is attached to by default. We can use z-index:-1; to put it below.

6. They are virtual nodes, not real nodes. like:

 div::after{
            content: " ";
            border:thin solid red;
        }

We can see in the browser:

::after is not a real node, but we often see it used on some websites.

6. Elements such as input, img, iframe, etc. cannot contain other elements, so content cannot be inserted through pseudo-elements.

Part II: Application

1. Use as a separator.

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>pseudo-element</title>
    <style>
        a{
            color:blue;
            text-decoration: none;
        }
        .log:after{
            content:"|";
            color:red;
        }
    </style>
</head>
<body>
    <a href="" class="log">Login</a><a href="">Register</a>
</body>
</html>

The effect is as follows:

2. Make a triangle

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>pseudo-element</title>
    <style>
        a{
            color:blue;
            text-decoration: none;
        }
        .log:before{
            content:" ";
            display: inline-block;
            width: 0;
            height: 0;
            border:10px solid transparent;
            border-left: 10px solid red;
        }
    </style>
</head>
<body>
    <a href="" class="log">Login</a>
</body>
</html>

The effect is as follows:

3. Clear floating (the following content is taken from Zhang Xinxu)

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>pseudo-element</title>
    <style>
    .box{padding:10px; background:gray;}
    .fix{*zoom:1;}
    .fix:after{display:block; content:"clear"; height:0; clear:both; overflow:hidden; visibility:hidden;}
    .l{float:left;}
    </style>
</head>
<body>
    <div class="box fix">
    <img class="l" src="http://image.zhangxinxu.com/image/study/s/s256/mm1.jpg" />
</div>
</body>
</html>

The effect is as follows:

Note: *zoom:1; is used to clear floats in IE6 (used on the parent element of the float element).

Summarize

The above is the full content of this article. I hope that the content of this article can be of some help to your study or work. If you have any questions, you can leave a message to communicate.

<<:  Introduction to JWT Verification Using Nginx and Lua

>>:  HTML form component example code

Recommend

MySQL paging performance exploration

Several common paging methods: 1. Escalator metho...

Detailed steps for installing and configuring MySQL 8.0 on CentOS 7.4 64-bit

Step 1: Get the MySQL YUM source Go to the MySQL ...

JS implements the dragging and placeholder functions of elements

This blog post is about a difficulty encountered ...

Detailed explanation of several methods of installing software in Linux

1. RPM package installation steps: 1. Find the co...

Summary of MySQL's commonly used concatenation statements

Preface: In MySQL, the CONCAT() function is used ...

HTML4.0 element default style arrangement

Copy code The code is as follows: html, address, ...

Detailed steps for configuring virtual hosts in nginx

Virtual hosts use special software and hardware t...

Simply learn various SQL joins

The SQL JOIN clause is used to join rows from two...

Vue implements simple data two-way binding

This article example shares the specific code of ...

Implementation steps for docker-compose to deploy etcd cluster

Table of contents Write docker-compose.yml Run do...

Linux IO multiplexing epoll network programming

Preface This chapter uses basic Linux functions a...

Installation and use of mysql mycat middleware

1. What is mycat A completely open source large d...

Share 5 JS high-order functions

Table of contents 1. Introduction 2. Recursion 3....

Web Design Tips: Simple Rules for Page Layout

Repetition: Repeat certain page design styles thr...