简体   繁体   English

将 css 文件链接到 ejs 文件

[英]Link css file to ejs file

How can i link a CSS file to ejs file,I tried the below solution but not sure what i did wrong如何将 CSS 文件链接到 ejs 文件,我尝试了以下解决方案,但不确定我做错了什么

.css code: .css 代码:

    body {
      background:url('img.jpg') no-repeat center center/cover;
    }

.ejs code: .ejs 代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="/CSS/stylesheet.css" type="text/css">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Feddback Form</title>
  </head>
  <body>
    
    <form action="" method="POST" class="form">
      <h1>Store Ownership Feedback</h1>
      <div>
        <label" for="name">Problem</label>
        <input type="text" id="name" name="name" class="box"/>
      </div>
      <div>
        <label for="request">Feedback</label>
        <textarea name="request" id="request" cols="30" rows="10" class="box"></textarea>
      </div>
      
      <button type="submit">Submit</button>
    </form>
      </div>
    
  </body>
</html>

.js code: .js 代码:

const express = require("express");
const { google } = require("googleapis");

const app = express();
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname+'/views'));
app.get("/", (req, res) => {
  res.render("index");
});

Files director: .js:/public.ejs:/public/views.css:/publice/views/CSS文件目录: .js:/public.ejs:/public/views.css:/publice/views/CSS

Before the add this line link rel="stylesheet" href="{{asset('css/style.css')}}" type="text/css"在添加此行链接之前 rel="stylesheet" href="{{asset('css/style.css')}}" type="text/css"

(Your style.css need to be in "public<css") (您的 style.css 需要在“public<css”中)

The generally recommended structure is that the main.js file should be in the root section and the css files should be in the public section.一般推荐的结构是main.js文件应该在root部分,css文件应该在public部分。 Below, I am sharing an example structure with the aim that it may help.下面,我分享一个示例结构,目的是它可能会有所帮助。

Directory Structure: Directory Structure目录结构:目录结构

Forwarding Static: public static转发 Static:公共 static

Importing css on header.ejs: Import css file在 header.ejs 上导入 css:导入 css 文件

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

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