styling pseudo element
what's wrong with this code..why does it show error?
html
css
js
18 Replies
Because
getComputedStyle()
returns a read-only object. You can't write to it.hmm..i searched a bit more.
found out u can use css variables
but yaa.. it's working now fortunately
of course you can, they inherit into pseudos just like any other child element.
If you want to change CSS styles you need to modify the base element, as you can't target pseudo elements with JS
yaa..i got that xD
to change the runtime style in JS get the element's .style property and use setProperty() on this object
getComputedStyle() is only to read what eventually affects the rendering
hmmm.. that's why we do with variables
we set variables for the child
give the value to the parent and the child just inherits
got it
yeee..i did the same..and it worked fine xD
As a proof of concept, throw this into a codepen:
HTML
CSS
JS
You'll see the box start off life red and change to blue after a second and a half.
As you said, the trick is using custom properties.
hmm..i think it would work even without declaring the variable on the .div in css
i think setProperty () will take care of that
shouldn't it?
It would, but you need something there to start with otherwise the background of the element is
none
and yaa.. it's working as intended xD
I followed what your code looked like you wanted to do
Start red, end up blue
I could have done
background: var(--clr, red);
in the ::before
selector, though, instead.but can't u just do
background : var(--clr,red);
for the before
lawl, yeppers
xD
but then, i thought what if I just use
before and after
i can cutt off 2 divs from there then
and here i did it with before and after