CSS PseudoElement
CSS PseudoElement
Syntax
Pseudo-element has a simple syntax which is given as follows:
• selector::pseudo-element {
• property: value;
• }
Example
• <html>
• <head>
• <style>
• body{
• text-align: center;
• }
• h1::first-letter {
• font-family: Lucida Calligraphy;
• font-size: 3cm;
• color: red;
• text-shadow: 5px 8px 9px green;
• }
• h1{
• color: blue;
• }
• </style>
• </head>
• <body>
• <h1> Welcome to seven mentore </h1>
• <h2> This is an example of ::first-letter pseudo-element. </h2>
• </body>
• </html>
Output
Example
In this example we will see the use of ::first-line element to add special
effects to the element's first line.
• <html>
• <head>
• <style>
• body{
• text-align: center;
• }
• h1::first-line {
• font-family: Lucida Calligraphy;
• font-size: 1cm;
• color: red;
• text-shadow: 5px 8px 9px green;
• }
• </style>
• </head>
• <body>
• <h1> Welcome to the javaTpoint.com. This site is developed so that
students may learn computer science related technologies easily. The j
avaTpoint.com is committed to provide easy and in-depth tutorials on v
arious technologies. </h1>
• <h2> This is an example of ::first-line pseudo-element. </h2>
• </body>
• </html>
Output
Example
• <html>
• <head>
• <style>
• body{
• text-align: center;
• }
• h1::before {
• content: "'Hello World.'";
• color: red;
• }
• </style>
• </head>
• <body>
• <h1>Welcome to the javaTpoint.com. </h1>
• <h2> This is an example of ::before pseudo-element. </h2>
• <h3> In the first line the "Hello World" has added by using the pseu
do-element ::before </h3>
• </body>
• </html>
Output
Example
• <html>
• <head>
• <style>
• body{
• text-align: center;
• }
• h1::after {
• content: "'Welcome to the javaTpoint.com.'";
• color: red;
• }
• </style>
• </head>
• <body>
• <h1> Hello World. </h1>
• <h2> This is an example of ::after pseudo-element. </h2>
• <h3> In the first line the "Welcome to the javaTpoint.com." has add
ed by using the pseudo-element ::after </h3>
• </body>
• </html>
Output
• color.
• background-color.
Example
• <html>
• <head>
• <style>
• body{
• text-align: center;
• }
• h1::selection {
• color: red;
• }
• </style>
• </head>
• <body>
• <h1> Hello World. </h1>
• <h2> Select the text in first line to see the effect. </h2>
• <h3> This is an example of ::selection pseudo-element. </h3>
• </body>
• </html>
Output
Syntax
It can be written as:
• selector.class::pseudo-element {
• property: value
• }
Example
This example will affect the first letter of those <h1> elements that
have class="example" rather than affecting the all <h1> elements.
• <html>
• <head>
• <style>
• body{
• text-align: center;
• }
• h1.example::first-letter {
• color: red;
• font-size: 2cm;
• font-family: Lucida Calligraphy;
• }
• </style>
• </head>
• <body>
• <h1 class="example"> Hello World. </h1>
• <h1> Welcome to the javaTpoint.com. </h1>
• <h3> This is an example of pseudo-element with CSS classes.</
h3>
• </body>
• </html>
Output