This question already has an answer here:
Currently I'm trying to understand how :active
and :focus
pseudo-classes related with each other.
Here is my example:
<a href="http://google.com" target="_blank">Google</a>
<style>
a:link { color: rgb(0, 138, 206); } /* Blue */
a:visited { color: rgb(180, 14, 180); } /* Violet */
a:active { color: yellow; }
a:focus { color: green; }
</style>
If you click on the link, you will see that it's color changed from blue/violet to green. You will not see active state, i.e. yellow.
Then, lets try to remove a:focus
from our CSS:
<style>
a:link { color: rgb(0, 138, 206); } /* Blue */
a:visited { color: rgb(180, 14, 180); } /* Violet */
a:active { color: yellow; }
</style>
Now, when we click on the link, it's active state is successfully visible. I.e., the link's color changed from blue/violet to yellow, for a 1 second.
I don't ask about difference between :active
and :focus
pseudo-classes - sure, I know it.
My question is about WHY we don't see :active
state (yellow) in the 1st example.
Simply saying, when you click on the link, both
active
andfocus
states are triggered. For that reason, if you want to see bothactive
andfocus
states,active
must be located belowfocus
:Note, that
active
must be located belowfocus
, as I already said. If you try to change the order, you will not see yellow text - it will be always green, because of overwritingfocus
overactive
. Let's show an example:Related question/answer: What is the difference between :focus and :active? (However, from my point of view, my answer is easier to understand).
Edit:
And so, returning to my original example, it was necessary just change the order of
active
andfocus
lines:There is no difference between those two examples...
The
:active
state works when you clicked on the element......
:focus
works after you clicked the element...If you see closely, when you click the
<a>
, it becomesyellow
first and then it will becomegreen
...Add some transition delay in the
:focus
...you will know the restStack Snippet