Skip to content

Commit e8c0a56

Browse files
authored
Merge pull request #911 from AmrutaJayanti/PS
Create Pseudo-class-and-Pseudo-elements.md
2 parents 06ac732 + cacfab8 commit e8c0a56

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
id: pseudo-class-and-pseudo-elements
3+
title: Pseudo class and Pseudo elements
4+
sidebar_label: Pseudo class and Pseudo elements
5+
tags: [css, introduction, web development, markup language, hyper text, web pages, career opportunities, personal growth, web-development, web design, web pages, websites, career opportunities, contribute to the web, stay relevant, express yourself, learn other technologies, have fun,pseudo classes,pseudo elements]
6+
description: In this tutorial you will learn about Pseudo classes and Pseudo elements in CSS
7+
---
8+
9+
Pseudo-classes are used to define the special states of an element. They are typically prefixed with a colon (`:`).
10+
11+
12+
1. `:hover` : Applies when the user designates an element (with a pointing device), but does not activate it. Often used to change the appearance of a button when the mouse pointer is over it.
13+
14+
```css
15+
button:hover {
16+
background-color: lightblue;
17+
}
18+
```
19+
20+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
21+
<button type="submit">Submit</button>
22+
<style>{`
23+
button:hover {
24+
background-color: lightblue;
25+
}
26+
`}</style>
27+
</BrowserWindow>
28+
29+
30+
2. `:focus` : Applies when an element has received focus (e.g., when clicked on or tabbed to).
31+
32+
```css
33+
input:focus {
34+
border-color: blue;
35+
}
36+
```
37+
38+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
39+
<input type="text" required/>
40+
<style>
41+
{`input:focus {
42+
border-color: blue;
43+
}`}
44+
</style>
45+
</BrowserWindow>
46+
47+
3. `:nth-child(n)` : Matches elements based on their position in a group of siblings.
48+
49+
```css
50+
li:nth-child(2) {
51+
color: green;
52+
}
53+
```
54+
55+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
56+
<ul>
57+
<li>Red</li>
58+
<li>Green</li>
59+
<li>BLue</li>
60+
</ul>
61+
<style>
62+
{`
63+
li:nth-child(2) {
64+
color: green;
65+
}
66+
`}
67+
</style>
68+
</BrowserWindow>
69+
70+
71+
4. `:first-child` : Applies to the first child of its parent.
72+
73+
```css
74+
.container div:first-child {
75+
color: blue;
76+
font-weight: bold;
77+
}
78+
```
79+
80+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
81+
<div className="pseudo_container">
82+
<div>Hello</div>
83+
<div>World</div>
84+
</div>
85+
<style>
86+
{`.pseudo_container div:first-child {
87+
color: blue;
88+
font-weight: bold;
89+
}`}
90+
</style>
91+
</BrowserWindow>
92+
93+
5. `:nth-of-type(n)` : Matches elements based on their position among siblings of the same type.
94+
95+
```css
96+
div:nth-of-type(3)
97+
{
98+
color: red;
99+
}
100+
```
101+
102+
## Pseudo Elements
103+
104+
1. `::before` : Inserts content before an element's actual content.
105+
106+
```css
107+
p::before {
108+
content: "Note: ";
109+
font-weight: bold;
110+
}
111+
```
112+
113+
2. `::after` : Inserts content after an element's actual content.
114+
115+
```css
116+
p::after {
117+
content: " - Read more";
118+
font-style: italic;
119+
}
120+
```
121+
122+
3. `::first-line` : Applies styles to the first line of a block-level element.
123+
124+
```css
125+
p::first-line {
126+
color: red;
127+
font-weight: bold;
128+
}
129+
```

0 commit comments

Comments
 (0)