Home  >  Article  >  Web Front-end  >  How to use css content attribute

How to use css content attribute

青灯夜游
青灯夜游Original
2019-05-28 17:28:464124browse

css content attribute is used to insert generated content. It is often used with the :before selector and :after selector to clear floating or place the generated content in front or behind the content of an element. All browsers support the content attribute.

How to use css content attribute

How to use the css content attribute?

The content attribute is used to insert generated content. It is often used in conjunction with the :before selector and :after selector to clear floats or place the generated content in front of the content of an element or later.

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)

Description: This attribute is used to define the generated content placed before or after the element. By default, this is often inline content, but the type of box this content creates can be controlled using the display attribute.

Note: All browsers support the content attribute. Internet Explorer 8 (and later) supports the content attribute if !DOCTYPE is specified.

Examples of using the css content attribute

1. The css content attribute uses the pseudo-class after to clear the float

The content attribute of css is specially applied to before/after pseudo-elements. 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: "Inserted 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:

How to use css content attribute

2 ) Inserting 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 to be open quotes;

● close-quote: Set Content to be closed quotes;

● no-open-quote: If specified, remove the beginning of the content Quotes;

● no-close-quote: If specified, remove the closing quotes 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:

How to use css content attribute

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 css content attribute);
				border: 1px solid powderblue;
			}
		</style>
	</head>

	<body>
		<p>这是一段测试文字,文字后面是图片:</p>
	</body>

</html>

Rendering:

How to use css content attribute

The above is the detailed content of How to use css content attribute. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn