Bootstrap Glyphicons not displaying in angular2 - css

I am trying to use bootstrap glyphicons in my angular2 app. I have installed bootstrap using the command npm install bootstrap --save
My code snippet is
<button *ngIf="pos.name == uname" type="button" class="btn btn-default btn-sm delete" (click)="DeleteBulletin();">
<span class="glyphicon glyphicon-trash glyph"></span> Delete
</button>
I have included bootstrap in my styleUrls-
styleUrls: ['node_modules/bootstrap/dist/css/bootstrap.min.css', 'app/home.component.css']
The glyphicon appears as shown-

Boostrap 4 does not support Glyphicons anymore, you can use Font Awesome instead:
npm install --save fortawesome/fontawesome-free
and add the css File to your .angular-cli.json
"apps": [
{
....
"styles": [
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.css",
"../node_modules/font-awesome/css/font-awesome.css"
],
...
}
]
],
Replace CSS class with the Font Awesome classes:
<i class="navbar-toggler-icon fa fa-bars"> </i>
recompile app: ng serve
Update:
Since Font Font-Awesome 5 and Angular 5 there is an offical angular-fontawesome package available:
https://github.com/FortAwesome/angular-fontawesome#installation

You have to import
<link data-require="bootstrap-css#3.3.6" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
to your index.html

I installed bootstrap3 as npm install bootstrap#3 and it started working
just need to add in angular-cli.json:-
"styles": [
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.css"
],

There is angular-cli-build.js file of root of the project if your application has been scaffold by angular-cli (using 'ng init' or 'ng new').
angular-cli-build.js instructs build of the project (using 'ng serve' or 'ng build') to place 3rd-party libraries into vendor folder.
There is fonts folder, in bootstrap distribution folder, holding glyphicons files. Those bootstrap files needs to be placed into vendor folder too.
angular-cli-build.js:
// Angular-CLI build configuration
// This file lists all the node_modules files that will be used in a build
// Also see https://github.com/angular/angular-cli/wiki/3rd-party-libs
/* global require, module */
var Angular2App = require('angular-cli/lib/broccoli/angular2-app');
module.exports = function(defaults) {
return new Angular2App(defaults, {
vendorNpmFiles: [
'systemjs/dist/system-polyfills.js',
'systemjs/dist/system.src.js',
'zone.js/dist/**/*.+(js|js.map)',
'es6-shim/es6-shim.js',
'reflect-metadata/**/*.+(ts|js|js.map)',
'rxjs/**/*.+(js|js.map)',
'#angular/**/*.+(js|js.map)',
'bootstrap/dist/css/bootstrap.min.css',
'bootstrap/dist/fonts/*'
]
});
};
For completeness, setup process for bootstrap was in those few steps below:
Commnad line:
npm install bootstrap --save
index.html:
<link href="/vendor/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
Restart 'ng serve' or just rebuild with 'ng build'
Here is result of page source:

If you are using Bootstrap 4, it doesn't include glyphicon in self release.
So you coud use another icon package such as FontAwesome or installed the Glyphicons separately.
I am using FontAwesome and that dependencies in my cases:
"dependencies": {
"#angular/animations": "^5.0.0",
"#angular/common": "^5.0.0",
"#angular/compiler": "^5.0.0",
"#angular/core": "^5.0.0",
"#angular/forms": "^5.0.0",
"#angular/http": "^5.0.0",
"#angular/platform-browser": "^5.0.0",
"#angular/platform-browser-dynamic": "^5.0.0",
"#angular/router": "^5.0.0",
"#ng-bootstrap/ng-bootstrap": "1.0.0-beta.9",
"bootstrap": "4.0.0",
"core-js": "^2.4.1",
"font-awesome": "^4.7.0",
"jquery": "^3.2.1",
"popper.js": "^1.12.9",
"rxjs": "^5.5.2",
"zone.js": "^0.8.14"
},
"devDependencies": {
"#angular/cli": "^1.6.5",
"#angular/compiler-cli": "^5.0.0",
"#angular/language-service": "^5.0.0",
"#types/jasmine": "~2.5.53",
"#types/jasminewd2": "~2.0.2",
"#types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~3.2.0",
"tslint": "~5.7.0",
"typescript": "~2.4.2"
}
In HTML templates just place a code like that:
<i class="fa fa-refresh" aria-hidden="true"></i>

if you are using bootstrap 4 glyphicon is no longer supported you should try an old version

if you include bootstrap files through styleUrls this will make an issue, I don't know why, but I found that the borwser looks at different location for glyphicons.
#Component({
moduleId: module.id,
selector: 'app-projects',
templateUrl: 'projects.component.html',
styleUrls: ['projects.component.css','../../vendor/bootstrap/dist/css/bootstrap.min.css'],
providers:[ProjectsService]
})
using above method for calling bootstrap stylesheet file causes following error
"GET http://localhost:4200/fonts/glyphicons-halflings-regular.woff "
what you can see from the error is that the browser is looking at localhost:4200/fonts/... where it should be localhost:4200/vendor/bootstrap/dist/fonts/...
anyway, a workaround is to make sure that you added the dist folder of bootstrap inside angular-cli-build.js file in vendorNpmFiles array as 'bootstrap/dist/**/*.+(js|css|eot|ttf|woff|woff2)' and remove bootstrap stylesheet from styleUrls.
then add to the index.html the following line:
<link href="/vendor/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
dont forget to ng serve or ng build

Please run
npm install glyphicons --save
and than please import glyphicons in your component
import icons from 'glyphicons';
var icons = require('glyphicons');
console.info('This is' + icons.heart+ ' Glyphicons!')
You can check once in your browser console.

The advice with FontAwesome is correct but only partially. If you just follow the FontAwesome instructions you will fail in an Angular project.
Instead check the hint on this page:
Font Awesome now has an official Angular component that’s available
for all who want to easily use our icons in projects. If you are using
Angular, you need the angular-fontawesome package or Web Fonts with
CSS.
Okay this means in an Angular project you need to use the following package: Angular FontAwesome
The best page that I have found for me as I am using NPM was this page.
So basically you need to:
Install FontAwesome in NPM
Import the module AngularFontAwesomeModule in your app.module.ts
Add the CSS in the angular-cli.json file
The details are all described in the link above.
To finally add an icon you can add a power-off icon in the app.component.html file:
Code:
<fa-icon [icon]="faPowerOff"></fa-icon>
AND you need to add the code in the related app.component.ts file:
Code:
import { faPowerOff } from '#fortawesome/free-solid-svg-icons';
...
export class AppComponent implements OnInit {
faPowerOff = faPowerOff;
...
ONE IMPORTANT NOTE!
Please realize you need to install the correct version of AngularFontawesome in NPM for your Angular version. Check this page for the compatibility table.
I used Angular 5 so I had to install it like this:
npm install #fortawesome/angular-fontawesome#0.1.1
The entry will then be added to your package.json file.

I did the same as #Sebastian Viereck . But i did
npm install --save-dev #fortawesome/fontawesome-free
and on the angular.json
"styles": ["node_modules/bootstrap/dist/css/bootstrap.css",
"node_modules/#fortawesome/fontawesome-free/css/all.min.css",
"src/styles.css"
],
It looks that fontawesome has a new paid version

From https://www.w3schools.com/bootstrap4/bootstrap_icons.asp -
Bootstrap 4 does not have its own icon library (Glyphicons from bootstrap 3 are not supported in BS4). However, there are many free icon libraries to choose from, such as Font Awesome and Google Material Design Icons. To use Font Awesome icons, follow these steps:
Install Font-Awesome from the terminal in your project folder
npm install --save font-awesome
and add the following line to your main stylesheet -
#import "~font-awesome/css/font-awesome.css";
You can now access all the icons available in the installed Font-Awesome library. Refer this link for the complete list of icons - Official Font Awesome Website

Related

How to configure minimum development environment for Tailwindcss v3 to utilize #import

The goal is to configure a development environment for Tailwindcss v3 that supports #import and the removal of unused custom CSS classes.
I am not using a bundler. The project depends on just HTML, CSS, and JS i.e. no frameworks. If it's important, I use VS Code.
This is what I've tried.
Project's configuration:
// tailwind.config.js
const defaultTheme = require('tailwindcss/defaultTheme');
module.exports = {
content: [
'./src/**/*.{html,js}',
],
darkMode: 'media',
theme: {
extend: {
fontFamily: {
primary: '"Inter", sans-serif',
mono: ['"DM Mono"', ...defaultTheme.fontFamily.mono]
}
},
},
variants: {
extend: {}
},
plugins: [
// ...
]
};
// postcss.config.js
module.exports = {
plugins: [
require('postcss-import'),
require('tailwindcss'),
require('autoprefixer'),
require('cssnano')
]
/* styles.css*/
#import "tailwindcss/base";
#import "./custom-base-styles.css";
#import "tailwindcss/components";
#import "./custom-components.css";
#import "tailwindcss/utilities";
#import "./custom-utilities.css";
And this is my commend line:
$ npx tailwindcss -i ./src/css/styles.css -o ./css/styles.css --watch
Running just the above command, the setup does not inline the custom*.css files.
To achieve inlining, I also have to execute below, which on the surface, seems as if it should not be necessary.
$ npx postcss-cli './src/css/styles.css' -o './css/styles.css' -w
The end result is, Tailwindcss is not removing my unused custom styles, even when wrapped in the required #layer {}.
Step 5 here (Using PostCSS) just says to start your build process and does not offer any details.
Step 4 here (Tailwind CLI) only says to start the Tailwind CLI process.
So, what am I missing?
Do I need to use a bundler? If so, my preferred one is Rollup.
Other details:
autoprefixer:^10.4.0
cssnano:^5.0.10
postcss-cli:^9.0.2
postcss-import:^14.0.2
tailwindcss: "^3.0.7
Your intuition is correct. You do not need to run post-cli. PostCSS will execute once you update your Tailwind command.
You are missing the --postcss parameter.
Assuming that postcss.config.js is in your project's root, copy this to the script section of your package.json:
"tw": "tailwindcss -i ./src/css/styles.css -o ./css/styles.css --postcss postcss.config.js --watch"
Then from the command line at the project's root, run:
npm run tw
Note: with the above command, Tailwind will not rebuild the output file after the HTML file has been saved. You'll need to edit and save one of the CSS source files to initiate a rebuild. (Perhaps I still have a configuration problem?)
One other item, how are you testing for the removal of unused custom classes?
The mistake I made was just commenting out the HTML utilizing the custom class and then looking at the CSS output to see if the class was removed. But Tailwind (as documented somewhere) won't remove a class if the class appears anywhere in the markup, even in a commented line. If you're testing your build process, rename the class in the markup to anything, and then Tailwind will drop the custom class from the CSS output.

Using tailwindCss vs3.0 issue

I'm trying to learn tailwindCss but the documentation shows the latest version
https://tailwindcss.com/docs/installation
which I followed to set up my first project, but the issue here is when I apply the utility class to my project it's not working. Here is my code
<body>
<h1 class="text-lime-400">Hello World of Joy </h1>
<hr>
</body>
here is the link to the generated styles by tailwindcss
<link rel="stylesheet" href="/public/styles.css">
Assalamu'Alaikum. Thank you for your question. As you are beginning with tailwind I suggest you to use CDN first. In Installation you will find Play CDN where you will copy only
<script src="https://cdn.tailwindcss.com"></script>
and paste in your head section of HTML.
after that all tailwind css code will run.
But if you wish to use it by installing then i suppose you got a package.json file. where you should write a script to build your css. and you need to build css. if you change anything in tailwind.config.js then you will need to rebuild the css again to effect the changed variants.
{
"name": "dims-web-1.1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build-css": "tailwindcss build src/style.css -o public/styles.css"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"tailwindcss": "^2.2.19"
}
}
here in scripts "build-css" is my custom code to build css. to build the css.
in terminal you need to write
npm run build-css
and hit enter to get your css in your public.css
then tailwind utility classes will work fine.
Thank You

Unable to use external css stylesheet in angular 9 project

I'm new to angular and was trying to implement something with W3 CSS. I was trying to add this css to my project. Below are the steps I have followed
Ran npm install --save w3-css in my project directory
Added node_modules/w3-css/w3.css in angular.json
"styles": [
"src/styles.css",
"node_modules/w3-css/w3.css"
],
After that I tried adding css classes to my component html file
<div class="w3-container w3-border w3-large">
<div class="w3-left-align"><p>Left aligned text.</p></div>
<div class="w3-right-align"><p>Right aligned text.</p></div>
</div>
But I couldn't see any class added.
Note: node_modules folder and angular json are in same directory.
import your css in style.css file.
#import "../node_modules/w3-css/w3.css";

Bootstrap not working properly in Angular 6

I started learning Angular and I'm following a tutorial. I tried putting the code as in the tutorial for the navigation-bar:
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
Recipe book
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li> Recipe</li>
<li> Shopping list</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<ul class="dropdown-menu">
<li>Save data </li>
<li>Fetch data </li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
But all I get is this:
If I remove bootstrap, I do get the other items.
I used npm install bootstrap --save and #import "~bootstrap/dist/css/bootstrap.css"; in styles.css to use bootstrap.
EDIT:
I did not wanted to open a question with two problem, but after looking at the answers so far ill describe another problem I had:
I also installed Jquery using nmp install jquery --save and added to angular.json:
"styles": [
"node_modules/bootstrap/dist/css/bootstrap.css",
"src/styles.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/tether/dist/js/tether.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
In this case, after removing #import "~bootstrap/dist/css/bootstrap.css"; from styles.css`, bootstrap is not working at all.
The only way i got bootstrap to work is as i described in the question.
EDIT 2:
I started a new project and followed #HDJEMAI s answer instructions.
I now still get the same page as in the picture but its now working through the rows added to styles and scripts in angular.json and not through the #import "~bootstrap/dist/css/bootstrap.css" in the styles.css file . But the problem is still that is not the same as in the tutorial even though the code is the same.
This is what he gets in the tutorial:
You have to install both bootstrap and JQuery:
npm install bootstrap jquery --save
Then you will find these files in your node module folder:
node_modules/jquery/dist/jquery.min.js
node_modules/bootstrap/dist/css/bootstrap.min.css
node_modules/bootstrap/dist/js/bootstrap.min.js
Then you have to update your angular.json file:
In the architect, build section, or even test section if you want to see Bootstrap working as well when testing your application.
"styles": [
"styles.css",
"./node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"./node_modules/jquery/dist/jquery.min.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js"
],
If you do not add the jquery.min.js script Bootstrap will not work.
Another requirement is popper.js if you need Bootstrap dropdown then you will need to install it and add the script file as well
npm install popper.js
Then add this script as well
"styles": [
"styles.css",
"./node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"./node_modules/jquery/dist/jquery.min.js",
"./node_modules/popper.js/dist/popper.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js"
],
Bootstrap dropdown require Popper.js (https://popper.js.org)
If you are adding the bootstrap path into the angular.json file, make sure it is the styles within the project\architect\build. Not the one in the project\architect\test.
{
"projects": {
"architect": {
"build": {
"styles": [
"node_modules/bootstrap/dist/bootstrap.min.css",
"src/styles.css"
],
"test": {
"styles": [
"src/styles.css"
],
First uninstall bootstrap. The next step is to install bootstrap by following command
npm install --save bootstrap#3 A
next go to styles.css and paste
#import "node_modules/bootstrap/dist/css/bootstrap.css"
I had the same issue. Below helped for me.
Stop the server and recompile using
ng serve
As I see, if you are following "Maximilian Schwarzmüller" course in UDEMY, make sure you have installed Bootstrap 3, not anything else. Use these commands in your project folder (the uninstall only if other version are installed).
npm uninstall --save bootstrap
npm install --save bootstrap#3
This will install it locally to your project.
I had Bootstrap4 installed (thinking the course would still work with 4 instead of 3) and using Bootstrap3 instead worked in this same project as your in the course (without popper and jQuery).
I've had a similar issue after installing bootstrap popper.js jquery I've manually removed bootstrap popper.js jquery folders from node_modules then I've done the following:
npm i bootstrap#latest jquery popper.js --save
going back to angular.json I've made sure I've included the following scripts under projects > architect > build :
"styles": [
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
"./src/styles.scss"
],
"scripts": [
"./node_modules/jquery/dist/jquery.min.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js",
"./node_modules/popper.js/dist/popper.min.js"
],
It solved the problem for me.
On my end, I've solved this issue by adding the following into "style.css" file and it worked.
#import "~bootstrap/dist/css/bootstrap.css"
Add your angular.json
{
"$schema": "./node_modules/#angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"xtreme-admin-angular": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "#angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/xtreme-admin-angular",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"node_modules/bootstrap/dist/bootstrap.min.css"
//Your root depend this path
]
And rerun your project
Add this 3 links in your index.html file. You don't have to install bootstrap or jquery or update your angular.json file. Works as a charm.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
install npm install bootstrap#latest jquery --save
install npm install popper.js;
then go to Angular.json file (line no 47) change lines with this:
"styles": [
"src/styles.css",
"./node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"./node_modules/jquery/dist/jquery.min.js",
"./node_modules/popper.js/dist/popper.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js"
],
After installation of bootstrap and adding it to angular.json file.
If bootstrap doesn't render, just change the port of development URL:
ng serve --port <your choice of port>
I think it is because of the problem with bootstrap, try this:
Step 1.
Inside .angular-cli.json file
"styles": [
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
]
Remove "../node_modules/bootstrap/dist/css/bootstrap.min.css"
Step 2.
Go to styles.css
Import bootstrap by adding this line
#import "~bootstrap/dist/css/bootstrap.css";
These steps solved the errors I got during the building process.
Had same issues with bootstrap items using VS code editor. I tried ng build before ng serve and bootstrap items worked fine.
I’d start by checking both “styles” and “scripts” under “build” and “test” sections
Add this line in style.css and its gonna work.
#import "~bootstrap/dist/css/bootstrap.css";
Thanks
For me I had to do both, copy the node_modules jquery and bootstrap js files in angular.json
"styles": [
"src/styles.css",
"node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
]
And copy #import "~bootstrap/dist/css/bootstrap.css"; in styles.css
I have also created a project following the video. This works for me
npm uninstall --save bootstrap npm install --save bootstrap#3
then in style array added like this
"styles": [
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
"src/styles.css"
],
Please check all your .component.ts files, especially recipe.component.ts. Maybe there is CSS encapsulation tuning like
#Component({
selector: 'app-recipes',
templateUrl: './recipes.component.html',
styleUrls: ['./recipes.component.css'],
encapsulation: ViewEncapsulation.ShadowDom
})
If it is so. You should remove encapsulation property.
I had the same problem when using bootstrap in an Angular project. I correctly installed it and updated the angular.json file, but I forgot to import it into the global style sheet (styles.css). As a result, I included it in the global stylesheet. So, if your installation is correct and your angular.json is up to date, please try importing it in the global style sheet (styles.css) as shown below:
#import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
I'm hoping it works because it did for me when I had the same problem.
I check many way
"../"...
or
"./"...
or /
also I don't want to use in "style.css" because maybe want add some "js" files or "css" files that depend on "js" file
to this reason
the best way was
"styles": [
"src/styles.css",
"node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/popper.js/dist/umd/popper.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
]
BUT restart your PC not only browser or IDE

Is it possible to have both css and scss in Angular

I am including a stand-alone build module which uses .scss (SASS) into an existing Angular app set up with plain CSS. Is it possible to have these both next to each other and does it need any custom configuration for example in the angular-cli.json defaults section:
"defaults": {
"styleExt": "css? scss?"
}
Or is it only possible to use one of the two?
EDIT for Angular 6+
Found on GitHub page of Angular:
stylePreprocessorOptions is still supported. You will have to manually update the newly generated angular.json though. Its new place is inside the "options" block of the json.
An example config could look like this:
"options": {
"outputPath": "../webapp",
"index": "src/index.html",
"main": "src/main.ts",
"tsConfig": "src/tsconfig.app.json",
"polyfills": "src/polyfills.ts",
"assets": [
{
"glob": "**/*",
"input": "src/resources",
"output": "/resources"
},
],
"styles": [
"src/styles.scss"
],
"stylePreprocessorOptions": {
"includePaths": [
"src/styles"
]
},
"scripts": []
}
Note the changed path.
ORIGINAL answer
Actually it just went fine out-of-the-box by having the "styleExt" set to "css". A bunch of components in a specific module were using SASS. I have a sass folder with variables and mixins and this setting in angular-cli.json was needed to have these scss files compiled / processed correctly:
"stylePreprocessorOptions": {
"includePaths": [
"sass"
]
}
The application renders fine with a mix of css and scss files. I assume this styleExt parameter is just there for the default component style (css, scss, less) file generation when adding components via angular-cli commands.
You can have both but you have to set your style extension to SASS or CSS only.
Now of if you want to use CSS or SASS in your component just use Angular CLI to generate component like
For CSS
ng g component my-component --style=css
For SASS
ng g component my-component --style=sass
So set style extension to SASS and use CSS extensions also in your project
I would recommend you change the styleExt to scss and your css files to scss because scss is a superset of css.

Resources