CSS content property


The content property in CSS is used primarily with pseudo-elements (::before and ::after) to insert content into a document. This property allows you to generate content dynamically before or after an element’s actual content, which can be useful for adding decorative elements, text, or other content without altering the HTML structure.

Syntax

selector::before { content: value; } selector::after { content: value; }
  • selector: The element to which you want to apply the pseudo-element.
  • value: The content to be inserted, which can be text, an image, or other values.

Values

  1. Text Content:

    • Description: You can insert plain text into a pseudo-element.
    • Example: content: "Text"; inserts the text "Text" before or after the element.
  2. Images:

    • Description: You can insert images using the url() function.
    • Example: content: url('image.png'); inserts an image before or after the element.
  3. Counters:

    • Description: You can use counters to display dynamically incrementing numbers.
    • Example: content: counter(section); inserts the current value of the section counter.
  4. Empty String:

    • Description: An empty string can be used to generate a pseudo-element without any visible content.
    • Example: content: ""; creates an empty pseudo-element, which can be useful for styling purposes.
  5. Quotes:

    • Description: You can use the content property to insert quotation marks.
    • Example: content: open-quote; inserts an opening quotation mark.
  6. No Value:

    • Description: If content is not specified, the pseudo-element will not be rendered.
    • Example: content: none; hides the pseudo-element.

Usage Example

The content property is commonly used with ::before and ::after pseudo-elements to add decorative or informational content. Here’s an example:

Example

/* Adding a decorative icon before each list item */ ul li::before { content: url('icon.png'); /* Insert an icon image before each list item */ margin-right: 8px; /* Add space between the icon and the text */ } /* Adding a quotation mark before and after a blockquote */ blockquote::before { content: open-quote; /* Insert opening quotation mark */ } blockquote::after { content: close-quote; /* Insert closing quotation mark */ }

HTML Example

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Content Property Example</title> <style> /* Include the CSS from the example above here */ </style> </head> <body> <ul> <li>First item</li> <li>Second item</li> <li>Third item</li> </ul> <blockquote> This is a quoted text. </blockquote> </body> </html>

Explanation:

  1. ul li::before:

    • Uses the content property with url('icon.png') to insert an icon image before each list item. The margin-right adds space between the icon and the text.
  2. blockquote::before and blockquote::after:

    • Uses the content property with open-quote and close-quote to insert quotation marks before and after the blockquote text.

Important Points

  1. Pseudo-Elements:

    • The content property is used specifically with ::before and ::after pseudo-elements. These pseudo-elements allow you to insert content before or after the actual content of an element.
  2. No Content, No Display:

    • If the content property is not set or is set to none, the pseudo-element will not be displayed.
  3. Styling:

    • You can apply other CSS properties to ::before and ::after pseudo-elements (e.g., color, font-size, background, etc.) to style the inserted content.
  4. Counter Integration:

    • You can use the content property in combination with counter() functions to create automatic numbering or other dynamic content.
  5. Browser Compatibility:

    • The content property and pseudo-elements are well-supported across modern browsers. However, testing across different browsers is recommended to ensure consistent behavior.