Rustc: no method named `head` found for struct `Document` in the current scope method not found in `Document`

Hello everyone, i trying use web_sys for put a CSS tag in HEAD of document.
But, the compiler find body() but not find head() tag in document.
Anybody have an idea about to how solve this problem?

rustc: no method named 'head' found for struct 'Document' in the current scope
method not found in 'Document'

pub fn set_css() -> Result<(), JsValue> {
        let window = web_sys::window().unwrap();
        let document = window.document().unwrap();

        let style: HtmlStyleElement = document.create_element("style")?.dyn_into()?;
        style.set_type("text/css");

        let css = "
            body {
                background-color: lightblue;
                font-family: Arial, sans-serif;
            }
            h1 {
                color: darkblue;
            }
        ";
        style.set_inner_html(css);

        let h1 = document.create_element("h1")?;
        h1.set_inner_html("Hello World, this a sample!");
        document.body().unwrap().append_child(&h1)?;

        let doc: web_sys::Document = web_sys::window().unwrap().document().unwrap();
        doc.head().unwrap().append_child(&style)?; // This doesn't work
        doc.body().unwrap().append_child(&style)?; // This work
        Ok(())
    }

´´´

https://docs.rs/web-sys/latest/web_sys/struct.Document.html#method.head

This API requires the following crate features to be activated: Document, HtmlHeadElement

1 Like