打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
CSS3学习系列之选择器(四)
  • 使用选择器来插入文字

css2中,使用before选择器在元素前面插入内容,使用after选择器在元素后面插入内容,在选择器的content属性中定义要插入的内容。将content属性值设定为none,然后在不需要插入内容的元素中将class属性值设定为这个给定的类名就可以了。另外,在CSS2.1中,除了none属性值外,还未content属性添加了一个normal属性值,其作用与使用方法none属性值的作用相同,并且使用方法也相同。从css2.1开始,值右当使用before选择与after选择器的时候,normal属性值的作用才与none属性值的作用相同,都是不让选择器在个别元素的前面或后面插入内容,但是none属性值只能应用在这两个选择器中,而normal属性值还可以应用在其他用来插入内容的选择器中,而在css2中,只有before选择器与after选择器能够用来在元素的前面或后面插入内容,所以在两者的作用完全相同,在css3中,已经追加了其他一些可以插入内容的选择的提案,针对这一类选择器,就只能用normal属性值了,而且normal属性值的作用也会根据选择器的不同而发生变化。

  •  插入图像文件
h2:before{ content:url(mark.png)}h2>你好h2>

目前firefox、safari、opera浏览器都支持这种插入图像文件的功能,在ie8中只支持插入文字的功能,不支持插入图像文件的功能。

另外在css3的定义中还可以通过url属性来插入音频文件、视频文件等其他格式的文件,但目前还没有得到任何浏览器的支持。

  • 将alt属性的值作为图像的标题来显示

如果在content属性中通过”attr(属性名)”这种形式来指定attr属性值,可以将某个属性的属性值显示出来,例子:

DOCTYPE html>html lang='en'>head> meta charset='UTF-8'> title>attr属性的使用示例title> style> img:after{ content:attr(alt); display: block; text-align: center; margin-top: 5px; } style>head>body>div> p>img src='sky.jpg' alt='蓝天白云'/>p>div>body>html>
  •  在多个标题前加上连续编号

在content属性中使用counter属性值来针对多个项目追加连续编号,使用方法如下所示:

<元素>:before{ content:counter(计数器名);}

使用计数器来计算编号,计数器可以任意命名。

另外,还需要在元素的样式中追加对元素的counter-increment属性的指定,为了使用连续编号,需要将counter-increment属性的属性值设定为before选择器或after选择的counter属性值中所指定的计数器名。代码如下:

<元素>{ Counter-increment:before选择器或after选择器中指定的计数器名}

 

例子:

DOCTYPE html>html lang='en'>head> meta charset='UTF-8'> title>对多个项目追加连续编号的示例title> style> h1:before { content:counter(mycounter); } h1{ counter-increment: mycounter; } style>head>body>div> h1>大标题h1> p>示例文字p> h1>大标题h1> p>示例文字p> h1>大标题h1> p>示例文字p>div>body>html>
  • 在项目编号中追加文字

可以在插入的项目编号中加入文字,使项目编号变成类似”第1章”之类的带文字的编号。例如:

h1:before{content:’第’counter(mycounter)’章’}
  •  指定编号的种类

用before选择器或after选择器的content属性,不仅可以追加数字编号,还可以追加字母编号或罗马数字编号。使用如下所示的方法指定编号种类。

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

可以使用list-style-type属性的值来指定编号的种类,list-style-type为指定列表编号时所用的属性。

  • 编号嵌套

使用嵌套编号如下:

h1{ counter-increment:mycounter counter-reset:subcounter}

 

例如:

DOCTYPE html>html lang='en'>head> meta charset='UTF-8'> title>编号嵌套示例title> style> h1:before { content:counter(mycounter)'.'; } h1{ counter-increment: mycounter; counter-reset: subcounter; } h2:before{ content: counter(subcounter)'.'; } h2{ counter-increment: subcounter; margin-left: 40px; } style>head>body>h1>大标题h1>h2>中标题h2>h2>中标题h2>h2>中标题h2>h1>大标题h1>h2>中标题h2>h2>中标题h2>h2>中标题h2>body>html>
  • 中编号中嵌入大编号

可以将大编号嵌入在中编号中。例如:

DOCTYPE html>html lang='en'>head> meta charset='UTF-8'> title>编号多层嵌入的示例title> style> h1:before { content:counter(mycounter)'.'; } h1{ counter-increment: mycounter; counter-reset: subcounter; } h2:before{ content: counter(mycounter)'-'counter(subcounter)'.'; } h2{ counter-increment: subcounter; counter-reset: subsubcounter; margin-left: 40px; } h3:before{ content: counter(mycounter)'-'counter(subconter)'-'counter(subsubcounter)'.'; } h3{ counter-increment: subsubcounter; margin-left: 40px; } style>head>body>h1>大标题h1>h2>中标题h2>h3>小标题h3>h3>小标题h3>h2>中标题h2>h3>小标题h3>h3>小标题h3>h1>大标题h1>h2>中标题h2>h3>小标题h3>h3>小标题h3>h2>中标题h2>h3>小标题h3>h3>小标题h3>body>html>
  • 在字符串两边添加嵌套文字符号

可以使用conten属性的open-quote属性值与close-quote属性值在字符串两边添加诸如括号,单引号,双引号之类的嵌套文字符号,open-quote属性值用于添加开始的嵌套文字符号,close-quote属性值用于添加结尾的嵌套文字符号。

例如:

DOCTYPE html>html lang='en'>head> meta charset='UTF-8'> title>添加嵌套文字符号的示例title> style> h1:before { content: open-quote; } h1:after{ content: close-quote; } h1{ quotes:'('')'; } style>head>body>h1>标题h1>body>html>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
打印样式设计
css
HTML+CSS的书写格式
CSS的简介和语法
counter的用法
使用CSS计数器美化数字有序列表
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服