将 .css 文件添加到 ejs [英] adding .css file to ejs

查看:56
本文介绍了将 .css 文件添加到 ejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ejs 在 node.js(express) 上工作,但我无法在其中包含 .css 文件.我单独尝试了与 html-css duo 相同的事情,它工作得很好......我怎么能包括在我的 .ejs 文件中也是如此.我的 app.js 是这样的:

im working on node.js(express) with ejs and im not able to include a .css file to it.i tried the same thing seperately as a html-css duo and it worked fine...how can i include the same in my .ejs file. My app.js goes thus:

var express = require('express');
var app = express();

app.set('views', __dirname + '/views');


app.get('/', function(req, res){
  res.render('index.ejs', {
        title: 'My Site',
    nav: ['Home','About','Contact'] 
  });
});

app.get('/home', function(req, res){
  res.render('index.ejs', {
        title: 'My Site',
    nav: ['Home','About','Contact'] 
  });
});

app.get('/about', function(req, res){
  res.render('about.ejs', {
    title: 'About',
     nav: ['Home','About','Contact']
  });
});

app.get('/contact', function(req, res){
  res.render('contact.ejs', {
    title: 'Contact',
     nav: ['Home','About','Contact']
  });
});


app.listen(3000);

和 index.ejs 文件:

and the index.ejs file:

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

</head>
<body>
<div>
    <h1> <%= title %> </h1>
    <ul>
<% for (var i=0; i<nav.length;i++) {%>

<li><a href="/<%=nav[i]%>"> <%=nav[i]%> </a></li>
   &nbsp;  
<% } %>
   </ul>
</div>

<br>
<h3>Node.js</h3>
<p class='just'>Express is agnostic as to which templating language you use. Templating languages can be a hot topic of debate.Here Iam going to use Jade.</p>
<p class ='just'>Again Express is agnostic to what you use to generate your CSS-you can use vanilla CSS but for this example I'm using Stylus.
</p>
<footer>
Running on node with express and ejs
</footer>
</home>
</html>

<br><h3>Node.js</h3><p class='just'>Express 与您使用的模板语言无关.模板语言可能是一个热门话题.这里我将使用 Jade.</p><p class ='just'>同样,Express 与您用来生成 CSS 的内容无关——您可以使用普通 CSS,但在本例中,我使用的是 Stylus.</p><页脚>使用 express 和 ejs 在节点上运行</页脚></home>

style.css file:

style.css 文件:

<style type="text/css"> body { background-color: #D8D8D8;color: #444;} h1 {font-weight: bold;text-align: center;} header { padding: 50px 10px; color: #fff; font-size :15px; text-align:right;} p { margin-bottom :20px; margin-left: 20px;} footer {text-decoration: overline; margin-top: 300px} div { width:100%; background:#99CC00;position:static; top:0;left:0;} .just { text-align: center; } a:link {color:#FF0000;} /* unvisited link */ a:visited {color:#0B614B;} /* visited link */ a:hover {color:#B4045F;} /* mouse over link */ a:active {color:#0000FF;} ul { list-style-type:none; margin:0; padding:0;text-align: right; } li { display:inline; } </style>


推荐答案

您的问题实际上并非特定于 ejs.

2 things to note here

这里需要注意的 2 件事

  1. style.css is an external css file. So you dont need style tags inside that file. It should only contain the css.

  1. style.css 是一个外部 css 文件.所以你不需要该文件中的样式标签.它应该只包含 css.

In your express app, you have to mention the public directory from which you are serving the static files. Like css/js/image

在您的 express 应用程序中,您必须提及提供静态文件的公共目录.像 css/js/image

it can be done by

可以通过

assuming you put the css files in public folder from in your app root.
now you have to refer to the css files in your tamplate files,
like

假设您将 css 文件放在应用根目录的公共文件夹中.现在你必须参考你的模板文件中的 css 文件,喜欢

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

Here i assume you have put the css file in css folder inside your public folder.

这里我假设您已将 css 文件放在公共文件夹内的 css 文件夹中.

So folder structure would be

所以文件夹结构是

这篇关于将 .css 文件添加到 ejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆