1

After the announcement yesterday of Salesforce open sourcing LWC, I've created a new project with the new npx tool on lwc.dev.

I have also added the SLDS css folder, and can view the styled components fine in the top level html file, but none of the child components get styled.

Is there another step that needs to be taken?

I understand that components should be styled individually, however duplicating common styles seems to mean writing css multiple times.

1 Answer 1

3

based on the html source it seems that the Standalone LWCs are using the real shadow dom instead of salesforce semantic one. That means it follows the normal Web Component Specification which means all styles are completely encapsulated.

Normally you are in theory able to add a link tag pointing to the stylesheet inside the template which seems to be prevented by the LWC Compiler. For now it seems to be possible to do it via Javascript but i am not sure if Salesforce will remove that Ability too.

Example:

    constructor() {

        super();
        const styles = document.createElement('link');
        styles.href = '/resources/css/main.css';
        styles.rel = 'stylesheet';
        this.template.appendChild(styles);
    }

Potentially all used Stylesheets should be also included in the main index.html as this approach would load the respective stylesheet after the dom element was already inserted.

4
  • Nice answer. It works, although time will tell if it keeps working. Commented May 30, 2019 at 13:11
  • I tried the same above approach but keep getting "its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.", I have started new thread salesforce.stackexchange.com/questions/316879/… please let me know if any workaround is available for this.
    – gs650x
    Commented Aug 15, 2020 at 16:54
  • @Renji-xD, Modifying the DOM in constructor is a bad practice in LWC [Check out: developer.salesforce.com/docs/component-library/documentation/…. Shouldn't this be done in connectedCallback or renderedCallback instead?
    – arut
    Commented Aug 16, 2020 at 6:34
  • thank you for pointing it out, yesterday I was struggling to import CSS and today I am struggling to import the jQuery, I have started a new thread, salesforce.stackexchange.com/questions/316904/… would highly appreciate your kind help.
    – gs650x
    Commented Aug 16, 2020 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .