CSS styles not being applied to view - css

I'm creating an app with node.js and express. I'm using pug as my template engine. The styles from the stylesheet are not being applied to the view.
In my header.pug file I have
link(rel='stylesheets/style.css', type='text/css', href='/stylesheets/style.css')
The style sheet is located in public/stylesheets. I've also included the following code below in my app.js file.
// serve static files from /public
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'pug');
app.set('views', __dirname + '/views');
When I run the application and expect the document I can open the stylesheet in the new tap and see the stylesheet, but it's not applying those styles to the pug file?

remove first /
also change rel atribute
like:
link(rel='stylesheet', type='text/css', href='stylesheets/style.css')

You can also try this way to load stylesheet.
index.pug
html
head
style
include style.css
title This is my first Pug file
body
div.split.left
div.centered...
Folder structure need to be like this.
views
index.pug
style.css
index.js

Related

Node.js Pug: Browser not processing CSS file

So, I have a layout.pug,an index.pug, and a login.pug.
Format of index.pug and login.pug:
extends layout
block content
//some content
This is the code in layout.pug that links the CSS file:
link(href='/stylesheets/creative.min.css', rel='stylesheet' ,type='text/css')
File structure:
public
stylesheets
creative.min.css
views
index.pug
layout.pug
login.pug
app.js
I have these lines in my app.js:
app.set('views', path.join(__dirname, './views'));
app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, './public')));
The problem is the CSS styling is being shown when I try to access index but not when I try to access login. While on the login page, I checked the page source and the CSS file was there so it's not as if it can't locate the resource (404). I've spent quite a few hours on this but couldn't get to the root of the problem. I'm a beginner in Node so any help would be appreciated. Thanks.

How to import css into Components in angular 4

I want to add css into component [Angular 4]. I try to add this into "resume.component.ts" by styleUrls, but I can't access url of assets !!!
Please help me add css this into component. Thanks all :D
you can add your css file's relative link in .angular-cli.json (or angular.json for v6) file
here:
"styles": [
// relative link
]
There are many ways of importing CSS file into Angular application.
Angular follows the component structure (angular cli) which will provide you CSS/HTML/TS/Spec.ts file per component.
So you can simply add CSS file for that particular component in #component's styleUrls property (which accept array of file names) by just adding file name , no need to add full path there.
If you want to add some global CSS in your application then you can add it into the .angular-cli.json file (as you are using angular v4).
Another way, if you want to import CSS files into another CSS then you can use simply import syntax like this using full path
#import '/assets/css/yourstyle.css';
You have to put your files and use the relative path from the assets folder.
For example in the above you've put a css file in src/assets/css/general_style.css, then
<link rel="stylesheet" href="assets/css/general_style.css">
You type wrong path. asset => assets
#import '/asset/css/main_style.css';
please change to
#import '/assets/css/main_style.css';

Angular - including CSS file in index.html

I'm trying to use the angular2-busy library in an angular project created with the CLI, but am having an issue importing the stylesheet:
<link rel="stylesheet" href="/node_modules/angular2-busy/build/style/busy.css">
The browser is telling me that it cannot find the file, even with the correct path. I also checked that the file exists, and it does. When I take out the rel="stylesheet" I don't get the error, but then the animations don't work.
Here is the package I am trying to use, if anyone is curious:
https://www.npmjs.com/package/angular2-busy
Angular CLI have it's own way to initialize your global css/js.
They are located in .angular-cli.json configuration
Locate "styles": and add your css there
Example :
"styles": [
"../node_modules/angular2-busy/build/style/busy.css",
"styles.css"
],
Hope that helps.
Basically there are three different ways to do that :-
By adding it to the "styles" array in angular-cli.json file as is shown by #penleychan in his answer.
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.min.css",
"styles.css"
]
You can directly import the css file into styles.css file (or any other css file) that is included in "styles" array in angular-cli.json file by adding the #import statement at the top of that file.
#import "~bootstrap/dist/css/bootstrap.min.css";
Include the css file in index.html page by adding a link element.
<link rel="stylesheet" type="text/css"
href="node_modules/bootstrap/dist/css/bootstrap.min.css"
/>
You can find a more elaborated explanation and a few more alternatives at this link
How to Use Angular with Linked Cascading Style Sheets (CSS)
Always use LINKED CSSS rather than the compiled and embedded JavaScript memory version of CSS Google Angular uses. Why? Linked <link> external CSS is superior in every way to embedded CSS, mainly because linked CSS is cached across thousands of page views, visits, and users online, saving you huge bandwidth values with increased CSS rendering speed in the browser, while implementing simpler, faster CSS management, overall.
HOW TO FIX ANGULAR FOR LINKED CSS
In angular.json delete all the references to CSS files under "styles". It should look like this now:
"styles": [],
Move your CSS files to the"src" folder inside your project, then add links <link> to your external CSS files inside index.html. Add in your link paths to your CSS file starting at the "src" folder and including the "styles" folder or any folder system you desire (see below). You can store your css wherever you want in your project now as long as those folders of files are under your "src" root folder. My physical CSS files in my project for the path below now sit under "src/styles". So the link path should just be my "styles" folder plus the file name:
<link href="styles/mystyles.css" rel="stylesheet" />
Any CSS files for bootstrap, font-awesome, etc. that you want in your project have to be manually copied from your "node_modules" folder in your project into a folder under your "src" folder, just like in the location used for the CSS file above in #2. Or, you can reference them from some fully qualified url online. If you want to create a link to them as above in "index.html", or import them into the html file directly (example below), that will also work. If you were importing them before from the "node_modules" folder that will not work as the Angular CLI or webpack resolved those paths by compiling your CSS imports into JavaScript. After you move those CSS files and link or import them from the src folder, they will not be compiled into Angular JavaScript now. When using #import, be sure to drop your bootstrap and font-awesome CSS files in the same "src/styles" folder as your main style sheet and import them into that stylesheet like this:
<style type="text/css">
#import "bootstrap.min.css";
#import "font-awesome.min.css";
</style>
In the same angular.json file above, under the "assets" JSON setting, add a reference to the location of your CSS files in #2 and #3 so the builder can copy them into your dist folder. Any CSS files linked or imported from that folder will get moved by the "dist" folder system when Angular is compiled. Note the new styles path at the bottom. If you have CSS in other folders you can add them here as well. This tells the builder to create the CSS directories in the "dist" folder Angular uses and copy all the CSS files inside them, so when you build for production your index.html links point to the right CSS files on the server:
"assets": [
"src/favicon.ico",
"src/assets",
"src/api",
"src/styles"
],
You now have a powerful set of link elements to all your CSS in the head of your index.html file and can edit them in the Angular project like you normally do, knowing they will work in both the Angular development test server and in your dist production copy. Your website will also benefit from browser caching of CSS one time in memory and permanent file caches.
It took me a day to dig through documentation and testing to figure out what should have been a natural part of any simple website API with linked CSS. I'm sorry Google Angular made this so convoluted. But this change works great!
This simply removes your CSS from the compile and build angular system that pushes all your CSS into a JavaScript file, which simply embedded your CSS into an inline style sheet block in the memory of your browser and head of your HTML page. Using your own linked CSS html tags is far superior and allows better caching and control of CSS cascade rules.
Good Luck!
Try
<link rel="stylesheet" href="node_modules/angular2-busy/build/style/busy.css" >
You are missing the self closing / at the end of your code. It's possible the browser is not fixing this for you.
<link rel="stylesheet" href="/node_modules/angular2-busy/build/style/busy.css" />
Also removing rel="stylesheet" would definitely not fix the problem since the browser needs to know exactly what kind reference you are referring to.
If fixing the closing tag does not work then your path is wrong. You can also try adding a ../ to the beginning of your path. This will make it relative to the folder the site is in.
<link rel="stylesheet" href="../node_modules/angular2-busy/build/style/busy.css" />

Import external file content into handlebar

I hope someone can give me a hint.
I would like to import content from one file into my handlebar file. Is it possible?
In my case, it is an css/scss file (e.g. reset.css) which stylings I want to import into my handlebar file (styleReset.hbs).
The "styleReset.hbs" should looks kind of like this:
<style type="text/css">
<!-- import of reset.css content -->
</style>
P.S. I don't want use the -tag
Yes, it is possible to import the external css file into your handlebars .hbs file (i.e- Template engine).
Follow these steps :
First create a folder public under which place your css
folder, which content all your css files. For ex folder structure would be - public/css/style.css (Note: This public folder contains all your static files like css, images, etc)
Register your public folder to express in your .js file by app.use(express.static(__dirname + '/public'));
Now you can import external css file in handlerbars template file
by <link rel="stylesheet" href="../css/style.css">
You can't import files with handlebars, only partials. You could precompile your reset.css as if it was a handlebars partial and include that with {{> filename}}.
Without knowing your build setup I don't think I can go into more detail.
http://handlebarsjs.com/precompilation.html
(Personally I'd use sass to import my reset.css to some main stylesheet that I include in the page.)
It is also possible to have a 'main' layout too, that can include header and footer.
app.engine('.hbs', exphbs({
extname: '.hbs',
defaultLayout: 'main'
}))
app.set('view engine', '.hbs')
Also, if you are using the module, "express-handlebars"(not "hbs"). You can set your extension name too.
1)In order to use your .css file in handlebars, file should be registered for use in app.js/server.js file shown bellow.
app.use("/bootstrap",express.static(__dirname+"/node_modules/bootstrap/dist"))
2)import the file in your handlebar file as given bellow image(it works for both main layout and child layout.
i) In Main Layout File
ii) In Child Layout
index.js
public:
style.css
views:
index.hbs
Inside index.js
var express=require('express');
var app=express();
var hbs = require('hbs');
app.set('view engine', 'hbs');
app.use(express.static('.'));
Inside index.hbs
<head>
<link href="./public/style.css" rel="stylesheet">
</head>

How to handle CSS with meteor?

I am building a test app to learn how to organize multiple files with METEOR.
I have a head.html and inside I have the following link to my custom CSS:
<!-- Custom CSS -->
<link type="text/css" rel="stylesheet" href="/stylesheets/globals/style.css"/>
Very normal, Yet I have trouble to make that working.
Here is my app directory:
-app folder
---client
-----head.html
-----index.html
-----stylesheets
-------globals
---------style.css
I know it seems to be a very basic question but I can not figure it out.
Basically you have 2 ways of inserting CSS in a Meteor project :
Using the Meteor build tool to automatically concatenate and minify all your CSS files living in the client/ directory : in this case you don't need to import your stylesheets using a link tag in the head. This is perfect for vital CSS files that your app should load when started.
Example : put your CSS file under client/stylesheets/globals/style.css and that's it, no need to import it, it's automatically injected in your project by Meteor.
Using the classic way of importing stylesheets in a web application : you can put your CSS files inside the public/ directory and they will be served by your app server. In this case the Meteor build process will be skipped so files won't be concatenated together nor minified. Use this method when you want to lazy load big CSS files only needed in a subpart of your app (for example admin section styling).
Example : put your minified CSS file under public/stylesheets/admin/style.css, and use something like iron:router to load the CSS file when hitting the admin route.
Router.route("/admin", {
// onRun hooks executed only once
onRun: function(){
// create a link taf holding a reference to our publicly served CSS file
var link=$("<link>",{
rel: "stylesheet",
href: "/stylesheets/admin/style.css"
});
// append to the head tag
$("head").append(link);
}
});

Resources