When I try to set the color of the link element, using the class name like this :
var link = document.createElement('a');
link.classList.add('f');
.f {
color: white !important;
}
The link still remains blue
but when I apply it generally using the anchor tag
a {
color: white !important;
}
the link text is turned to white, why is that ? What should I do ?
I wanted the link color of my anchor tag to be displayed white, but when I style it with my classname it gets displayed blue, but if I use the general anchor tag the style is applied
4
Answers
Here You Go
The problem here is the ‘a’ attribute is not getting the class value.
So instead we can use the following logic to set the class value to ‘a’ tag:
Also no need to add
!important
anymoreFirstly, you should always ignore
!important
it should not be used frequently until it is important, as it gives element the highest specificity and makesCSS
harder to maintain. and in this codeyou are only creating an element but it is not made in
DOM
you should append it in the body first like thisNow your anchor is added in the
DOM
with class first it was not added. Now yourCSS
using class selector will work.