The usage of:: before:: after in css

Keywords: Attribute css3

I. Introduction

: before and:: after are pseudo elements. (in CSS3, in order to distinguish them from pseudo classes, pseudo elements are written with double colons. However, due to compatibility issues, most of them are now unified single colons, such as: first line,: first letter,: before,: after, etc., but the new pseudo elements introduced in CSS3 are not allowed to use single colons.)

The ": before" pseudo element can insert new content before the content of the element.
The ": after" pseudo element can insert new content after the content of the element.

: before or:: after must be used in combination with the content attribute. The content cannot be absent, and the content must be at least empty. The display of the pseudo element defaults to inline, which can be modified by itself

Two, usage

1. Use the value of content:
(1) content: "string";

You can add some characters before or after the words

p:after{~~Remove line formatting~~ 
	<p>Please enter a name</p>
	<p>Please enter age</p>
	<p>Please enter gender</p>


You can add some small icons in front of the words, and adjust the position by positioning

			position: relative;

(3) Call the attribute of the element through the attr() attribute, such as displaying the alt prompt text of the picture or the link's href address.

The attr() function returns the attribute value of the selected element.

			content:"(" attr(href) ")";
<p><a href="">WeChat</a></p>
<p class="hello"></p>

2. Realize triangle

In the same way, we can get hexagon: set the box itself as a regular triangle, set the box:: after as an inverted triangle, and realize hexagon by positioning and overlapping together

			border:20px solid transparent;
			border-bottom:20px solid pink;
			border:20px solid transparent;
			border-left:20px solid pink;
			border:20px solid transparent;
			border-top:20px solid pink;
			border:20px solid transparent;
			border-right:20px solid pink;

	<div id="top-triangle"></div>
	<div id="bottom-triangle"></div>
	<div id="right-triangle"></div>
	<div id="left-triangle"></div>

3. Achieve dialog effect:

			position: relative;    /*Move back to the box position*/
			display: table;
			min-height: 40px;
			text-align: center;
			background-color: #9EEA6A;
			margin: 0;
			border-radius: 7px;
			position: absolute;
			display: inline-block;
			content: "";
			width: 0px;
			height: 0px;
			border: 8px solid transparent;
			top: 15px;		/*Move to the middle*/
			border-right-color: #9EEA6A;
			left: -16px;
			border-left-color: #9EEA6A;
			right: -16px;
		.left p,.right p{
			padding:0px 10px;

    <div class="left">
        <p>How do you do</p>
    <div class="right">
        <p>Long time no see.~</p>

4. Implementation arrow

			position: relative;
			width: 200px;
			height: 50px;
			background-color: pink;
			position: absolute;
			width: 12px;
			height: 12px;
			border: 1px solid black;
			border-bottom-color: transparent;
			border-right-color: transparent;
			transform: translate(-50%, -50%) rotate(-45deg);
			left: 20px;
			top: 50%;
	<div class="box"></div>

5. Clear float
Principle: use: after and: before to insert two element blocks inside the element, so as to achieve the effect of clearing floating.

.outer:after { 
		clear:both; 	/*Clear floating*/
		display:block; /*Show pseudo elements*/
		height:0;		/*No occupying position*/
		visibility:hidden; 	/*Allow the browser to render it, but do not show it*/

6. Implement some label support for placeholder

			border:1px solid gray;
	<input class="input" placeholder="Please enter text"></div>

(this article refers to a lot of blogs, which are not completely original)

Published 8 original articles, won praise 0, visited 112
Private letter follow

Posted by tonga on Sun, 15 Mar 2020 04:30:45 -0700