百科狗-知识改变命运!

content - 对象前或后显示内容 - css 文本样式

乐乐2年前 (2023-11-21)阅读数 50#技术干货
文章标签元素

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

浏览器支持

IE8以上浏览器都支持content

语法:

content:normal|none|

:取值[pending()| | contents | footnote | endnote | div-note | list-item || | open-quote | close-quote | no-open-quote | no-close-quote | icon || | | document-url |]

content - 对象前或后显示内容 - css 文本样式

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

取值:

  • none:不会产生伪类元素
  • normal:在:before:after伪类元素中会被视为none
  • :文本内容
  • url()URI值会指定一个外部资源(比如图片)。如果该资源或图片不能显示,它就会被忽略或显示一些占位(比如无图片标志)。
  • :计数器可以指定两种不同的函数: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不会生产任何内容,但是会改变(增加或降低)引号层级。

例子

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.

//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.

.demo1 q{color:#ff6600; font-style: italic;}.demo1 q::before{content: open-quote}.demo1 q::after{content: close-quote}.demo1 h1::before{content:"chapter ";}

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

home page

//CSS
a::before{
 content: url(https://www.baikegou.com/favicon.ico) " qq344225443: ";
 font: x-small arial,freesans,sans-serif;
 color: gray;
}
CSS.demo2 a::before{content: url(https://www.baikegou.com/favicon.ico)" qq344225443:"; font: x-small arial,freesans,sans-serif; color: gray;}
  • this is my first idea
  • and another good idea

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;
}

paperback best sellers

  1. political thriller
  2. halloween stories
  3. my biography
  4. vampire romance
//CSS .newentry::after { content: " new!"; color: red; }
  • mozilla home page
  • mozilla developer network
  • world wide web consortium
//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; }

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}