• ::before和::after伪元素的用法


    css3为了区分伪类和伪元素,伪元素采用双冒号写法。

    常见伪类——:hover,:link,:active,:target,:not(),:focus。

    常见伪元素——::first-letter,::first-line,::before,::after,::selection。

    ::before和::after下特有的content,用于在css渲染中向元素逻辑上的头部或尾部添加内容。

    这些添加不会出现在DOM中,不会改变文档内容,不可复制,仅仅是在css渲染层加入。

    所以不要用:before或:after展示有实际意义的内容,尽量使用它们显示修饰性内容,例如图标。

    举例:网站有些联系电话,希望在它们前加一个icon☎,就可以使用:before伪元素,如下:

    1 <!DOCTYPE html>
    2 <meta charset="utf-8" />
    3 <style type="text/css">
    4     .phoneNumber::before {
    5     content:'260E';
    6     font-size: 15px;
    7 }
    8 </style>
    9 <p class="phoneNumber">12345645654</p>

    结果:

    content:

    ::before和::after必须配合content属性来使用,content用来定义插入的内容,content必须有值,至少是空。默认情况下,伪类元素的display是默认值inline,可以通过设置display:block来改变其显示。

    content可取以下值。

    1、string

    使用引号包一段字符串,将会向元素内容中添加字符串。如:a:after{content:""}

    如下:

     1 <!DOCTYPE html>
     2 <meta charset="utf-8" />
     3 <style type="text/css">
     4 p::before{
     5     content: "《";
     6     color: blue;
     7 }
     8 p::after{
     9     content: "》";
    10     color: blue;
    11 }
    12 </style>
    13 <p>平凡的世界</p>

    结果:

    2、attr()

    通过attr()调用当前元素的属性,比如将图片alt提示文字或者链接的href地址显示出来。

    <style type="text/css">
    a::after{
        content: "(" attr(href) ")";
    }
    </style>
    <a href="http://www.cnblogs.com/starof">starof</a>

    3、url()/uri()

    用于引用媒体文件。

    举例:“百度”前面给出一张图片,后面给出href属性。

    复制代码
    <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">百度</a>
    </body>    
    复制代码

    效果:

    4、counter()

    调用计数器,可以不使用列表元素实现序号功能。

    配合counter-increment和counter-reset属性使用:

    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>
  • 相关阅读:
    const 指针与指向const的指针
    Every breath you take
    数据结构之图(存储结构、遍历)
    六个前端开发工程师必备的Web设计模式/模块资源(转)
    mouseover和mouseout多次触发解决方法(兼容ie和firefox)(转)
    javascript DOM操作HTML文档
    Javascript 严格模式详解(转)
    你需要知道的三个 CSS3技巧(转)
    CommonJS规范(转)
    使用Yeoman,Grunt和Bower开发AngularJS(译)
  • 原文地址:https://www.cnblogs.com/2734156755z/p/9273433.html
Copyright © 2020-2023  润新知