CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



CSS Pseudo-element

CSS Pseudo-elements are used to style specific parts of an element. They are not part of the HTML markup, but are created using CSS. Pseudo-elements are denoted by a double colon (::) before the element name.

There are two types of pseudo-elements: ::before and ::after. These elements allow you to insert content before or after an element's content, without having to modify the HTML markup.

The ::before pseudo-element is used to insert content before an element's content. This content can be text, an image, or any other HTML element. The ::after pseudo-element is used to insert content after an element's content.

Here is an example of how to use the ::before pseudo-element to insert an image before a paragraph:

<p>This is a paragraph.</p>

p::before {
  content: url("image.jpg");
}

In this example, the ::before pseudo-element is used to insert an image before the paragraph. The content property is used to specify the content to be inserted, which in this case is an image.

Here is an example of how to use the ::after pseudo-element to insert text after a paragraph:

<p>This is a paragraph.</p>

p::after {
  content: " - End of paragraph";
}

In this example, the ::after pseudo-element is used to insert text after the paragraph. The content property is used to specify the content to be inserted, which in this case is text.

Pseudo-elements can also be used to style specific parts of an element. For example, you can use the ::first-letter pseudo-element to style the first letter of a paragraph:

<p>This is a paragraph.</p>

p::first-letter {
  font-size: 2em;
  color: red;
}

In this example, the ::first-letter pseudo-element is used to style the first letter of the paragraph. The font-size and color properties are used to specify the font size and color of the first letter.

Pseudo-elements can also be used to create special effects, such as drop caps. Here is an example of how to use the ::first-letter pseudo-element to create a drop cap:

<p><span class="dropcap">T</span>his is a paragraph.</p>

p .dropcap {
  font-size: 3em;
  float: left;
  margin-right: 0.1em;
  color: red;
}

In this example, the ::first-letter pseudo-element is used to create a drop cap. The first letter of the paragraph is wrapped in a <span> element with a class of "dropcap". The font-size, float, margin-right, and color properties are used to style the drop cap.

CSS Pseudo-elements are a powerful tool for styling specific parts of an element. They allow you to create special effects and add content to your web pages without having to modify the HTML markup.

References

Activity