16

I tried using a mixin in my styles.scss but it says it is not defined. I've tried:

  1. Using it in styles.scss
@include media-breakpoint-up(sm) {
  .some-class {
    display: block;
  }
}

result:

@include media-breakpoint-up(sm) {
        ^
      No mixin named media-breakpoint-up
  1. importing bootstrap again:

    @import "../node_modules/bootstrap/scss/bootstrap.scss";

Now it works but bootstrap is included twice if I look in styles.bundle.js. Bootstrap is already included in angular-cli.json.

chrisly
  • 55
  • 6
gyozo kudor
  • 6,284
  • 10
  • 53
  • 80

6 Answers6

23

I've the same problem with all components in theirs own folder projects/<prj>/src/app/navbar/navbar.scss:

and I've fixed temporary adding manually

   @import '~bootstrap/scss/functions';
   @import '~bootstrap/scss/variables';
   @import '~bootstrap/scss/mixins';

I know for sure that isn't beautiful but resolve it ...

My packages versions below

Angular CLI: 7.0.6
Node: 9.10.1
OS: darwin x64
Angular: 7.0.4
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.10.6
@angular-devkit/build-angular      0.10.6
@angular-devkit/build-ng-packagr   0.10.7
@angular-devkit/build-optimizer    0.10.6
@angular-devkit/build-webpack      0.10.6
@angular-devkit/core               7.0.6
@angular-devkit/schematics         7.0.6
@angular/cli                       7.0.6
@angular/fire                      5.1.0
@ngtools/json-schema               1.1.0
@ngtools/webpack                   7.0.6
@schematics/angular                7.0.6
@schematics/update                 0.10.6
ng-packagr                         4.4.5
rxjs                               6.3.3
typescript                         3.1.6
webpack                            4.19.1
chrisly
  • 55
  • 6
miticojo
  • 357
  • 1
  • 2
  • 5
11

Rather than importing all of bootstrap again you actually just want the mixins file and variables file:

@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";

These files have no actual CSS in them - only SCSS variables and mixins.

UncleDave
  • 6,872
  • 1
  • 26
  • 44
6

There are two ways of doing this. Basically, both the ways are same, they are just different in syntax. So at the top of your SCSS file, use the following line:

@import "~bootstrap/scss/bootstrap-grid.scss";

OR

@import "../../node_modules/bootstrap/scss/bootstrap-grid.scss";

Here is a complete example:

main.scss

@import "~bootstrap/scss/bootstrap-grid.scss

.test {
  background-color: yellow;
}
@include media-breakpoint-up(md) {
  .test {
    background-color: red;
  }
}

HTML code:

<div class="test">
  This div should have yellow background on mobile and red background in medium devices and up.
</div>

NOTE: This assumes that you already have Bootstrap in your list of dependencies in package.json file. If not, then you can add the following line in it and you may change the Bootstrap version as you see fit.

"bootstrap": "4.1.3"

Devner
  • 6,825
  • 11
  • 63
  • 104
2

You can import mixins to use in scss files by:

@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";

Also, if you try to import only mixins, it might give you an error.

0

if you are using sass you can just import bootstrap directly into your styles.scss no need to add it to the angular-cli.json file.

More info -> https://github.com/angular/angular-cli/wiki/stories-include-bootstrap

Eduardo Vargas
  • 8,752
  • 2
  • 20
  • 27
-1

There are some handy extra mixins pack I recently commited. Feel free to use it https://github.com/Omi0/boostrap-mixins

Timothy
  • 3,213
  • 2
  • 21
  • 34