7

Quick Tip: Style Pseudo-elements with Javascript Using Custom Properties

 3 years ago
source link: https://css-irl.info/quick-tip-style-pseudo-elements-with-javascript-using-custom-properties/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Quick Tip: Style Pseudo-elements with Javascript Using Custom Properties

Sun Mar 28 2021

In Javascript we have a few ways of selecting elements, but we can’t directly target pseudo-elements. Something like this, for instance, won’t work, and will return null:

document.querySelector('.my-element::after')

That can make it tricky to apply styles to pseudo-elements with JS. If we want to dynamically calculate the height of an element and apply it with JS, we can do it like so:

const element = document.querySelector('.my-element')

element.style.height = someFunctionToCalculateHeight()

But how do we do this with a pseudo-element? Luckily, CSS custom properties can help.

We can instead assign a custom property for the height in our CSS:

.my-element::after {
height: var(--height, 0);
}

Here I’m providing a default (or fallback) value of 0 for the custom property. It’s a good idea to provide a default if we’re relying on the custom property being defined in JS (if we want it to be anything other than the property’s default value).

Then we can set the value in JS:

const element = document.querySelector('.my-element')

element.style.setProperty('--height', someFunctionToCalculateHeight())

Here’s a simple demo (and yes, I know the JS in this example is unecessary as it could easily be achieved with CSS alone — it’s merely to illustrate the concept!):


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK