Skip to content
Sign upLog in
← Back to Community

How To Render CSS Files In Express HTML Files

Profile icon
ObiVibKenobi

Uhh so the question might sound confusing but--
basically, I made an express server with a few HTML files. I tried to use a CSS file on that HTML file, but I couldn't link it.... Anyone know why, and how to fix it?

Answered by EpicGamer007 [earned 5 cycles]
View Answer
Voters
Profile icon
FaustineW1
Profile icon
ObiVibKenobi
Comments
hotnewtop
Profile icon
EpicGamer007

You need to host it. do something like,

app.get('/style.css', (req, res) => { res.sendFile("path/to/your/css/file/style.css"); });

in your nodejs file

then in your html file, do, <link rel="stylesheet" href="/style.css"></link> or however you link css.

Profile icon
EpicGamer007

Alternatively, you can do express.static("./public") or the folder with your css files. then you can do. <link rel="stylesheet" href="....../go/to/your/folder/for/css/style.css"></link>

Profile icon
ObiVibKenobi

Oh thanks that’s helpful :D

@EpicGamer007

Profile icon
EpicGamer007

@ObiVibKenobi
np :)

Profile icon
ObiVibKenobi

Express.static didn’t work...

@EpicGamer007

Profile icon
EpicGamer007

@ObiVibKenobi
invite me to the repl

Profile icon
RYANTADIPARTHI

Do you mean link css files in HTML? if so, it's like this.

<link rel="stylesheet" href="style.css" type="text/css">

Profile icon
ObiVibKenobi

Mhm that's the problem. When I put it in, the CSS never works (for some reason)

@RYANTADIPARTHI

Profile icon
RYANTADIPARTHI

@ObiVibKenobi
well it should. then it's probably a bug. report it, or try it again.

Profile icon
RYANTADIPARTHI

Also, put it in the head part of the HTML.

<link rel="stylesheet" href="styles.css">

you can put the type part if you want to. i would suggest you do anyways.

Profile icon
RYANTADIPARTHI

@ObiVibKenobi
so is everything good?

Profile icon
ObiVibKenobi

Not working, but I decided to just put it into the HTML file with a <style> tag

@RYANTADIPARTHI

Profile icon
RYANTADIPARTHI

@ObiVibKenobi
ok cool.