before和after的操作

before和after,前者是在元素之前插入东西,后者是在元素后面插入东西,但插入的东西不仅仅只是文字而已,还有图标,以及计算器的操作。

由于两者的操作基本一样,这里以before为例

插入文字

<!doctype html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <style type="text/css">
        	p:nth-child(1)::before{
        		content: '插入文字'
        	}
        	p:nth-child(2)::before{
        		content: '插入❤'
        	}
        	p:nth-child(3)::before{
        		/*改变插入文字的颜色和背景*/
        		content: '插入文字';
        		background: skyblue;
        		color: #fff;

        	}
        </style>
    </head>
    <body>
    	<p>段落1</p>
    	<p>段落2</p>
    	<p>段落3</p>
    </body>
</html>

插入图片

<!doctype html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <style type="text/css">
        	p::before{
        		content: url('./菜单.svg'); /*图片地址*/
        	}
        </style>
    </head>
    <body>
    	<p>段落1</p>
    </body>
</html>

计数器

纯数字

<!doctype html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <style type="text/css">
        	/*
				这里的add,可以是任意变量,但标签的counter-increment属性值要和上面的一样。
        	*/
        	h2::before{
        		content: counter(add);
        	}
        	h2{
        		counter-increment: add;
        	}
        </style>
    </head>
    <body>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    </body>
</html>

拼接文本

计数器也可以拼接文本,不过不用什么加号之类

<!doctype html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <style type="text/css">
        	h2::before{
        		content: '第'counter(add)'章';
        	}
        	h2{
        		counter-increment: add;
        	}
        </style>
    </head>
    <body>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    </body>
</html>

指定项目编号

content: counter(计数器名, 编号种类)

upper-alpha 大写字母

upper-roman 大写罗马字母

<!doctype html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <style type="text/css">
        	h2::before{
        		content: counter(add, upper-roman)'. ';
        	}
        	h2{
        		counter-increment: add;
        	}
        </style>
    </head>
    <body>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    	<h2>标题</h2>
    </body>
</html>

编号嵌套

<!doctype html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <style type="text/css">
        	h2::before{
        		content: counter(add)'. ';
        	}
        	h2{
        		counter-increment: add;
        		counter-reset: addd; /*让子标题重新编号*/
        	}
        	h3::before{
        		content: counter(addd)'. ';
        	}
        	h3{
        		counter-increment: addd;
        	}
        </style>
    </head>
    <body>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    </body>
</html>

在编号与编号加横线

<!doctype html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <style type="text/css">
        	h2::before{
        		content: counter(add)'. ';
        	}
        	h2{
        		counter-increment: add;
        		counter-reset: addd; /*让子标题重新编号*/
        	}
        	h3::before{
        		content: counter(add) '-' counter(addd)'. ';
        	}
        	h3{
        		counter-increment: addd;
        		padding-left: 40px;
        	}
        </style>
    </head>
    <body>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    	<h3>子标题</h3>
    </body>
</html>
<!doctype html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <style type="text/css">
        	h2::before{
        		content: counter(add)'. ';
        	}
        	h2{
        		counter-increment: add;
        		counter-reset: addd; /*让子标题重新编号*/
        	}
        	h3::before{
        		content: counter(add) '-' counter(addd)'. ';
        	}
        	h3{
        		counter-increment: addd;
        		counter-reset: adddd;
        		padding-left: 40px;
        	}
        	h4::before{
        		content: counter(add) '-' counter(addd) '-' counter(adddd) '. ';
        	}
        	h4{
        		counter-increment: adddd;
        		padding-left: 40px;
        	}
        </style>
    </head>
    <body>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h4>子标题的子标题</h4>
    	<h4>子标题的子标题</h4>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h4>子标题的子标题</h4>
    	<h4>子标题的子标题</h4>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h4>子标题的子标题</h4>
    	<h4>子标题的子标题</h4>
    	<h2>标题</h2>
    	<h3>子标题</h3>
    	<h4>子标题的子标题</h4>
    	<h4>子标题的子标题</h4>
    </body>
</html>

在字符串两边添加嵌套文字符号

首先content有两个属性值,open-quoteclose-quote,可以在文字两边添加如括号,单引号,双引号之类的文字符号。

open-quote是开始的符号,close-quote是结束符号,元素的样子中使用quotes属性来决定要什么符号。

不使用quotes属性时,默认是使用双引号

<!doctype html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <style type="text/css">

        	h2::before{
        		content: open-quote;
        	}
        	h2::after{
        		content: close-quote;
        	}
        	
        	
        </style>
    </head>
    <body>
    	<h2>标题2</h2>
    	
    </body>
</html>

使用别的符号

<!doctype html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <style type="text/css">

        	h2::before, h3::before{
        		content: open-quote;
        	}
        	h2::after, h3::after{
        		content: close-quote;
        	}
        	h2{
        		quotes: "("")"
        	}
        	h3{
        		/*转义字符*/
        		quotes: "\""\"
        	}
        	
        </style>
    </head>
    <body>
    	<h2>标题2</h2>
    	<h3>标题3</h3>
    	
    </body>
</html>
原文地址:https://www.cnblogs.com/tourey-fatty/p/12066912.html