
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
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 −
Selecting <h1> element and <div> element −
Selecting <p> element and <article> element −
Selecting <code> element −
- Related Articles
- Pseudo-classes and CSS Classes
- Pseudo-classes and all CSS Classes
- What are CSS pseudo-classes
- Anchor Pseudo-classes in CSS
- Working with CSS Pseudo Classes
- Commonly used pseudo-classes in CSS
- CSS Pseudo Elements
- How to manipulate CSS pseudo-elements ::before and ::after using jQuery?
- Difference Between Pseudo-Class and Pseudo-Element in CSS
- CSS :after pseudo-element
- CSS :before pseudo-element
- The ::before and ::after Pseudo-element in CSS
- CSS :first-letter pseudo-element
- :active pseudo class in CSS
- visited pseudo class in CSS

Advertisements