How to dynamically set custom CSS properties in LWC?

Hi! 🙂

Imagine, as a developer, you’ve a use case where you’re developing a custom modal LWC component and you’d like to make it flexible so that you can easily adjust the height of the modal.

One (and relatively the easier) way of doing this is by using in-line CSS, and here’s how it will look(somewhat)

/* JS file */
import { LightningElement, api } from 'lwc';

export default class CustomModalExample extends LightningElement {

    @api height; /* This property is exposed via the XML file. */
    
    get modalStyling() {
        return 'height:' + this.height;
    }
}

/* Html file */

<template>

    <!-- Modal Container -->
    <div style={modalStyling}>

    <!-- Modal -->       

    </div>

</template>

This’d work fine, however, using in-line CSS is NOT a best practice and it’s generally advised to use CSS file to style your component. But then, the question arises that how can you use variables in your file CSS files and set their value at runtime.

So if we refactor the code of the aforementioned example , it’d look like this.

/* CSS File */

.modal-container {
    height : var(--modalHeight, 500px);
}

/* JS File */

import { LightningElement, api } from 'lwc';

export default class CustomModalExample extends LightningElement {

    @api height; /* This property is exposed via the XML file. */

    /* A lifecycle hook that is Called after every render of the component. */
    renderedCallback() { 

        this.initCSSVariables();

        /* JFYI, use a flag if you only want to run this logic on first render of the component. */

    }

    initCSSVariables() {
        var css = this.template.host.style;
        css.setProperty('--modalHeight', this.height);
    }
}


/* Html file */

<template>

    <!-- Modal Container -->
    <div class="modal-container">

    <!-- Modal -->       

    </div>

</template>

This way you can prevent yourself from writing in-line CSS, and if you’re wondering what is var() in the CSS file, you can checkout the MDN documentation.

JFYI, this solution came from here.

Hope you find this one useful! Catch you in the next one! ✌
And thank you for being an awesome reader! Subscribe to this blog for receiving all the latest updates straight to your inbox. 🙂

4 thoughts on “How to dynamically set custom CSS properties in LWC?

  1. I recently found this trick myself, and I’ll be using it for theming. Note that setting a variable in document.body.style will affect all instances of that variable, so it may not be useful in all scenarios. To set a local copy that is scoped to your component, use this.template.host.style instead. A very simple, ugly example I wrote can be found here.

    https://webcomponents.dev/edit/7P1QXjfdUaEzCS4KpU2J/

    Like

      • It seems like this suggestions only works if you don’t have a section component anywhere in your flow screen. If a section is present, then the lwc will error in the flow screen like “Cannot read properties of undefined (reading ‘setProperty’)”

        Is there a workaround for this scenario?

        Like

      • Interesting! Ideally that shouldn’t be happening. Sections have caused issues in the past too. It’d be best if you can put this in the ‘Salesforce Automation’ group on the Trailblazer community.

        Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.