FAQ: Learn CSS Selectors Visual Rules - CSS Setup Selectors - Important

This community-built FAQ covers the “Important” exercise in Codecademy’s CSS lessons.

FAQs on the CSS exercise Important

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

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

Need broader help or resources about CSS in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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

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

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try searching for it by clicking the spyglass icon (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

if i have 4

elements and i added !important style to one

element. will the !important apply to every

elemnts

This question keeps popping up and I strongly suspect it’s because a lesson author didn’t give it much thought and included the subject way too early in the introduction to CSS, long before learners understand anything about the cascade, specificity, inheritence and importance. It should not come up until near the end of any CSS course.

A naive learner will see !important as some kind of magic bullet that solves all their problems. They would be categorically incorrect in that notion. It doesn’t solve problems, it creates them.

In answer to the question, if all the elements are of the same type, regardless how they are classed or identified, the selector rule with !important will trump them all.

An earlier topic on this subject asks roughly the same question and to that was given an HTML/CSS example to demonstrate the omnipotence of !important. It should be easy to find that topic, and when you do, study the example closely by copying it to your computer and running it. It’s a clear case of a picture being a thousand words.

5 Likes

can !important override another !important with a different rule?

1 Like

This exercise talks about making our CSS more flexible. What does this mean and how can we accomplish this? - #17 by mtf

Use that model to add an !important modifier on the #h5 selector rule. What is the result?


Later…

      h5  {
        color: black !important;
      }
      h5 {
        color: red;
      }
      .h5  {
        color: blue;
      }
      #h5  {
        color: green !important;
      }

Output
important%20_over_important_result


!important specificity - Replit

Of course this is ludicrous. We would never code CSS to this extreme, but since we’re just playing…

Remove the modifier and see what happens.

1 Like

Why hasn’t !important been deprecated out of W3C standards as of HTML5 if there’s no real reason to use it in the first place?

On a side note, “naive” is a pejorative. Pretty sure that’s against Codecademy policy, sir.

I would support Roy’s use of the term naïve here.

1: (of a person or action) showing a lack of experience, wisdom, or judgment.
2: (of a person) natural and unaffected; innocent.

That is descriptive and accurate with what Roy was conveying, which was helpful advice.

3 Likes

Much appreciated on the second set of eyes, @oduffy, too much YouTube can wreak havoc on one’s mental state. I’m still curious as to why !important hasn’t been deprecated yet as of HTML5, if there’s almost no need to ever use it.

Back to work for this naive coder…

That is a really good question. Let’s take a look at the latest CSS specification:

CSS attempts to create a balance of power between author and user style sheets. By default, rules in an author’s style sheet override those in a user’s style sheet, which override those in the user-agent’s default style sheet. To balance this, a declaration can be made important, which increases its weight in the cascade and inverts the order of precedence.

An important declaration takes precedence over a normal declaration. Author and user style sheets may contain !important declarations, with user !important declarations overriding author !important declarations. This CSS feature improves accessibility of documents by giving users with special requirements (large fonts, color combinations, etc.) control over presentation.

!important is a very useful tool in the context it was meant to be used - in user style sheets. As a developer, you have full control over agent style sheets - there is no need to use !important.

It is a bad practice to use !important in agent style sheets and this is clearly stated by the CSS specification.

!important breaks the natural flow of precedence and specificity. It might help you solve your problem quickly, but this will lead to other problems in the future. !important is meant to be used in the user style sheets.


Sometimes we don’t have full control over the agent style sheets. Sometimes we have to work with a 3rd party library which we cannot modify or we have to deal with an API which returns markup code with inline styles applied.

This is where the use of !important is justified and it might even be the best solution in terms of the maintenance of the codebase. But this happens rarely.

2 Likes

@factoradic

Thanks, that’s a pretty cool workaround. I can see why it came with a warning label during the lesson. :grimacing:

2 Likes

You’re very welcome :slight_smile:

2 Likes

Can someone explain the difference between

h2.destination 

destination.h2

How do I know when to use either or?

for some reason I am progressing in the lesson, but none of my code is affecting the learning space.
anyone have a reason why?

What is the difference between specificity selector and chaining selector

There is no such thing as a specificity selector. All selectors have specificity.

Chaining is done with class and id selectors, usually class.

1 Like

Try clicking the refresh button at the top of the displayed page window on the right-hand side.