external style sheet in pug template will not load
Asked Answered
N

3

5

I am unable to figure out how to get a style sheet to load in pug. I have tried every which way I can think of. and have searched extensively on Google. This seems like such a silly problem, but I have already wasted so much time trying to figure it out.

My file structure is as follows

/main
    server.js
    package.json
    /views
        layout.pug
        main.pug
    /public
        style.css

In all of the following examples I have tried to reference the file in these ways:

  • href="../public/style.css"
  • href="./public/style.css"
  • href="/public/style.css"
  • href="public/style.css"

and

  • href="/style.css"

I have tried:


1) link element in main.pug

    //- daytime page
    extends layout.pug
    block styles
        link(rel="stylesheet" type="text/css" href="../public/style.css")

2) In the layout.pug using include

//- layout
doctype
html
  head
    meta(charset='UTF-8')
    block title
      title country
    block styles
  body
    style.
      include ../public/day.css

AS WELL AS inside the header


3) In layout.pug using

//- layout
doctype
html
  head
    meta(charset='UTF-8')
    block title
      title country
    block styles
      link(rel="stylesheet" type="text/css" href="../public/style.css")
body

Inspect Source

When I inspect the source of the pug files that contain the pug element. the browser shows that the link has been rendered correctly inside the parent.

screenshot of firefox inspect element

css does load when I write it directly in the pug file like so:

style.
    selector{
        rule
        rule
}

I can not keep my css inline, and I can not use html directly in the pug file (assignment rules). Why are my stylesheets not loading?


Specs:

  • Firefox 56.0
  • Ubuntu 16.04
  • pug 2.0
  • express 4.16.2
Noumenon answered 28/10, 2017 at 21:40 Comment(2)
Some browsers don't accept the hot load of css. You should try a .createElement('link') with a .onload eventFreeze
Did you ever figure out why? I am having the same problem :/Sawyere
H
7

Check if you have set public folder as static inside of server.js

app.use(express.static(path.join(__dirname, 'public')));

then in pug files add

head
block styles
  link(rel="stylesheet" type="text/css" href="/style.css")
Haematocele answered 4/9, 2018 at 12:36 Comment(0)
B
3
style
     include css.css

It's working for me and I hope will help you , read here https://pugjs.org/language/includes.html

Brunildabruning answered 4/10, 2019 at 4:32 Comment(0)
H
2

For pub version 2.* and higher, use the below code,

html
    head
        style
             include ./style.css
    body
Harslet answered 28/11, 2019 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.