Home > Article > Web Front-end > How to use css3 to add the content attribute? Usage of content attribute (code example)
This chapter will introduce how to use css3 to add the content attribute? The use of the content attribute (code example), detailed explanation of the css3 content (content) attribute, let everyone know how to use the content attribute to insert, clear floats, and insert content. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Detailed explanation of the content attribute
1. The role of the content attribute:
The content attribute is used to insert generated content, often with the :before selector and the :after selector Used in conjunction with clearing floats or placing generated content before or after an element's content.
2. Basic syntax:
content: normal | string | attr() | uri() | counter();
normal: Default value.
string: Search the content of the text, usually a string.
attr(): Insert the attribute value of the element, syntax: attr(attribute).
uri(): Insert an external resource file, which can be an image, audio, video file or any other resource supported by the browser.
counter(): Counter, used to insert sorting identifiers. counter() can not only append numeric numbers, but also alphanumeric numbers or Roman numeral numbers. Syntax: content:couter (counter name, number type)
3. Browser support:
All browsers support the content attribute.
Note: IE8 only supports the Content attribute if !DOCTYPE is specified.
2. Application of css content attribute
1. The css content attribute uses the pseudo-class after to clear floats
The content attribute of css is specially applied to the before/after pseudo-element. The most common application is to use pseudo-classes to clear floats.
//一种常见利用伪类清除浮动的代码 .clearfix:after { content:"."; //这里利用到了content属性 display:block; height:0; visibility:hidden; clear:both; } .clearfix { *zoom:1; }
Principle:
The after pseudo-element uses content to generate a block-level element with a content of one point behind the element, and then uses clear:both to clear the float.
2. The css content attribute uses pseudo elements to insert content into the page
1) Insert plain text
Usage: content: "Insert article", or content: none does not insert content
Example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>插入纯文字</title> <style> h1::after{ content:",在h1后插入内容" } h2::after{ content:none } </style> </head> <body> <h1>这是h1</h1> <h2>这是h2</h2> </body> </html>
Rendering:
2) Insert text symbols
You can use the following attribute values of the css content attribute to implement the insertion (deletion) of text symbols:
none Set Content, if specified as Nothing;
open-quote Set Content is the opening Quotes;
close-quote Set Content to be closed quotes;
no-open-quote If specified, remove the opening quotes of the content;
no-close-quote if Specify, remove the closing quotation marks of the content;
inherit The value of the specified content attribute should be inherited from the parent element.
The open-quote attribute value and close-quote attribute value of the content attribute add nested text symbols such as brackets, single quotes, and double quotes on both sides of the string. open-quote is used to add the starting text symbol, and close-quote is used to add the ending text symbol.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>插入文字符号</title> <style> h1 {quotes: "(" ")";/*利用元素的quotes属性指定文字符号*/} h1::before {content: open-quote;} h1::after {content: close-quote;} h2 {quotes: "\"" "\"";/*添加双引号要转义*/} h2::before {content: open-quote;} h2::after {content: close-quote;} </style> </head> <body> <h1>这是h1</h1> <h2>这是h2</h2> </body> </html>
Rendering:
3), insert the image
css content attribute can also be directly added to the element Insert pictures before/after
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>插入图片</title> <style> p::after { content: url(cssHow to use css3 to add the content attribute? Usage of content attribute (code example)); border: 1px solid powderblue; } </style> </head> <body> <p>这是一段测试文字,文字后面是图片:</p> </body> </html>
Rendering:
Summary: The above are some examples of the application of css content attributes. It is very simple. You can do it yourself. Try writing.
The above is the detailed content of How to use css3 to add the content attribute? Usage of content attribute (code example). For more information, please follow other related articles on the PHP Chinese website!