CSS3之利用选择器和content属性在页面中插入内容

CSS3之使用选择器在页面中插入内容

    1. 使用选择器来插入内容:例,
    h2:before{
                        content:‘COLUMN’;
                        color:white;
                        background-color:orange;
                        padding:1px 5px;
                        }
    2. 指定个别元素不进行插入:例,
    h2.sample.before{
                                    content:none;
                                    }    --表示h2中class为sample的不插入内容--
    3. 使用选择器插入图像:例,
    h2:before{
                        content:url(mark.png);
                        }
    4. 使用content属性来插入项目编号:
            在多个标题前加上连续编号:在content属性中使用counter属性来针对多个项目追加连续编号另外,还需要在元素的样式中追加对元素的counter-increment属性的指定,为了使用连续编号,需要将counter-increment属性的属性值设定为before选择器或after选择器的counter属性值中指定的计数器名。例,
            h1:before{
                                content:counter(mycounter);
                                }
            h1{
                counter-increment:mycount;
                }
    5. 在项目编号中追加文字:例,
    h1:before{
                        content:‘第’counter(mycounter)'章';
                        }
    指定编号的种类:
    content:counter(计数器名,编号种类)
    upper-roman:大写罗马字母
    upper-alpha:大写字母
    编号嵌套:可以在大编号中嵌套中编号,在中编号中嵌套小编号。
    大编号:
    h1:before{
                        content:counter(mycounter);
                        }
    h1{
        counter-increment:mycounter;
        }
    中编号:
    h2:before{
                        content:counter(subcounter);
                        }
    h2{
        counter-increment:subcounter;
        }
    在这个事例中,中标题的编号是连续的,如果想在第二个大标题里的中标题重新开始编号的话,需要在大标题中添加counter-reset属性,在这事例中counter-reset:subcounter
    
    6. 在字符串两边添加嵌套文字符号:可以使用content属性的open-quote属性值与close-quote属性值在字符串两边添加诸如括号、单引号、双引号之类的嵌套文字符号。open-quote属性值用于添加开始的嵌套文字符号,close-quote属性值用于添加结尾的嵌套文字符号。注意,当需要添加双引号时,需要使用“\”转义字符。例,
    
结果:
        


使用选择器在页面中插入文字

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         p:before  
  9.         {  
  10.             content: '你好,';  
  11.             color: white;  
  12.             background-color: orange;  
  13.             font-family: '黑体';  
  14.         }  
  15.   
  16.         p:after  
  17.         {  
  18.             content: ',见到你很高兴!';  
  19.             color: white;  
  20.             background-color: orange;  
  21.             font-family: 黑体;  
  22.         }  
  23.   
  24.         p.zhang:after  
  25.         {  
  26.             content: none;  
  27.         }  
  28.   
  29.         p.zhang:before  
  30.         {  
  31.             content: none;  
  32.         }  
  33.     </style>  
  34. </head>  
  35. <body>  
  36.     <p>小红</p>  
  37.     <p class="zhang">张林</p>  
  38.     <p>王强</p>  
  39. </body>  
  40. </html>  


在标题前插入图像文件

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         h1:after  
  9.         {  
  10.             content: url(img.png);  
  11.         }  
  12.     </style>  
  13. </head>  
  14. <body>  
  15.     <p>王强</p>  
  16. </body>  
  17. </html>  
在CSS3中,使用content属性的attr(属性名)来指定attr的值。这样可以将某个属性的属性值显示出来。
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         img:after  
  9.         {  
  10.             content: attr(alt);  
  11.             display: block;  
  12.         }  
  13.     </style>  
  14. </head>  
  15. <body>  
  16.     <p align="center">  
  17.         <img src="xp.png" alt="系统图片" width="200" height="200" />  
  18.     </p>  
  19. </body>  
  20. </html>  
添加编号
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         h3:before  
  9.         {  
  10.             content: '第'counter(count)'章';  
  11.             color: red;  
  12.             font-family: 黑体;  
  13.             font-size: 24px;  
  14.         }  
  15.   
  16.         h3  
  17.         {  
  18.             counter-increment: count;  
  19.         }  
  20.     </style>  
  21. </head>  
  22. <body>  
  23.     <h3>HTML5简介</h3>  
  24.     <h3>HTML5结构</h3>  
  25.     <h3>多媒体播放</h3>  
  26.     <h3>本地存储</h3>  
  27.     <h3>表单与文件</h3>  
  28.     <h3>新增元素</h3>  
  29. </body>  
  30. </html>  


使用before选择器或after选择器的counter属性,不但可以在编号中追回文字和设置样式,还可以为编号设置编号类型。

指定编号类型可以使用list-style-type属性,常用的编号种类介绍如下:


[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         h1:before  
  9.         {  
  10.             content: counter(count, upper-roman)'.';  
  11.             color: red;  
  12.             font-family: 黑体;  
  13.         }  
  14.   
  15.         h1  
  16.         {  
  17.             counter-increment: count;  
  18.         }  
  19.   
  20.         h2:before  
  21.         {  
  22.             content: counter(count1)'.';  
  23.             color: red;  
  24.             font-family: 黑体;  
  25.         }  
  26.   
  27.         h2  
  28.         {  
  29.             counter-increment: count1;  
  30.             margin-left: 50px;  
  31.         }  
  32.     </style>  
  33. </head>  
  34. <body>  
  35.     <h1>创建网站及站点信息</h1>  
  36.     <h2>网站建设理论</h2>  
  37.     <h1>安装IIS服务器</h1>  
  38.     <h2>站点管理</h2>  
  39.     <h1>网站栏目及版块设计</h1>  
  40.     <h2>网页文本概述</h2>  
  41.     <h1>设计产品信息版块</h1>  
  42.     <h2>多彩文字设计</h2>  
  43. </body>  
  44. </html>  

在符号两边嵌入文字符号

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         h1:before  
  9.         {  
  10.             content: open-quote;  
  11.             color: red;  
  12.             font-family: 黑体;  
  13.         }  
  14.   
  15.         h1:after  
  16.         {  
  17.             content: close-quote;  
  18.             color: red;  
  19.             font-family: 黑体;  
  20.         }  
  21.   
  22.         h1  
  23.         {  
  24.             quotes: "《" "》";  
  25.         }  
  26.     </style>  
  27. </head>  
  28. <body>  
  29.     <h1>ASP基础教程与实验指导</h1>  
  30.     <h1>计算机组装</h1>  
  31.     <h1>HTML5从新手到高手</h1>  
  32. </body>  
  33. </html>  

content属性主要用来插入内容,而该属性与before和after伪元素配合使用,将生成的内容放在一个元素内容的前面或后面。另外,该内容创建的框类型可以用display属性控制。
content: normal string attr() uri() counter()
normal
    默认值。
string   插入文本内容。
attr()   插入元素的属性值。
uri()   插入一个外部资源(图像、声频、视频或浏览器支持的其他任何资源)
counter()   计数器,用于插入排序标识。

[html]  view plain  copy
 print ?
  1. <!DOCTYPE html>  
  2.   
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         .text  
  9.         {  
  10.             width: 400px;  
  11.             height: 50px;  
  12.             line-height: 50px;  
  13.             overflow: hidden;  
  14.             text-align: center;  
  15.             color: #ff0000;  
  16.             border: #993300 solid 1px;  
  17.         }  
  18.   
  19.         #text_c:before  
  20.         {  
  21.             content: "您使用的浏览器支持content属性";  
  22.         }  
  23.     </style>  
  24. </head>  
  25. <body>  
  26.     <div id="text_c" class="text">  
  27.         ---content属性  
  28.     </div>  
  29. </body>  
  30. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值