ToDo
ToDo

Reputation: 782

Angular 2 - PrimeNg style not working

I've followed the instruction to install primeng by running npm install primeng --save then importing what I need in the app.module.ts file, for example:

import {CheckboxModule} from 'primeng/primeng';  

...

imports: [
    CheckboxModule,
  ],...

I then add the style sheets to the index.html file:

<head>
  ...
    <link rel="stylesheet" type="text/css" href="../node_modules/primeng/resources/themes/omega/theme.css" />
    <link rel="stylesheet" type="text/css" href="../node_modules/primeng/resources/primeng.min.css" />
    <link rel="stylesheet" type="text/css" href="../node_modules/font-awesome/css/font-awesome.min.css" />
</head>

My IDE can find the file location (by holding ctrl and clicking on the href value) but it is not found by the browser (404 error).

I've copied the checkbox example (http://www.primefaces.org/primeng/#/checkbox) and added it to one of my components but the style is the same as a normal checkbox.

Also, no other errors are thrown.

Should the styles be added to another file? I'm not sure why it's not working.

Upvotes: 17

Views: 63616

Answers (8)

dbgupta
dbgupta

Reputation: 149

Add @imports in styles.css file

@import "~primeicons/primeicons.css";
@import "~primeflex/primeflex.css";
@import "~primeng/resources/themes/fluent-light/theme.css";
@import "~primeng/resources/primeng.min.css";
@import "~font-awesome/css/font-awesome.css";

for other themes, go to nodemoules/primeng/resources/themes and select themes

Upvotes: 0

akhouri
akhouri

Reputation: 3165

A lot has changed since the question was first asked (in 2017) wrt the way themes (free and commercial) are available with primeng. This is updated answer for anyone facing a similar issue as above in 2020. (update valid for "primeng": "^10.0.0-rc.2" and angular v~10.0.6)

Essentially there are three ways of importing free primeng themes in an angular 2+ application.

  • add the primeng imports to angular.json styles block
  "styles": [
              "src/styles.scss",
              "node_modules/primeicons/primeicons.css",
              "node_modules/primeng/resources/themes/saga-blue/theme.css",
              "node_modules/primeng/resources/primeng.min.css"
            ],
  • add primeng import to src/app/styles.scss file
@import url("../node_modules/primeicons/primeicons.css");
@import url("../node_modules/primeng/resources/themes/saga-orange/theme.css");
@import url("../node_modules/primeng/resources/primeng.min.css");
  • also you could link the styles in the html head tag but the issue there is that none of the paths from /node_modules work, so the way to make that work is to copy the styles (from say /node_modules/primeng/resources/themes/saga-purple/theme.css to an equivalent path in /assets and use that path in the link) - the possible issue why /node_modules links do not work is probably because angular compile process creates bundle files as part of the webpack workflow and does not affect the index.html in which the link is referenced to /node_module
    <link rel="stylesheet" id="theme-link" type="text/css" href="assets/themes/saga-purple/theme.css">

Upvotes: 11

Ankita
Ankita

Reputation: 149

I resolve my problem by remove rel="stylesheet" type="text/css" from index.html and also added below imports to styles.css :

@import '~primeicons/primeicons.css';
@import '~primeng/resources/themes/nova-light/theme.css';
@import '~primeng/resources/primeng.min.css';

Upvotes: 14

ChicagoJohnnyVegas
ChicagoJohnnyVegas

Reputation: 148

Just upgraded to PrimeNG 6.1.6 and got this error.

Looks like with this release the themes have stopped using theme.css in favor of theme.scss. So, you will have to reference "node_modules/primeng/resources/themes/omega/theme.scss" (instead of "theme.css") in the "styles" section of angular.json AND …

You will need to npm rebuild node-sass.

Upvotes: 2

Smaillns
Smaillns

Reputation: 3167

in the style.css file , add your imports for example :

 @import '../node_modules/primeng/resources/themes/omega/theme.css'

if you have correctly downloaded PrimeNG, it should work now

just a bit note : be sure you have imported the primeNG modules in the correct place (In fact, we import modules not components, take care;)

Upvotes: 11

ToDo
ToDo

Reputation: 782

I found a tutorial that uses PrimeNg with Angular CLI that worked for me.

I added the font-awesome.min.css stylesheet to index.html.

Then the theme I wanted (e.g. "../node_modules/primeng/resources/themes/omega/theme.css",) to angular-cli.json file in the "styles" [...] section.

Upvotes: 11

Mertcan Diken
Mertcan Diken

Reputation: 15371

What are you using for project structure if you are using cli you should add those to styles.css. In overall they should go into the bundle.

Upvotes: 1

AVJT82
AVJT82

Reputation: 73387

According to the setup instructions you should use the following:

<link rel="stylesheet" type="text/css" href="/node_modules/primeng/resources/themes/omega/theme.css" />
<link rel="stylesheet" type="text/css" href="/node_modules/primeng/resources/primeng.min.css" />
<link rel="stylesheet" type="text/css" href="YOUR_PATH/font-awesome.min.css" />

notice that the path starts with:

href="/node_modules/primeng/resources/themes/omega/theme.css" 

not:

href="../node_modules/primeng/resources/themes/omega/theme.css"

Hope this helps! :)

Upvotes: 4

Related Questions