css3 - 使用选择器在页面中插入内容
2017-04-28 08:56
471 查看
1.使用选择器来插入内容
当插入的内容为文字时,必须要在插入文字的两旁加上单引号或者双引号。html:
<h2>我是一个标题</h2>
css:
<style> h2::before { content: 'COLUMN'; color: #fff; background-color: orange; font-family: 'Comic Sans Ms',Helvetica,sans-serif; padding: 1px 5px; margin-right: 10px; } </style>
2.指定个别元素不进行插入
html:
<body> <h2>我是一个标题</h2> <h2 class="sample">我是标题2</h2> </body>
css:
<style> h2::before { content: 'COLUMN'; color: #fff; background-color: orange; font-family: 'Comic Sans Ms',Helvetica,sans-serif; padding: 1px 5px; margin-right: 10px; } h2.sample::before { /*类名为sample的h2标签不添加样式*/ content: none; } </style>
3.插入图像文件
使用before或after选择器,除了可以在元素的前面或后面插入文字之外,还可以插入图像文件。插入图像时,需要使用url属性值来指定图像文件路径如下:html:
<body> <h2 class="new">我是一个标题B</h2> <h2 class="new">我是一个标题C</h2> <h2>我是标题D</h2> </body>
css:
<style> h2.new::after { content: url(./images/02.jpg); } </style>
解析:目前IE8只支持插入文字,不支持插入图像文件;
插入图像文件的好处:虽然可以利用img元素在画面中追加图像文件,但是也可以使用样式表来追加图像文件,好处是节省页面编码的时间。推荐使用插入图像文件的方法。
用途:新发表的文章、新到的货物等
4.使用content属性来插入项目编号
html:
<body> <h2 class="new">我是一个标题</h2> <p>示例文字</p> <h2 class="new">我是一个标题</h2> <p>示例文字</p> <h2 class="new">我是一个标题</h2> <p>示例文字</p> </body>
css:
<style> h2::before{ content: counter(mycounter);/*mycounter是计数器名*/ } h2 { counter-increment: mycounter;/*设置连续编号*/ } </style>
解析:counter-increment是为了使用连续编号来设置before或after选择器的content属性值中所设定的计数器名;
5.在项目编号中追加文字
html:
<body> <h2>我是一个标题</h2> <p>示例文字</p> <h2>我是一个标题</h2> <p>示例文字</p> <h2>我是一个标题</h2> <p>示例文字</p> </body>
css:
<style> h2::before{ content: '第'counter(mycounter)'章'; color: blue; /*设置编号的样式*/ font-size: 30px; } h2 { counter-increment: mycounter; } </style>
6.指定编号的种类
用before或after选择器的content属性,不仅可以追加数字编号,还可以追加字母编号、罗马数字编号。使用方法如下:content:counter(计数器名,编号种类)
编号种类:使用“upper-alpha”指定大写字母编号;”upper-roman”指定大写罗马字母
例如:
大写字母编号css:
<style> h2::before{ content: counter(mycounter,upper-alpha);/*大写字母编号*/ color: blue; font-size: 30px; } h2 { counter-increment: mycounter; } </style>
大写罗马字母css:
<style> h2::before{ content: counter(mycounter,upper-roman);/*大写罗马字母编号*/ color: blue; font-size: 30px; } h2 { counter-increment: mycounter; } </style>
7.编号嵌套
html:
<body> <h2>我是一个标题</h2> <h4>我是中标题</h4> <h4>我是中标题</h4> <h2>我是一个标题</h2> <h4>我是中标题</h4> <h4>我是中标题</h4> <h2>我是一个标题</h2> <h4>我是中标题</h4> <h4>我是中标题</h4> </body>
第一张图片css:
<style> h2::before{ content: counter(mycounter)'.'; color: blue; font-size: 30px; } h2 { counter-increment: mycounter; } h4::before { content: counter(subcounter)'.'; } h4 { counter-increment: subcounter; margin-left: 40px; } </style>
第二张图片css:
<style> h2::before{ content: counter(mycounter)'.'; color: blue; font-size: 30px; } h2 { counter-increment: mycounter; counter-reset: subcounter; /*将中编号进行重置*/ } h4::before { content: counter(subcounter)'.'; } h4 { counter-increment: subcounter; margin-left: 40px; } </style>
解析:在第一张图片中看到这6个中标题的编号是连续的,如果要使大标题里的中标题重新开始编号的话,需要在大标题中使用
counter-reset属性将中编号进行重置。即在h2元素的样式指定代码添加
counter-reset属性。
相关文章推荐
- CSS3之使用选择器在页面中插入内容
- css3之使用选择器在页面中插入内容
- CSS3之利用选择器和content属性在页面中插入内容
- CSS3系列一(概述、选择器、使用选择器插入内容)
- CSS3系列一(概述、选择器、使用选择器插入内容)
- 使用选择器在页面中插入内容
- 使用选择器在页面中插入内容
- CSS3之利用选择器和content属性在页面中插入内容
- Css3(02)使用选择器插入内容(before/after)
- 使用选择器在页面中插入
- 使用CSS选择器在页面中插入内容
- 使用before和after选择器向页面中插入文字图片编号引号
- Css3学习笔记(二):在页面中插入内容
- CSS3选择器/使用content属性来插入项目编号
- 使用PHP向Mysql数据库插入信息,页面中文内容显示正常,但在数据库里却是乱码,这个怎么解决?
- js操作cookie;js的setInterval;C#获取指定页面的内容;Ajax.dll的使用
- 使用css3选择器,兼容多浏览器
- jQuery-Selectors(选择器)的使用(四--五、内容篇&可见性篇)
- jQuery Selectors(选择器)的使用(四-五、内容篇&可见性篇)
- jQuery-Selectors(选择器)的使用(四--五、内容篇&可见性篇)