Computer >> Computer tutorials >  >> Programming >> CSS

Pseudo-elements and CSS Classes


CSS Pseudo-elements can be applied on elements but also on CSS classes as well.

Example

Let’s see an example of using CSS Pseudo elements on CSS Classes −

<!DOCTYPE html>
<html>
<head>
<title>CSS Pseudo Elements and CSS Classes</title>
<style>
form {
   width:70%;
   margin: 0 auto;
   text-align: center;
}
* {
   padding: 2px;
   margin:5px;
   box-sizing: border-box;
}
input[type="button"] {
   border-radius: 10px;
}
.child{
   line-height: .1em;
   border: 4px solid black;
}
::selection{
   color:white;
}
.child:nth-child(1), h1::selection{
   background-color: #FF8A00;
}
.child:nth-child(2), div::selection{
   background-color: #F44336;
}
.child:nth-child(3), p::selection{
   background-color: #C303C3;
}
.child:nth-child(4), article::selection{
   background-color: #4CAF50;
}
.child:nth-child(5), code::selection{
   background-color: #FEDC11;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>CSS Pseudo Elements and CSS Classes</legend>
<div id="container">
<h1>A Heading</h1>
<div>Welcome to DIV World</div>
<p>Every one is welcome in this town. Make yourself comfertable</p>
<article>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</article>
<code>function helloWorld(){ document.write("Hello World");}</code>
<div><span class="child">h1</span><span class="child">div</span><span class="child">p</span><span class="child">article</span><span class="child">code</span></div>
</div>
</body></html>

Output

This will produce the following output −

Initial layout −

Pseudo-elements and CSS Classes

Selecting <h1> element and <div> element −

Pseudo-elements and CSS Classes

Pseudo-elements and CSS Classes

Selecting <p> element and <article> element −

Pseudo-elements and CSS Classes

Pseudo-elements and CSS Classes

Selecting <code> element −

Pseudo-elements and CSS Classes