CodeNewbie Community 🌱

Cover image for What color should the text be? (A CSS Quiz)
Álvaro Montoro
Álvaro Montoro

Posted on • Originally published at alvaromontoro.com

What color should the text be? (A CSS Quiz)

Today, I ran two quick polls on Twitter (they may still be running) and then reshared them on Reddit (on r/css: initial and follow-up), Mastodon (front-end.social), and LinkedIn (on the HTML5/CSS3 group: initial and follow-up). As I write this article, results are still coming in, so feel free to answer on your favorite social media before continuing.

Poll 1

The question is simple: without running the code –that part is essential as if you run it, you'll know the answer immediately and would be cheating–what color is the text inside the <div>? 

This is the HTML code:

<div>Hello World</div>
Enter fullscreen mode Exit fullscreen mode

And this is the CSS:

div {
  color: blue;
}

div {
  color: red !important;
}

div {
  color: green  !  important; /* notice the spacing */
}
Enter fullscreen mode Exit fullscreen mode

There are four possible answers:

  • Blue
  • Red
  • Green
  • None of the above (Black)

Poll 2

Then, there's a follow-up question... which is genuinely the same question, just changing a little bit the code: without running the code, what color is the text inside the <div>?

This time, the HTML code is:

<div id="div" class="div">Hello World</div>
Enter fullscreen mode Exit fullscreen mode

And the CSS:

#div {
  color: blue;
}

.div {
  color: red !important;
}

div {
  color: green   !   important; /* notice the spacing */
}
Enter fullscreen mode Exit fullscreen mode

And again, there are four possible answers:

  • Blue
  • Red
  • Green
  • None of the above (Black)

I added the code into two demos, where you can see the answers and a short explanation (don't worry, I'll add both at the end of the article so you don't have to go outside):

Now I will add a picture to have some space before adding the solutions and explanations, so you can't see a spoiler while looking at the code or thinking about the questions.

Sphere over a grid
Photo by Michael Dziedzic on Unsplash.

.
.
.
.
.
.
.
.
.
.
.


Answers

Poll 1

The text will be Green. If you answered Red, don't worry, you are in good company, as most people answered that color.

Why though? Doesn't Red have an !important? Yes, it does, and so does Green. We add the ! delimiter followed by the important keyword to indicate that a declaration is important. But there is no requirement for them to be together! White spaces are allowed between the delimiter and the keyword: !important is the same as ! important. As we have two !important declarations and their rules have the same specificity, the one appearing last prevails: the color will be Green.

Poll 2

The text will be Red. If you answered Blue, you went with the most common answer on Twitter.

Why though? The id selector is more specific and sets the color to Blue... Right? Wrong! Because the other two rules have important declarations for colors (as explained in the previous section) even when less specific than the id selector. So we look at the relative specificity: .div has a class (1–0) and div is just an element/tag selector (0–1). .div is more specific. Thus the text will be Red.


Poster photo by Towfiqu barbhuiya on Unsplash.

Top comments (0)