CSS content Property

You are Here:

Demo

This is a normal paragraph, but the quote is added by css content property.

CSS content Property

CSS content property is used with the ::before and ::after pseudo-elements, to insert generated content.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p::before{ content: open-quote; font-weight: bold; font-size: 28px; } p::after{ content: close-quote; font-weight: bold; font-size: 28px; } </style> </head> <body> <h1>CSS content Property</h1> <p>This is a paragraph.</p> </body> </html>

Syntax

Using CSS

element{ content: normal; }

Using Javascript

JavaScript doesn't allow you to add pseudo-class to a element. but we can do it in an alternate way. Check our last example.

Animatable

No, content property is not animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS content property is normal.

Property Value

The following table provides a list of values for CSS content property.

ValueExplanation
noneSpecifies that the pseudo-element is not generated.
normalComputes to none for the ::before and ::after pseudo-elements.
stringSets the content to the text you specify.
urlSets the content to a URL that designates an external resource (such as an image).
counterThe value of a CSS counter, generally a number.
attr(x)The value of the element's attribute x as a string.
open-quote | close-quoteThese values are replaced by the appropriate string from the quotes property.
no-open-quote | no-close-quoteIntroduces no content, but increments (decrements) the level of nesting for quotes.

Using no-open-quote and no-close-quote

In this example, we will demonstrate how to escape the quotes by using no-open-quote and no-close-quote.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p::before{ content: open-quote; font-weight: bold; font-size: 28px; } p::after{ content: close-quote; font-weight: bold; font-size: 28px; } #point::before{ content: no-open-quote; } #point::before{ content: no-close-quote; } </style> </head> <body> <h1>CSS content Property</h1> <p>This is first paragraph.</p> <p id="point">This is second paragraph.</p> </body> </html>

Using None

In this example, The pseudo-element will not be generated.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p::before{ content: open-quote; font-weight: bold; font-size: 28px; } p::after{ content: close-quote; font-weight: bold; font-size: 28px; } #point::before{ content: none; } #point::before{ content: none; } </style> </head> <body> <h1>CSS content Property</h1> <p>This is first paragraph.</p> <p id="point">This is second paragraph.</p> </body> </html>

Using Normal

Normal computes to none.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p::before{ content: open-quote; font-weight: bold; font-size: 28px; } p::after{ content: close-quote; font-weight: bold; font-size: 28px; } #point::before{ content: normal; } #point::before{ content: normal; } </style> </head> <body> <h1>CSS content Property</h1> <p>This is first paragraph.</p> <p id="point">This is second paragraph.</p> </body> </html>

Using String

In the following example, we will demonstrate how to add string (text) to the content.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p::before{ content: "Important - "; } </style> </head> <body> <h1>CSS content Property</h1> <p>This is a paragraph.</p> </body> </html>

Using counter()

In the following example, we will demonstrate how to set counter to the content.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> p{ counter-increment: index; } p::before{ content: counter(index); } </style> </head> <body> <h1>CSS content Property</h1> <p>This is first paragraph.</p> <p>This is second paragraph.</p> </body> </html>

Using attr

In the following example, we will demonstrate how to add attr to the content.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> a::before { content: attr(href); } </style> </head> <body> <h1>CSS content Property</h1> <a href="https://wikimass.com/js">(Learn JavaScript)</a> </body> </html>

Using JavaScript

In the following example, we will demonstrate how to set the CSS content property of an element using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> .point::before { content: attr(href); } </style> </head> <body> <h1>CSS content Property</h1> <a href="https://wikimass.com/js">(Learn JavaScript)</a> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementsByTagName("a")[0]; function myFunction(){ x.className += "point"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author