Skip to content

Commit f6e9e06

Browse files
authored
chore(curriculum): add pseudo-classes and elements transcripts (freeCodeCamp#58474)
1 parent 6ce7be4 commit f6e9e06

File tree

7 files changed

+947
-7
lines changed

7 files changed

+947
-7
lines changed

curriculum/challenges/english/25-front-end-development/lecture-working-with-pseudo-classes-and-pseudo-elements-in-css/672aa74f761c065c9828a95e.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,107 @@ dashedName: what-are-pseudo-classes
88

99
# --description--
1010

11-
Watch the video lecture and answer the questions below.
11+
Watch the video or read the transcript and answer the questions below.
12+
13+
# --transcript--
14+
15+
What are pseudo-classes, and how do they work?
16+
17+
Pseudo-classes are special CSS keywords that allow you to select an element based on its specific state or position. The element's state or position could include:
18+
19+
- When it's active.
20+
- When it's being hovered over by a mouse.
21+
- When it's the first child of a parent.
22+
- When it's the last child of a parent.
23+
- When a link has been visited.
24+
- When it's disabled.
25+
26+
To use a pseudo-class, you add it to the selector by using a colon (`:`) followed by the name of the pseudo-class:
27+
28+
```css
29+
selector:pseudo-class {
30+
/* CSS properties */
31+
}
32+
```
33+
34+
Let's see how you can use a pseudo-class to represent each of the states and positions we already mentioned.
35+
36+
The `:active` pseudo-class lets you select the active state of an element, like clicking on a button:
37+
38+
```css
39+
button:active {
40+
background: greenyellow;
41+
}
42+
```
43+
44+
The `:hover` pseudo-class defines the hover state of an element. An example would be hovering over a button or link:
45+
46+
```css
47+
a:hover {
48+
text-decoration: none;
49+
background: crimson;
50+
}
51+
```
52+
53+
The `:first-child` pseudo-class selects an element that is the first child of its parent element. Here's an HTML example showing a `div` element containing multiple paragraph elements:
54+
55+
```html
56+
<div class="container">
57+
<p>first child</p>
58+
<p>second child</p>
59+
<p>third child</p>
60+
<p>last child</p>
61+
</div>
62+
```
63+
64+
Here's the CSS that selects the first paragraph element in that `div` container:
65+
66+
```css
67+
.container p:first-child {
68+
background: lightcoral;
69+
padding: 0.4rem;
70+
}
71+
```
72+
73+
The first paragraph element in that `div` will receive the `lightcoral` background color and `padding` of `0.4rem` on all four sides.
74+
75+
The `:last-child` pseudo-class targets the last element that is the child of its parent. Here is an example of targeting the last paragraph element in the container `div` element:
76+
77+
```css
78+
.container p:last-child {
79+
background: lightcoral;
80+
padding: 0.4rem;
81+
}
82+
```
83+
84+
The `:visited` pseudo-class lets you style a link the user has already visited:
85+
86+
```css
87+
a:visited {
88+
color: purple;
89+
}
90+
```
91+
92+
The `:disabled` pseudo-class lets you style an interactive element in disabled mode:
93+
94+
```css
95+
button:disabled {
96+
background-color: lightgray;
97+
}
98+
```
99+
100+
As you can see, pseudo-classes let you style elements based on user interactions, like hovering or visiting a link. This makes your website feel more interactive.
101+
102+
Apart from the pseudo-classes already mentioned, there are others like:
103+
104+
- `:focus`
105+
- `:first-of-type`
106+
- `:last-of-type`
107+
- `:nth-of-type`
108+
- `:modal`
109+
- `:enabled`
110+
- `:checked`
111+
- `:required`, and more.
12112

13113
# --questions--
14114

curriculum/challenges/english/25-front-end-development/lecture-working-with-pseudo-classes-and-pseudo-elements-in-css/672bbe9171a5cca90f2edeea.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,100 @@ dashedName: what-are-examples-of-element-user-action-pseudo-classes
88

99
# --description--
1010

11-
Watch the lecture video and answer the questions below.
11+
Watch the video or read the transcript and answer the questions below.
12+
13+
# --transcript--
14+
15+
What are examples of element user action pseudo-classes?
16+
17+
User feedback is a crucial element of web design. For instance, it's important for users to receive visual cues when they interact with elements on a website, such as hovering over a button or clicking a link. This feedback helps users understand the state of interactive elements, like indicating whether a link has been visited or not.
18+
19+
User action pseudo-classes in CSS are special keywords that allow you to provide this kind of feedback without needing JavaScript or other programming languages.
20+
21+
These pseudo-classes include `:hover`, `:active`, `:focus`, and `:visited`, among others. They enable you to change the appearance of elements based on user interactions, improving the overall user experience.
22+
23+
Let's dive into some of the user action pseudo-classes we have and see how they work.
24+
25+
The `:active` pseudo-class applies styles when an element is activated by the user. For example, when the user clicks a button or a link, it provides immediate visual feedback, showing users that their actions are being recognized.
26+
27+
```css
28+
a:active {
29+
color: crimson;
30+
}
31+
```
32+
33+
The `:hover` pseudo-class is triggered when a user hovers over an element with their mouse or other pointing device. Developers often use it to create visual feedback for buttons, links, or any element that should respond to user attention. Here's a button a user would hover over before clicking:
34+
35+
```html
36+
<button class="btn">Hover Over Me</button>
37+
```
38+
39+
Here's the CSS that changes the color, background color, and cursor of the button once the user hovers over it:
40+
41+
```css
42+
.btn:hover {
43+
background-color: darkgreen;
44+
color: white;
45+
cursor: pointer;
46+
}
47+
```
48+
49+
The `:focus` pseudo-class applies styles when an element gains focus, typically through keyboard navigation or when a user clicks into a form input. This is not just for feedback but also crucial for accessibility. It ensures that users who rely heavily on keyboards can easily identify which element they are interacting with.
50+
51+
Here's an input element inside a form element:
52+
53+
```html
54+
<form>
55+
<input type="text" />
56+
</form>
57+
```
58+
59+
Here's the CSS that gives the input a solid dark green border and a `border-radius` when the user clicks into it:
60+
61+
```css
62+
input:focus {
63+
outline: 2px solid darkgreen;
64+
border-radius: 4px;
65+
}
66+
```
67+
68+
The `:visited` pseudo-class targets a link the user has visited. This can be useful for helping users distinguish between pages they have already visited and the ones they are yet to visit. Here is an example of changing the anchor text color to cyan when the link is visited:
69+
70+
```css
71+
a:visited {
72+
color: cyan;
73+
}
74+
```
75+
76+
The `:checked` pseudo-class in CSS allows you to style form elements such as checkboxes and radio buttons when they are selected (checked). This pseudo-class is useful for customizing the appearance of these elements to enhance user experience, even though browsers provide default styles for them.
77+
78+
Here's the kind of checkbox you usually check to agree to terms on a website:
79+
80+
```html
81+
<form>
82+
Agree <input class="checkbox" type="checkbox" />
83+
</form>
84+
```
85+
86+
Here's how you can use the `:checked` pseudo-class to indicate to the user that it is checked:
87+
88+
```css
89+
.checkbox:checked {
90+
appearance: none;
91+
width: 12px;
92+
height: 12px;
93+
background-color: red;
94+
}
95+
```
96+
97+
In this example, we are using the `appearance` property set to `none` to remove the default styling applied by the browser to checkbox inputs. When the user checks the box, it will have a background color of `red`.
98+
99+
Other examples of action pseudo-classes are:
100+
101+
- `:focus-within`: for applying styles to an element when it or any of its descendants have focus.
102+
- `:enabled`: for targeting form buttons or other elements that are currently enabled.
103+
- `:disabled`: for targeting form buttons or other elements that are disabled.
104+
- `:target`: for applying styles to an element that is the target of a URL fragment (the part of a URL after the `#` symbol).
12105

13106
# --questions--
14107

curriculum/challenges/english/25-front-end-development/lecture-working-with-pseudo-classes-and-pseudo-elements-in-css/672bbe9d6ec03ea954d92ff7.md

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,126 @@ dashedName: what-are-examples-of-input-pseudo-classes
88

99
# --description--
1010

11-
Watch the lecture video and answer the questions below.
11+
Watch the video or read the transcript and answer the questions below.
12+
13+
# --transcript--
14+
15+
What are examples of input pseudo-classes?
16+
17+
The appearance and behavior of input elements matter when building web forms. A form with inputs that respond to user actions goes a long way in improving user experience, and it should not cause confusion or frustration.
18+
19+
CSS provides several input pseudo-classes that can make your forms more intuitive and user-friendly. Let's look at these pseudo-classes in detail.
20+
21+
The `:focus` pseudo-class allows you to target an input element when a user selects or clicks on it, drawing attention to the active input field. This helps users easily identify where they're currently typing:
22+
23+
```css
24+
input:focus {
25+
border: 2px solid crimson;
26+
outline: none;
27+
}
28+
```
29+
30+
As the name implies, the `:hover` pseudo-class is triggered when the user places the cursor over an element. It provides visual feedback before the user interacts with the input, effectively alerting them that the input is ready for action.
31+
32+
```css
33+
input:hover {
34+
background-color: orange;
35+
}
36+
```
37+
38+
In the example above, the input element background changes to an orange color when the user hovers over it, letting them know that the field is ready for interaction.
39+
40+
The `:checked` pseudo-class lets you style a radio button or checkbox differently when a user selects it. This way, the user can easily see which option they've chosen.
41+
42+
Here is an example:
43+
44+
```html
45+
<form>
46+
I have read the terms<input class="checkbox" type="checkbox">
47+
</form>
48+
```
49+
50+
```css
51+
.checkbox:checked {
52+
appearance: none;
53+
width: 12px;
54+
height: 12px;
55+
background-color: coral;
56+
}
57+
```
58+
59+
`:required` targets input elements that have the `required` attribute. It signals to the user that they must fill out the field to submit the form.
60+
61+
The HTML below has a form with two required input elements:
62+
63+
```html
64+
<form action="#">
65+
<label for="name">Name:</label>
66+
<input type="text" id="name" name="name" required />
67+
68+
<label for="email">Email:</label>
69+
<input type="email" id="email" name="email" required />
70+
71+
<label for="phone">Phone Number:</label>
72+
<input type="tel" id="phone" name="phone" />
73+
74+
<button type="submit">Submit</button>
75+
</form>
76+
```
77+
78+
And this CSS applies an orange border of two pixels to the two required inputs:
79+
80+
```css
81+
input:required {
82+
border: 2px solid orange;
83+
}
84+
```
85+
86+
When validating forms, you can use the `:valid` pseudo-class to style the input fields that meet the validation criteria, and `:invalid` to style the input fields that do not meet the criteria. Typically, you will use green for a valid input and red for an invalid input.
87+
88+
Here is an example using the `:valid` pseudo-class:
89+
90+
```css
91+
input:valid {
92+
border-color: green;
93+
}
94+
```
95+
96+
Here is an example of using the `:invalid` pseudo-class:
97+
98+
```css
99+
input:invalid {
100+
border-color: crimson;
101+
}
102+
```
103+
104+
The `:disabled` pseudo-class allows you to select and style input elements that are disabled. These elements have the `disabled` attribute, which prevents users from interacting with them. When an input is disabled, it cannot be clicked, focused, or edited.
105+
106+
Here is an example of a label and a disabled input element:
107+
108+
```html
109+
<label for="name">Name:</label>
110+
<input class="text-input" type="text" id="name" name="name" disabled />
111+
```
112+
113+
In the CSS, we are targeting that disabled input and giving it a background color of `lightgray` and changing the cursor pointer to `not-allowed`.
114+
115+
```css
116+
.text-input:disabled {
117+
background-color: lightgray;
118+
cursor: not-allowed;
119+
}
120+
```
121+
122+
The `cursor: not-allowed;` CSS property value changes the appearance of the cursor to indicate that an action is not permitted.
123+
124+
When applied to an element, it shows a cursor with a circle-slash symbol (usually a circle with a diagonal line through it) to signal to users that the element is not interactive or cannot be clicked or interacted with.
125+
126+
Here are some other examples of input pseudo-classes and what they do:
127+
128+
- `:autofill`: applies styles to input fields that the browser automatically fills with saved data.
129+
- `:optional`: styles input elements that are not required and can be left empty.
130+
- `:in-range` and `:out-of-range`: style elements based on whether their values are within or outside specified range constraints.
12131

13132
# --questions--
14133

0 commit comments

Comments
 (0)