0

i am not sure if is possible to do in plain js

i am trying to add some styles to a style tag loading from a css file,

My problem is, i don't know how to read the file to transform in a string

I can't find a solution to do something like

import baseStyle from "./css/style.css"
//or
const baseStyle require("./css/style.css")

so the idea is parse in the html

        
const cssStyle = document.createElement("style");
        
cssStyle.innerHTML = baseStyle;

const doc = iframe.contentDocument;
doc.head.appendChild(cssStyle)


I am using GrapesJs, i am trying to parse some css from a framework

Thanks for the help

I try to load the css from several ways but nothing works

1 Answer 1

2

You could try using fetch to get the file as text.

Something like this:

async function setCss() {
    const baseStyle = await fetch("./css/style.css").then(res => res.text())
    
    const cssStyle = document.createElement("style");
    
    cssStyle.innerHTML = baseStyle;

    const doc = iframe.contentDocument;
    doc.head.appendChild(cssStyle)
}
Sign up to request clarification or add additional context in comments.

1 Comment

works perfect, gracias

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.