当前位置:网站首页>Pseudo class elements -- before and after
Pseudo class elements -- before and after
2022-07-05 10:20:00 【Pursue ~】
List of articles
before/after A pseudo class is equivalent to inserting two additional tags inside an element , Its most suitable and recommended application is graphics generation . In some exquisite UI Implementation , Can be simplified HTML Code , Improve readability and maintainability . Effect use :


Small icons like this are mostly used before,after To achieve , Not only simple but also convenient .
1. Basic usage
:before and :after The function of is to specify the element content ( Not the elements themselves ) Insert a containing before or after content Attribute specifies the inline element of the content , The basic usage is as follows :
#example:before {
content: "#";
color: red;
}
#example:after {
content: "$";
color: red;
}
Both of these pseudo classes belong to inline elements , But with display:block; Attribute can convert it into a block element , The more common usage is some implementation of styles , There is also the effect of clearing floating ..
2. Style modification
The code is as follows :
<div class="quote">
<span> Fight tiger </span>
</div>
.quote:before,.quote:after{// Use these two pseudo classes to realize style rendering
content:"";
display:inline-block;
width:5%;
margin:5px 1%;
border-bottom:1px solid blue;
}
3. Remove the floating
The code is as follows :
<div class="parent">
<div class="son1"></div>
<div class="son2"></div>
</div>
<div class="parent2">parent2</div>
//css Code
.son1{
width:300px;
height:200px;
background-color: lightgray;
float:left;
}
.son2{
width:300px;
height:100px;
background-color: lightblue;
float:left;
}
.parent2{
width:400px;
height: 400px;
background-color:blue;
color:#fff;
text-align:center;
line-height:400px;
font-size:30px;
}
If you add this code to the above code to clear the float, you will achieve a different effect :
.parent:after{
content:"";
display:block;// Set as block element
clear:both; // Use this property to clear the float
}
::before and ::after Endemic content, Used in css Add content to the logical head or tail of an element in rendering .
These additions will not appear in DOM in , Will not change the content of the document , Do not copy , Just in css Render layer added .
So don't use :before or :after Show meaningful content , Try to use them to show decorative content , For example, icons .
Be careful : In the use of before and after when content essential .
Be careful : In the use of before and after when content essential .
Be careful : In the use of before and after when content essential .
5.content attribute
::before and ::after Must cooperate content Attribute to use ,content Used to define what to insert ,content It must be worth , At least it's empty . By default , Of pseudo class elements display Is the default value inline, Can be set by display:block To change the display .
content Take the following values .
1、string
Wrap a string in quotation marks , A string will be added to the element content . Such as :a:after{content:""}
<!DOCTYPE html>
<meta charset="utf-8" />
<style type="text/css">
p::before{
content: "《";
color: blue;
}
p::after{
content: "》";
color: blue;
}
</style>
<p> Ordinary world </p>
2、attr()
adopt attr() Call the properties of the current element , For example, the picture alt Prompt text or link href The address is displayed .
<style type="text/css">
a::after{
content: "(" attr(href) ")";
}
</style>
<a href="http://www.cnblogs.com/starof">starof</a>
3、url()/uri()
Used to reference media files .
give an example :“ Baidu ” Here is a picture , It's given later href attribute .
<style>
a::before{
content: url("https://www.baidu.com/img/baidu_jgylogo3.gif");
}
a::after{
content:"("attr(href)")";
}
a{
text-decoration: none;
}
</style>
---------------------------
<body>
<a href="http://www.baidu.com"> Baidu </a>
</body>
4、counter()
Call counter , You can implement the serial number function without using list elements .
coordination counter-increment and counter-reset Attributes use :
h2:before { counter-increment: chapter; content: "Chapter " counter(chapter) ". " }
<style>
body{
counter-reset: section;
}
h1{
counter-reset: subsection;
}
h1:before{
counter-increment:section;
content:counter(section) "、";
}
h2:before{
counter-increment:subsection;
content: counter(section) "." counter(subsection) "、";
}
</style>
------------------------------------------------
<body>
<h1>HTML tutorials</h1>
<h2>HTML Tutorial</h2>
<h2>XHTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h1>Scripting tutorials</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>
<h1>XML tutorials</h1>
<h2>XML</h2>
<h2>XSL</h2>
</body>
边栏推荐
- Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set
- Swift saves an array of class objects with userdefaults and nssecurecoding
- What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
- AtCoder Beginner Contest 258「ABCDEFG」
- 微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
- Swift tableview style (I) system basic
- DDOS攻击原理,被ddos攻击的现象
- A high density 256 channel electrode cap for dry EEG
- 【JS】数组降维
- NCP1342芯片替代料PN8213 65W氮化镓充电器方案
猜你喜欢

学习笔记6--卫星定位技术(上)

Is it really reliable for AI to make complex decisions for enterprises? Participate in the live broadcast, Dr. Stanford to share his choice | qubit · viewpoint

A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo

@Serializedname annotation use

isEmpty 和 isBlank 的用法区别

Workmanager Learning one
![[tips] get the x-axis and y-axis values of cdfplot function in MATLAB](/img/08/2d039df6ea3ace8685512b2af8281d.png)
[tips] get the x-axis and y-axis values of cdfplot function in MATLAB

Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new

Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)

What is the most suitable book for programmers to engage in open source?
随机推荐
各位大佬,我测试起了3条线程同时往3个mysql表中写入,每条线程分别写入100000条数据,用了f
vscode的快捷键
C function returns multiple value methods
Implementation of smart home project
程序员搞开源,读什么书最合适?
Glide Mastery
QT VT100 parser
WorkManager學習一
Events and bubbles in the applet of "wechat applet - Basics"
Glide conclusion
报错:Module not found: Error: Can‘t resolve ‘XXX‘ in ‘XXXX‘
到底谁才是“良心”国产品牌?
如何写出高质量的代码?
Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
Glide advanced level
Swift saves an array of class objects with userdefaults and nssecurecoding
DDOS攻击原理,被ddos攻击的现象
Jupiter notebook shortcut key
自动化规范检查软件如何发展而来?
Coffeescript Chinese character to pinyin code