• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • content

    CSS的 content 属性用于在元素的::before::after伪元素中插入内容。使用content属性插入的内容都是匿名的可替换元素。

    示例

    content: normal                                /* Keywords that cannot be combined with other values */
    content: none
    
    content: 'prefix'                              /*  value, non-latin characters must be encoded e.g. \00A0 for   */
    content: url(http://www.example.com/test.html) /*  value */
    content: chapter_counter                       /*  values */
    content: attr(value string)                    /* attr() value linked to the HTML attribute value */
    content: open-quote                            /* Language- and position-dependant keywords */
    content: close-quote
    content: no-open-quote
    content: no-close-quote
    
    content: open-quote chapter_counter            /* Except for normal and none, several values can be used simultaneously */
    
    content: inherit
    

    浏览器支持

    IE浏览器火狐浏览器opera浏览器chrome浏览器safari浏览器
    IE8以上浏览器都支持content

    语法:

    content:normal| none| <content-list>

    <content-list>:取值[pending(<identifier>)| <string>| contents | footnote | endnote | div-note | list-item |<counter>| <named-string>| open-quote | close-quote | no-open-quote | no-close-quote | icon |<glyph>| <uri>| <datetime>| document-url |<target>]

    <counter>=counter(name)| counter(name,list-style-type)| counters(name,string)| counters(name,string,list-style-type)

    取值:

    • none:不会产生伪类元素
    • normal:在:before:after伪类元素中会被视为none
    • <string>:文本内容
    • <uri>url()URI值会指定一个外部资源(比如图片)。如果该资源或图片不能显示,它就会被忽略或显示一些占位(比如无图片标志)。
    • <counter>:计数器可以指定两种不同的函数:counter()counters()。前面一个有两种形式:counter(name)counter(name,style)。产生的内容是该伪类元素指定名称的最小范围的计数;格式由style指定(默认是'decimal'——十进制数字)。后一个函数同样也有两种形式:counters(name,string)counters(name,string,style)。The generated text is the value of all counters with the given name in scope at this pseudo-element, from outermost to innermost separated by the specified string. The counters are rendered in the indicated style('decimal' by default). See the section on automatic counters and numbering for more information. The name must not be 'none','inherit' or 'initial'. Such a name causes the declaration to be ignored.
    • attr(X)将元素的X属性以字符串形式返回。如果该元素没有 X 属性,则返回一个空字符串。区分大小写的属性返回值依赖文挡的语言设定。
    • open-quote|close-quote这些值会被quotes中定义的字符串替换。
    • no-open-quote|no-close-quote不会生产任何内容,但是会改变(增加或降低)引号层级。

    例子

    <h1>5</h1>
    <p> we shall start this with a quote from sir tim berners-lee,
     <q cite="http://www.w3.org/people/berners-lee/faq.html#internet">
     i was lucky enough to invent the web at the time when the internet already existed - and had for a decade and a half.</q> we must understand that there is nothing fundamentally wrong with building on the contributions of others.
    </p>
    
    <h1>6</h1>
    <p> here we shall quote the mozilla manifesto,
     <q cite="http://www.mozilla.org/en-us/about/manifesto/">
     individuals must have the ability to shape the internet and their own experiences on the internet.</q> and so, we can infer that contributing to the open web, can protect our own individual experiences on it.
    </p>
    
    //CSS
    q {
     color: #ff6600;
     font-style: italic;
    }
    
    q::before { content: open-quote }
    q::after { content: close-quote }
    
    h1::before { content: "chapter "; }
    

    5

    we shall start this with a quote from sir tim berners-lee, i was lucky enough to invent the web at the time when the internet already existed - and had for a decade and a half. we must understand that there is nothing fundamentally wrong with building on the contributions of others.

    6

    here we shall quote the mozilla manifesto, individuals must have the ability to shape the internet and their own experiences on the internet. and so, we can infer that contributing to the open web, can protect our own individual experiences on it.

    代码举例- link前面加一个icon

    <a href="https://www.lanmper.cn/">home page</a>
    
    //CSS
    a::before{
     content: url(https://www.lanmper.cn/favicon.ico) " lanmper: ";
     font: x-small arial,freesans,sans-serif;
     color: gray;
    }
    
    CSS
    <div>
      <ul class="brightidea">
     <li>this is my first idea</li>
     <li>and another good idea</li>
      </ul>
    </div>

    CSS

    /* first import the icon from a suitable site */
    @import url(http://weloveiconfonts.com/api/?family=entypo);
    
    .brightidea li::after{
     content: '1f4a1';
     font-family: 'entypo', sans-serif;
    }
    
    <h2>paperback best sellers</h2>
    <ol>
     <li>political thriller</li>
     <li class="newentry">halloween stories</li>
     <li>my biography</li> 
     <li class="newentry">vampire romance</li>
    </ol>
    
    //CSS
    .newentry::after {
     content: " new!";
     color: red;
    }
    
    <ul>
     <li><a id="moz" href="http://www.mozilla.org/">
     mozilla home page</a></li>
     <li><a id="mdn" href="https://developer.mozilla.org/">
     mozilla developer network</a></li>
     <li><a id="w3c" href="http://www.w3c.org/">
     world wide web consortium</a></li>
    </ul>
    
    //CSS
    a {
     text-decoration: none;
     border-bottom: 3px dotted navy;
    }
    
    a::after {
     content: " (" attr(id) ")";
    }
    
    #moz::before {
     content:url(https://mozorg.cdn.mozilla.net/media/img/favicon.ico) ;
    }
    
    #mdn::before {
     content:url(https://mdn.mozillademos.org/files/7691/mdn-favicon16.png) ;
    }
    
    #w3c::before {
     content:url(http://w3c.org/2008/site/images/favicon.ico) ;
    }
    
    li {
     margin: 1em;
    }
    
    

    上篇:text-shadow

    下篇:quotes