-2
I was mounting a screen using css and html, and came across the following situation:
class "x" is added with a js, when the user rolls the sidebar (if the position is greater than 0, class x is added to the element).
in my css I needed to change the background color, so I put:
elemento .x{
codigo css...
}
and it didn’t work, but when I put it like this:
elemento.x{
codigo css...
}
It worked. For those who saw no difference, it is in the space between "element" and ". x".
NOTE: I have some css code in which I put space and it works, but these are not added with js.
Why does this space make it difficult in some situations and not in others? What’s the difference?
Script that adds class:
<script type="text/javascript">
window.addEventListener("scroll", function(){
var header = document.querySelector("header");
header.classList.toggle("x", window.scrollY > 0);
});
</script>
Do you have the js function that adds the class? When there is space they are going through a path to the element, when they are together means the same element. Spaceless
<elemento class="x" />
Roomy<elemento> <p class="x"></p> </elemento>
– Pedro Henrique
@Pedrohenrique I have yes, I will leave the js in the question. Thank you for your answer
– SaMuK
simple, when it has space it looks for the child of the element with class x, when this without space css looks for the element with class x
– Lennon S. Bueno
@Samuk your css selector is
header.x
?– Pedro Henrique
@Pedrohenrique Yes, exactly.
– SaMuK
@Samuk then is right, when you use
header.x
, stylization is applied to the elementheader
with classx
.– Pedro Henrique