Reputation: 879
This is a part of my nuxt.config.js file:
head: {
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
// load bootsttrap.css from CDN
//{ type: 'text/css', rel: 'stylesheet', href: '//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' },
]
},
css: [
// this line include bootstrap.css in each html file on generate
'bootstrap/dist/css/bootstrap.css',
'assets/main.css'
],
In this case bootstrap.css included in each html file on nuxt generate. For resolve it I comment line 'bootstrap/dist/css/bootstrap.css' in css section and uncomment rel stylesheet line in link section.
After this bootstrap.css file loaded from CDN and not included in html files. So, I think it not is very well idea.
How copy bootstrap.css from 'node_modules/bootstrap/dist/...' to '~/assets' on build, and after this, load it from here?
Upvotes: 21
Views: 28476
Reputation: 2141
Steps to include bootstrap.css into a nuxt project:
1 .Install bootstrap-vue
npm i bootstrap-vue
2.Create the file plugins/bootstrap-vue.js and in it type:
/* eslint-disable import/first */
import Vue from 'vue'
import BootstrapVue from 'bootstrap-vue'
Vue.use(BootstrapVue)
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
3.Add created plugin to nuxt.config.js:
plugins: [
'@/plugins/bootstrap-vue',
],
After these steps it should work and you can use bootstrap.
Upvotes: 18
Reputation: 19139
This is the simplest way I have found to include the regular Bootstrap version (not Bootstrap-Vue) in your Nuxt.js project. First, install Bootstrap from your Nuxt.js project directory (I'm using version 5.0.0-beta2):
npm install bootstrap
Then, in nuxt.config.js
file, include Bootstrap's CSS and Javascript like this:
css: [
"~/node_modules/bootstrap/dist/css/bootstrap.min.css"
],
plugins: [
{ src: "~/node_modules/bootstrap/dist/js/bootstrap.bundle.min.js", mode: "client" }
],
Note the mode: "client"
flag which indicates that the Javascript should only be run on the client. This prevents a ReferenceError: document is not defined
error that happens because Bootstrap is not compatible with server side rendering.
Upvotes: 14
Reputation: 2257
If you don't want to do it manually by reading the following answer, you can simply clone my bootstrap 5 with nuxt boilerplate repository, click here.
npm install bootstrap@next
Note: The command will change, currently bootstrap 5 is in the beta version.
After installing the bootstrap, you will know that you need to install some loaders to compile Bootstrap.
npm install --save-dev sass sass-loader fibers postcss postcss-loader autoprefixer
Note: Installation of fibers is not mentioned in bootstrap documentation but synchronous compilation with sass (2x speed increase) is enabled automatically when fibers is installed.
At first, create a folder in your nuxt project’s assests folder and also create a scss file inside the created folder. Suppose, the folder’s name is bootstrap and the file name is ‘mystyle.scss’. Now, on this mystyle.scss file, you can override any built-in custom Bootstrap variables.
But you need to import bootstrap styles in this mystyle.scss too.After that, you need the following line to import bootstrap styles
@import "~bootstrap/scss/bootstrap";
Note: Make sure that you import this in the last line of your .scss file.
After importing the bootstrap style, you need to edit the nuxt.config.js
export default {
css: [ { src: '~/assets/bootstrap/mystyle.scss', lang: 'sass'}
],
}
This will not only compile the overridden bootstrap styles but also add the bootstrap style in the whole nuxt project.
First, download bootstrap. You will get a css and a js folder on that downloaded folder. There are many files under those two folders. We just need one file from js folder. The name of that file is bootstrap.bundle.min.js. Copy that 'bootstrap.bundle.min.js' file and paste that file on your nuxt project's static folder. Then you need to edit your nuxt.config.js to use it.
export default {
script: [
{
src: '/bootstrap.bundle.min.js',
}
]
}
That's it, enjoy!
You can read about it in detail in my medium article, click here.
Upvotes: 5
Reputation: 5687
I used for Bootstrap v5
nuxt.config.js
css: [
'~/assets/scss/main.scss',
],
styleResources: {
scss: [
'~/node_modules/bootstrap/scss/_functions.scss',
'~/node_modules/bootstrap/scss/_variables.scss',
'~/node_modules/bootstrap/scss/_mixins.scss',
'~/node_modules/bootstrap/scss/_containers.scss',
'~/node_modules/bootstrap/scss/_grid.scss'
]
},
modules: [
'@nuxtjs/style-resources',
],
@nuxtjs/style-resources to have mixins, variables, grid available inside components: https://www.npmjs.com/package/@nuxtjs/style-resources example:
<style scoped lang="scss">
.header {
@include make-container();
}
</style>
main.scss
@import "bootstrap-configuration";
@import "bootstrap-optional";
@import "@/node_modules/bootstrap/scss/helpers";
@import "@/node_modules/bootstrap/scss/utilities/api";
@import "custom";
_bootstrap-configuration.scss
@import "bootstrap-required";
@import "@/node_modules/bootstrap/scss/utilities";
_bootstrap-optional.scss (I can include only what I want)
@import "@/node_modules/bootstrap/scss/root";
@import "@/node_modules/bootstrap/scss/reboot";
@import "@/node_modules/bootstrap/scss/type";
@import "@/node_modules/bootstrap/scss/images";
@import "@/node_modules/bootstrap/scss/containers";
@import "@/node_modules/bootstrap/scss/grid";
@import "@/node_modules/bootstrap/scss/tables";
@import "@/node_modules/bootstrap/scss/forms";
@import "@/node_modules/bootstrap/scss/buttons";
@import "@/node_modules/bootstrap/scss/transitions";
@import "@/node_modules/bootstrap/scss/dropdown";
@import "@/node_modules/bootstrap/scss/button-group";
@import "@/node_modules/bootstrap/scss/nav";
@import "@/node_modules/bootstrap/scss/navbar";
@import "@/node_modules/bootstrap/scss/card";
@import "@/node_modules/bootstrap/scss/accordion";
@import "@/node_modules/bootstrap/scss/breadcrumb";
@import "@/node_modules/bootstrap/scss/pagination";
@import "@/node_modules/bootstrap/scss/badge";
@import "@/node_modules/bootstrap/scss/alert";
@import "@/node_modules/bootstrap/scss/progress";
@import "@/node_modules/bootstrap/scss/list-group";
@import "@/node_modules/bootstrap/scss/close";
@import "@/node_modules/bootstrap/scss/toasts";
@import "@/node_modules/bootstrap/scss/modal";
@import "@/node_modules/bootstrap/scss/tooltip";
@import "@/node_modules/bootstrap/scss/popover";
@import "@/node_modules/bootstrap/scss/carousel";
@import "@/node_modules/bootstrap/scss/spinners";
_custom.scss (here I put variables to override Bootstrap and custom css for components)
$theme-colors: (
"red": #cf142b,
"black": #000,
"tan": #e0d9c7,
"blue": #009eba,
"blue-light": #d6e3e6,
"blue-dark": #006985,
"red-dark": #7d212b,
);
@import
'mixins/mixins';
@import
'_base/colors',
'_base/typography',
'_base/headings';
@import
'components/_page-header',
'components/text-editor',
'components/content-with-side-image',
'components/featured-artists',
'components/video-player',
'components/audio-player',
'components/call-to-action',
'components/divider',
'components/tabs',
'components/image-gallery',
'components/logos',
'components/button',
'components/page-submenu',
'components/page-submenu-target';
I got idea from https://v5.getbootstrap.com/docs/5.0/customize/optimize/
Upvotes: 0
Reputation: 864
You install bootstrap-vue :
yarn add bootstrap-vue
Then in the nuxt.config.js file you add a module, and a separate config for importing the icons :
modules: ['bootstrap-vue/nuxt'],
bootstrapVue: {
icons: true // Install the IconsPlugin (in addition to BootStrapVue plugin
}
And thats it. For more configuration read the documentation here : https://bootstrap-vue.org/docs (Read until the middle where it handles nuxt)
Upvotes: 0
Reputation: 154
As for me i create a sass
directory inside assets
directory in nuxt.
Then i add app.scss
file inside sass
directory i just created.
Then inside app.scss
i import the following:
@import "bootstrap-variable-override";
@import "~bootstrap/scss/bootstrap.scss";
Then i do yarn add node-sass sass-loader --save
After that in nuxt.config.js
i modify css array to include my app.scss
css: [
'@/assets/sass/app.scss'
],
This will compile whatever scss i will write and import in app.scss and provide as a compiled css.
But if you are bootstrap-vue then all you need to do is add this as modules:
modules: ['bootstrap-vue/nuxt']
Upvotes: 8
Reputation: 16513
Here is my setup, and this is for those who like to customize bootstrap default style:
In my assets/styles/_bootstrap.scss
I have imported required style:
@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";
@import "~bootstrap/scss/mixins";
//@import "~bootstrap/scss/root";
@import "~bootstrap/scss/reboot";
@import "~bootstrap/scss/type";
//@import "~bootstrap/scss/images";
//@import "~bootstrap/scss/code";
@import "~bootstrap/scss/grid";
//@import "~bootstrap/scss/tables";
@import "~bootstrap/scss/forms";
@import "~bootstrap/scss/buttons";
@import "~bootstrap/scss/transitions";
//@import "~bootstrap/scss/dropdown";
//@import "~bootstrap/scss/button-group";
//@import "~bootstrap/scss/input-group";
//@import "~bootstrap/scss/custom-forms";
//@import "~bootstrap/scss/nav";
//@import "~bootstrap/scss/navbar";
//@import "~bootstrap/scss/card";
//@import "~bootstrap/scss/breadcrumb";
//@import "~bootstrap/scss/pagination";
//@import "~bootstrap/scss/badge";
//@import "~bootstrap/scss/jumbotron";
//@import "~bootstrap/scss/alert";
//@import "~bootstrap/scss/progress";
//@import "~bootstrap/scss/media";
//@import "~bootstrap/scss/list-group";
@import "~bootstrap/scss/close";
@import "~bootstrap/scss/modal";
//@import "~bootstrap/scss/tooltip";
//@import "~bootstrap/scss/popover";
@import "~bootstrap/scss/carousel";
@import "~bootstrap/scss/utilities";
//@import "~bootstrap/scss/print";
Just in case you like to change the default style of bootstrap then I created assets/styles/_bootstrap-variables.scss
:
There are lots of variable that you can find in node-modules/bootstrap/scss/variables.scss
, I am just changing a few.
@import url('https://fonts.googleapis.com/css?family=Cabin:400,500,700');
$font-family-cabin: 'Cabin', sans-serif;
$font-family-base: $font-family-cabin;
$primary: #b62f20;
$secondary: #e8e8e9;
$success: #87b4a6;
$info: #f0f6fc;
//$warning: #faeecf;
//$danger: $red !default;
//$light: $gray-100 !default;
//$dark: $gray-800 !default;
And import all my other styles and plugins in one file assets/styles/main.scss
:
@import "bootstrap-variables"; // this should alway be on top
@import "bootstrap";
@import "my-other-style";
And finally, import the stylesheet in layouts/default.vue
<template lang="pug">
div
nuxt
</template>
<style lang="scss">
@import '../assets/styles/main';
</style>
Upvotes: 3
Reputation: 22754
In my case, I add it from CDN as an object entry to link
array (in nuxt.config.js)
link: [
{rel: 'stylesheet', type: 'text/css', href: 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css'}
]
Upvotes: 3
Reputation: 439
In my case, I put bootstrap.css file in "static" folder, and then register it to the nuxt.config.js as bellow
head: {
title: "Nuxt",
meta: [
{ charset: "utf-8" },
{ name: "viewport", content: "width=device-width, initial-scale=1" },
{
hid: "description",
name: "description",
content: "Nuxt"
}
],
link: [
{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" },
{ rel: "stylesheet", href: "/css/bootstrap.css" } //Register your static asset
]
},
Upvotes: 3
Reputation: 1859
If you are looking to centralize your CSS imports (specifically bootstrap from node_modules, in your case) into a single file for Nuxt generating, you could include an at-rule import to your 'assets/main.css' (recommend to update it to '~/assets/main.css') specified in your config.
@import '../node_modules/bootstrap/dist/css/bootstrap.css'
Just a reminder: when you simply run Nuxt in dev mode, the CSS will be injected via JS. When generated, however, Nuxt will create a single, hashed, CSS file as part of the document root directory.
Upvotes: 3