For using Cascade Style Sheet in Nodejs-Express app make sure the following is done
- Create assets folder
- place the style sheet inside the folder
Express and the static
In order to use the style sheet with ejs views ( which is a templating system for Express app. Every ejs template is similar to HTML file except optionally it can avoid the tag ( <,>) operator and it carries a .ejs extension), declare the middle ware as follows
app.use('/assets',express.static(assets))
In Ejs template
in a ejs Template we can use the css as follows
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Minimal Blog Engine</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/assets/style.css">
</head>
<body>
<h1>EJS Template</H1>
</body>
</html>
and it will work.
These EJS posts may also help you
- How to use css in Nodejs-Express project - Learn how to use CSS/Bootstrap in Express-EJS templates
- Reusable partial EJS-templates in Express - Learn how to build web/static-blog app with Express and EJS reusable /partial templating system
- How to flash a message in Nodejs-Express-EJS app - How to add a message-flashing in a Nodejs-Express app and render the flashed messages in EJS template