简体   繁体   English

如何在 ejs 视图中渲染 css?

[英]How to render css in ejs views?

I have the following folder structure:我有以下文件夹结构:

|——-index.js
├── public
│   └── css
│       └── styles.css
├── routes
│   ├── index.routes.js
│   └── users.routes.js
└── views
    ├── login.ejs
    ├── profile.ejs
    └── register.ejs

In my index.js file I'm setting up the source for my stylesheets to /public :在我的index.js文件中,我将样式表的源设置为/public

app.use(express.static("public"));
app.set("view engine", "ejs");

My login router is /users/login and that endpoint is rendering my view page login.ejs .我的登录路由器是/users/login并且该端点正在呈现我的视图页面login.ejs

In login.ejs I'm importing the stylesheet as follows:login.ejs我按如下方式导入样式表:

<!-- bootstrap -->
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
      integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
      crossorigin="anonymous"
    />

<!-- stylesheet -->
    <link href="css/styles.css" type="text/css" />

Unfortunately, the css is not being rendered and I can't seem to add any styling to my view page.不幸的是, css没有被渲染,我似乎无法向我的视图页面添加任何样式。 Am I importing it incorrectly?我是否错误地导入了它? Is it a problem with the nested directory是不是嵌套目录有问题

Try importing the css like this in head tag-尝试在 head 标签中导入这样的 css-

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" href="/css/styles.css">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM