search
HomeWeb Front-endCSS TutorialHow to use css content attribute

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 id="这是h">这是h1</h1>
        <h2 id="这是h">这是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 id="这是h">这是h1</h1>
		<h2 id="这是h">这是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
Two Images and an API: Everything We Need for Recoloring ProductsTwo Images and an API: Everything We Need for Recoloring ProductsApr 15, 2025 am 11:27 AM

I recently found a solution to dynamically update the color of any product image. So with just one of a product, we can colorize it in different ways to show

Weekly Platform News: Impact of Third-Party Code, Passive Mixed Content, Countries with the Slowest ConnectionsWeekly Platform News: Impact of Third-Party Code, Passive Mixed Content, Countries with the Slowest ConnectionsApr 15, 2025 am 11:19 AM

In this week's roundup, Lighthouse sheds light on third-party scripts, insecure resources will get blocked on secure sites, and many country connection speeds

Options for Hosting Your Own Non-JavaScript-Based AnalyticsOptions for Hosting Your Own Non-JavaScript-Based AnalyticsApr 15, 2025 am 11:09 AM

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

It's All In the Head: Managing the Document Head of a React Powered Site With React HelmetIt's All In the Head: Managing the Document Head of a React Powered Site With React HelmetApr 15, 2025 am 11:01 AM

The document head might not be the most glamorous part of a website, but what goes into it is arguably just as important to the success of your website as its

What is super() in JavaScript?What is super() in JavaScript?Apr 15, 2025 am 10:59 AM

What's happening when you see some JavaScript that calls super()?.In a child class, you use super() to call its parent’s constructor and super. to access its

Comparing the Different Types of Native JavaScript PopupsComparing the Different Types of Native JavaScript PopupsApr 15, 2025 am 10:48 AM

JavaScript has a variety of built-in popup APIs that display special UI for user interaction. Famously:

Why Are Accessible Websites so Hard to Build?Why Are Accessible Websites so Hard to Build?Apr 15, 2025 am 10:45 AM

I was chatting with some front-end folks the other day about why so many companies struggle at making accessible websites. Why are accessible websites so hard

The `hidden` Attribute is Visibly WeakThe `hidden` Attribute is Visibly WeakApr 15, 2025 am 10:43 AM

There is an HTML attribute that does exactly what you think it should do:

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.