TIL CSS variable tricks
POSTED ON:
TAGS: css javascript variables
This is a neat trick.
If you want to change the background with Javascript, you can use a CSS variable.
<!-- BEFORE -->
<section
class="newsletter"
style="background-image: url('/assets/ui/decorative/newsletter-lg-aj1891101.svg')">
</section>
Notice that you're using a 'hardcoded' style tag and URL.
IF you were controlling the backgound with JS, you can target the --thumb
variable directly.
Like this:
<!-- AFTER -->
<section
class="newsletter"
style="--thumb:url('/assets/ui/decorative/newsletter-lg-aj1891101.svg')">
</section>
<style>
.newsletter {
background-image: var(--thumb);
background-size: cover;
background-position: 100% 50%;
}
</style>
VIA:
Practical Use Cases For CSS Variables
Related TILs
Tagged: css