CSS Pseudo Elements
CSS Pseudo Elements
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R
CSS Float Tutorials
CSS Inline-block
References Exercises Bootcamp Upgrade Get Certified Create Website Sign Up Log in
CSS Align
CSS Combinators
CSS Pseudo-class
CSS Pseudo-element
CSS Opacity
CSS Navigation Bar
CSS Dropdowns
CSS Image Gallery
CSS Image Sprites
CSS Pseudo-elements
CSS Attr Selectors
❮ Previous Next ❯
CSS Forms
CSS Counters
CSS Website Layout
CSS Units What are Pseudo-Elements?
CSS Specificity
CSS !important A CSS pseudo-element is used to style specified parts of an element.
CSS Math Functions COLOR PICKER
For example, it can be used to:
school
w3 s
3
CE
02
TI 2
R
FI .
ED
The ::first-line pseudo-element is used to add a special style to the first line of a text.
Get started
The following example formats the first line of the text in all <p> elements:
Example
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
Try it Yourself »
font properties
color properties
background properties
word-spacing
letter-spacing
text-decoration
vertical-align
text-transform
line-height
clear
The double colon replaced the single-colon notation for pseudo-elements in CSS3. This was an attempt from W3C to distinguish
between pseudo-classes and pseudo-elements.
The single-colon syntax was used for both pseudo-classes and pseudo-elements in CSS2 and CSS1.
For backward compatibility, the single-colon syntax is acceptable for CSS2 and CSS1 pseudo-elements.
The following example formats the first letter of the text in all <p> elements:
Example
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
Try it Yourself »
font properties
color properties
background properties
margin properties
padding properties
border properties
text-decoration
vertical-align (only if "float" is "none")
text-transform
line-height
float
clear
Example
p.intro::first-letter {
color: #ff0000;
font-size: 200%;
}
Try it Yourself »
The example above will display the first letter of paragraphs with class="intro", in red and in a larger size.
Multiple Pseudo-elements
Several pseudo-elements can also be combined.
In the following example, the first letter of a paragraph will be red, in an xx-large font size. The rest of the first line will be blue,
and in small-caps. The rest of the paragraph will be the default font size and color:
Example
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
p::first-line {
color: #0000ff;
font-variant: small-caps;
}
Try it Yourself »
The following example inserts an image before the content of each <h1> element:
Example
h1::before {
content: url(smiley.gif);
}
Try it Yourself »
The following example inserts an image after the content of each <h1> element:
Example
h1::after {
content: url(smiley.gif);
}
Try it Yourself »
Example
::marker {
color: red;
font-size: 23px;
}
Try it Yourself »
The following CSS properties can be applied to ::selection : color , background , cursor , and outline .
The following example makes the selected text red on a yellow background:
Example
::selection {
color: red;
background: yellow;
}
Try it Yourself »
Exercise:
Set the background-color to red, of the first line of the paragraph.
<style>
{
background-color: red;
}
</style>
<body>
<p class="intro">
In my younger and more vulnerable years
my father gave me some advice that I've
been turning over in my mind ever since.
'Whenever you feel like criticizing anyone,' he told me,
'just remember that all the people in this world
haven't had the advantages that you've had.'
</p>
</body>
Submit Answer »
::after p::after Insert something after the content of each <p> element
::before p::before Insert something before the content of each <p> element
:first-child p:first-child Selects every <p> elements that is the first child of its parent
:first-of-type p:first-of-type Selects every <p> element that is the first <p> element of its parent
:in-range input:in-range Selects <input> elements with a value within a specified range
:lang(language) p:lang(it) Selects every <p> element with a lang attribute value starting with "it"
:last-child p:last-child Selects every <p> elements that is the last child of its parent
:last-of-type p:last-of-type Selects every <p> element that is the last <p> element of its parent
:nth-child(n) p:nth-child(2) Selects every <p> element that is the second child of its parent
:nth-last-child(n) p:nth-last-child(2) Selects every <p> element that is the second child of its parent, counting
from the last child
:nth-last-of-type(n) p:nth-last-of-type(2) Selects every <p> element that is the second <p> element of its parent,
counting from the last child
:nth-of-type(n) p:nth-of-type(2) Selects every <p> element that is the second <p> element of its parent
:only-of-type p:only-of-type Selects every <p> element that is the only <p> element of its parent
:only-child p:only-child Selects every <p> element that is the only child of its parent
:out-of-range input:out-of-range Selects <input> elements with a value outside a specified range
:target #news:target Selects the current active #news element (clicked on a URL containing that
anchor name)
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.