FAQ: Selectors - Pseudo-class

This community-built FAQ covers the “Pseudo-class” exercise from the lesson “Selectors”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Full-Stack Engineer
Front-End Engineer
Back-End Engineer

FAQs on the exercise Pseudo-class

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I noticed that when I added the hover pseudo selector to the links before the attribute selectors the on hover action did not occur. Only when I placed this code after the attribute selectors did it work. Do all pseudo selectors need to be declared after base element, class, and attribute styling declarations?

Could we please have the list of all the pseudo-classes? Or a link where we can find the list of all the pseudo-classes?
pseudo-classes seem interesting to design a website but also need to make sure not to overdo it.

No, it is not like you need to place the pseudo selectors after all other rulesets.

It is only the particular codecademy environment that is made in such a way that you add the new ruleset at the end unless specified to do so. This kind of makes it easier for them to write code to check if the learner is actually doing what is being asked to and what error pops up when that is not done.

Further, I would advise you to make a folder and copy-paste the HMLT and CSS code in notepad++ in separate files and try to open the HTML code in a new browser window to see how your CSS code is working. See how the file naming would affect the linking.

Also, it is late but welcome to Codecademy.

1 Like

Here are two links that I’ve found:
https://www.w3schools.com/css/css_pseudo_classes.asp

I think it would be better if Codecademy gives an in-depth lesson on pseudo-classes because I, unfortunately, cannot memorize all of them.

2 Likes

Adding a hover pseudo-class to your page is feeling like giving him a little bit of the soul :smiley:
This changing of color feels like it’s alive :smiley: (I think I need to rest more :D)

Hello,

I can see no change after I have added the style rule in the css file.
The assignment is that I have to add a pseudo selector to change the colour of the links to dark orange on hover.
When I hover over the links the colour doesn’t change. Why is that?

Here’s my CSS code:

  • {
    border: 1px solid red;
    }

p {
color: green;
}

h1 {
color: maroon;
}

.title {
color: teal;
}

.uppercase {
text-transform: uppercase;
}

#article-title {
font-family: cursive;
}

a:hover {
color: darkorange;
}

a[href*=‘florence’] {
color: lightgreen;
}

a[href*=‘beijing’] {
color: lightblue;
}

a[href*=‘seoul’] {
color: lightpink;
}

Step 1 specifies:

Add a ruleset to the end of style.css that selects …

You need to move the rule to the bottom,

a[href*='seoul'] {
  color: lightpink;
}

a:hover {
  color: darkorange;
}

Since the specificity of the rules is the same, so the last rule will win.

Thanks for the reply.

I’m a bit confused here. Even though the specificity is the same for both rulesets, they are targeting different properties of the element. One is related to text color when there’s no interaction with the link, while the other is related to what happens to the text color once it is hovered over.

Why putting the hover ruleset before the text color ruleset nullifies the hover action? I was somehow expecting that both will apply

Let’s look at the two situations.

SITUATION A:
(a:hover placed at bottom of css file)

The a[href*=...] {...} rules will set the colors of the links to lightgreen, lightblue and lightpink as specified in the rules. This will show up on the webpage. When we hover over the links, then the a:hover {...} rule will come into play. Since the specificity of all these rules is same but the hover rule is placed at the bottom, so upon hovering, the hover rule will win and the color will change to darkorange. Once we stop hovering, the hover rule is no longer in play and we go back to the colors specified in the earlier rules.

SITUATION B:
(a:hover placed before the a[href*=] rules)

When we are not hovering over the links, the a[href*=...] {...} rules will set the colors of the links to lightgreen, lightblue and lightpink as specified. This will show up on the webpage.
When we hover over the links, the a:hover {...} rule comes into play. Now, this rule wants to change the color of the link to darkorange. But, the a[href*=...] {...} rules want to keep the colors the same as before. Two rules are competing over the same property. Since the specificity is the same and the a[href*=...] {...} rules appear at the bottom, so they will win and the color won’t change to darkorange.

Think of it this way. The a:hover {...} rule comes into consideration only when hovering (a special case). When not hovering, it is silent.
In contrast, the a[href*=...] {...} rules are applicable in all situations (broader and more general). Whether you hover or don’t hover, these rules are active and want to impose their coloring preference.

1 Like

This actually makes a lot of sense :slight_smile:
Thanks for the great explanation.

Cheers :beers:

1 Like

Hi, I just want to ask if pseudo classes are similar to javascript events bcause pseudo classes are quite interactive