|
3 | 3 | [Bootstrap](http://getbootstrap.com/) is a popular CSS framework which can be used within an Angular project.
|
4 | 4 | This guide will walk you through adding bootstrap to your Angular CLI project and configuring it to use bootstrap.
|
5 | 5 |
|
| 6 | +## Using CSS |
| 7 | + |
| 8 | +### Getting Started |
| 9 | + |
6 | 10 | Create a new project and navigate into the project
|
| 11 | + |
7 | 12 | ```
|
8 | 13 | ng new my-app
|
9 | 14 | cd my-app
|
10 | 15 | ```
|
11 | 16 |
|
| 17 | +### Installing Bootstrap |
| 18 | + |
12 | 19 | With the new project created and ready you will next need to install bootstrap to your project as a dependency.
|
13 | 20 | Using the `--save` option the dependency will be saved in package.json
|
14 |
| -``` |
15 |
| -// version 3.x |
| 21 | + |
| 22 | +```sh |
| 23 | +# version 3.x |
16 | 24 | npm install bootstrap --save
|
17 | 25 |
|
18 |
| -// version 4.x |
| 26 | +# version 4.x |
19 | 27 | npm install bootstrap@next --save
|
20 | 28 | ```
|
21 | 29 |
|
| 30 | +### Configuring Project |
| 31 | + |
22 | 32 | Now that the project is set up it must be configured to include the bootstrap CSS.
|
23 | 33 |
|
24 |
| -Open the file `angular-cli.json` from the root of your project. |
25 |
| -Under the property `apps` the first item in that array is the default application. |
26 |
| -There is a property `styles` which allows external global styles to be applied to your application. |
27 |
| -Specify the path to `bootstrap.min.css` |
28 |
| -``` |
29 |
| -// version 3.x and 4.x |
30 |
| -"../node_modules/bootstrap/dist/css/bootstrap.min.css" |
| 34 | +- Open the file `angular-cli.json` from the root of your project. |
| 35 | +- Under the property `apps` the first item in that array is the default application. |
| 36 | +- There is a property `styles` which allows external global styles to be applied to your application. |
| 37 | +- Specify the path to `bootstrap.min.css` |
| 38 | + |
| 39 | + It should look like the following when you are done: |
| 40 | + ```json |
| 41 | + "styles": [ |
| 42 | + "../node_modules/bootstrap/dist/css/bootstrap.min.css", |
| 43 | + "styles.css" |
| 44 | + ], |
| 45 | + ``` |
| 46 | + |
| 47 | +**Note:** When you make changes to `angular-cli.json` you will need to re-start `ng serve` to pick up configuration changes. |
| 48 | + |
| 49 | +### Testing Project |
| 50 | + |
| 51 | +Open `app.component.html` and add the following markup: |
| 52 | + |
| 53 | +```html |
| 54 | +<button class="btn btn-primary">Test Button</button> |
31 | 55 | ```
|
32 | 56 |
|
33 | 57 | With the application configured, run `ng serve` to run your application in develop mode.
|
34 | 58 | In your browser navigate to the application `localhost:4200`.
|
35 |
| -Make a change to your application to ensure that the CSS library has been included. |
36 |
| -A quick test is to add the following markup to `app.component.html` |
| 59 | +Verify the bootstrap styled button appears. |
| 60 | + |
| 61 | +## Using SASS |
| 62 | + |
| 63 | +### Getting Started |
| 64 | + |
| 65 | +Create a new project and navigate into the project |
| 66 | + |
37 | 67 | ```
|
38 |
| -<button class="btn btn-primary">Test Button</button> |
| 68 | +ng new my-app --style=scss |
| 69 | +cd my-app |
39 | 70 | ```
|
40 |
| -After saving this file, return to the browser to see the bootstrap styled button. |
41 | 71 |
|
42 |
| -## Using SASS |
43 |
| -Open `angular-cli.json` change reference of `styles.css` to `styles.scss` and rename the file accordingly. |
| 72 | +### Installing Bootstrap |
| 73 | + |
| 74 | +```sh |
| 75 | +# version 3.x |
| 76 | +npm install bootstrap-sass --save |
| 77 | + |
| 78 | +# version 4.x |
| 79 | +npm install bootstrap@next --save |
| 80 | +``` |
| 81 | + |
| 82 | +### Configuring Project |
44 | 83 |
|
45 | 84 | Create an empty file `_variables.scss` in `src/`.
|
46 | 85 |
|
47 |
| -In `styles.scss` add the following: |
| 86 | +If you are using `bootstrap-sass`, add the following to `_variables.scss`: |
48 | 87 |
|
| 88 | +```sass |
| 89 | +$icon-font-path: '../node_modules/bootstrap-sass/assets/fonts/bootstrap/'; |
49 | 90 | ```
|
50 |
| -@import '_variables'; |
| 91 | + |
| 92 | +In `styles.scss` add the following: |
| 93 | + |
| 94 | +```sass |
| 95 | +// version 3 |
| 96 | +@import 'variables'; |
| 97 | +@import '../node_modules/bootstrap-sass/assets/stylesheets/_bootstrap'; |
| 98 | +
|
| 99 | +// version 4 |
| 100 | +@import 'variables'; |
51 | 101 | @import '../node_modules/bootstrap/scss/bootstrap';
|
52 | 102 | ```
|
53 | 103 |
|
54 |
| -Open `app.component.html` and add the following markup |
55 |
| -``` |
| 104 | +### Testing Project |
| 105 | + |
| 106 | +Open `app.component.html` and add the following markup: |
| 107 | + |
| 108 | +```html |
56 | 109 | <button class="btn btn-primary">Test Button</button>
|
57 | 110 | ```
|
| 111 | + |
58 | 112 | With the application configured, run `ng serve` to run your application in develop mode.
|
59 | 113 | In your browser navigate to the application `localhost:4200`.
|
60 | 114 | Verify the bootstrap styled button appears.
|
61 | 115 | To ensure your variables are used open `_variables.scss` and add the following:
|
| 116 | + |
| 117 | +```sass |
| 118 | +$brand-primary: red; |
62 | 119 | ```
|
63 |
| -$body-color: red; |
64 |
| -``` |
65 |
| -Return the browser to see the font color changed. |
66 | 120 |
|
67 |
| -**Note:** When you make changes to `angular-cli.json` you will need to re-start `ng serve` to pick up configuration changes. |
| 121 | +Return the browser to see the font color changed. |
0 commit comments