12

I've to add Bootstrap 4 in my custom theme.

I add Bootstrap 3 by using the reference:

How to use bootstrap in my custom theme

But, bootstrap 4 is not working.

  • What do you mean with "not working" ?? – Black Nov 28 at 7:26
23

For adding Bootstrap 4 In Magento 2.2.3 (tested) & above in your custom theme follow below steps

1) Bootstrap css files inside web folder

Note: THEME_LOCATION => app\design\frontend\vendor-name\theme-name\

THEME_LOCATION\web\css\bootstrap.css 

Call bootstrap css files in default_head_blocks

THEME_LOCATION\Magento_Theme\layout\default_head_blocks.xml

Adding css

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <!--Bootstrap css-->
        <css src="css/bootstrap.css" />
    </head>
</page>

2) Adding Js files in bundle form rather then individual because individual doesn't work

Place bootstrap.bundle.js files can download from here

THEME_LOCATION\Magento_Theme\web\js\bootstrap.bundle.js

Call Bootstrap Js Into requirejs-config.js

THEME_LOCATION\Magento_Theme\requirejs-config.js

code for

var config = {
    paths: {
            'bootstrap':'Magento_Theme/js/bootstrap.bundle',
    } ,
    shim: {
        'bootstrap': {
            'deps': ['jquery']
        }
    }
};

Add below code in header.phtml after your already present script tag

THEME_LOCATION\Magento_Theme\templates\html\header.phtml

Code:

<script type="text/javascript">require(['bootstrap']);</script>

Run Below Commands:

php bin/magento setup:static-content:deploy (Append -f if you are using Magento 2.2.x >= version)

php bin/magento cache:flush

Note: Tested on Magento 2.2.3 and Latest as well 2.2.6. Also tested on 2.3.3

Updated : Download Bootstrap compiled from here and use bundle js

  • Ok, Thanks. I will try this solution. BTW What is the difference between bootstrap.bundle.js and bootstrap.js? – Magecode May 30 '18 at 13:13
  • Popper.js included in bundle js ..... and jquery is already inside magento.... popper doesn't work alone but it works using as a bundle – Manoj Deswal May 30 '18 at 15:03
  • @Magecode is it working on Magento 2.2.4 ? – Manoj Deswal Jun 1 '18 at 3:51
  • 1
    I finally figured it out... I placed the js in MY_THEME/web/js instead of MY_THEME/Magento_Theme/web/js im sorry – Black Nov 29 at 11:17
  • 1
    This was the standard working way before 2.2.3 but in 2.2.3 it didn't worked and I discovered the above way. But it stills works in 2.3.2 .... The way you done that's proper way – Manoj Deswal Nov 29 at 11:20
11

You can add Bootstrap this way, follow below steps: Note: This solution doesn't work with Magento 2.2.3 and above

1) Place your JS and CSS at below location

/app/design/frontend/vendor-name/theme-name/web/css

/app/design/frontend/vendor-name/theme-name/web/js

2) Call files in your default_head_blocks.xml

app/design/frontend/vendor-name/theme-name/Magento_Theme/layout/default_head_blocks.xml

add these line of code

<css src="css/bootstrap.css" />
 <script src="js/bootstrap.min.js"/>

3) Run below commands

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy
  • And how does it work then with Magento 2.2.3 and above? – Black Nov 29 at 11:01
7

You should use bower package manager to add bootstrap 4 in your theme web folder.

WHY? Because there are constant update & easy to manage. Run the update command the Bootstrap 4 is updated.

ALSO: We prefer to use LESS or SCSS with bootstrap 4. Its robust and easy to refactor.

Follow below steps to install bower:

1) Install LTS nodeJS on your respective OS from: https://nodejs.org/en/

2) Install bower package manager globally: npm install -g bower

3) Install git from: https://git-scm.com/

4) Install bootstrap 4:

In web directory of you Magento custom theme

For example: Open terminal at <Magento root>/app/design/frontend/MyCustom/theme/web/

Run this command to install bootstrap 4: bower install bootstrap4

5) After this you need to add fonts, bootstrap CSS & bootstrap JS in default layout node to work boostrap on every page in Magento_Theme module in your custom theme.

We use gulp to compile SCSS to CSS.

  • How to use bower package manager? – Magecode Jun 6 '18 at 11:17
  • You need to download node from here: link. If you are using linux then use respective package manager. Then install bower using this command: sudo npm install -g bower. After install bower install js/css packages in your Theme directory: app/design/frontend/My/Theme/ – Ananth Jun 7 '18 at 2:47
  • Here you need to change default install directory from bower_components to web using .bowerrc config file. – Ananth Jun 7 '18 at 2:54
  • 1
    I think Bower is not anymore relevant. Bower team even recommends using Yarn or Webpack or Parcel for frontend projects. – phagento Nov 26 '18 at 13:06
  • First open package.json file of Bootstrap 4, if you see files array key it shows only that file type matching will be add to your project directory. files key is used by Bower command only. If you want to contribute or recompile Bootstrap then you should try Yarn. Because it will add unnecessary file/directory to the project. Webpack and Parcel are for node project they compile to JavaScript use for frontend. – Ananth Nov 27 '18 at 16:39
5

After reading the above answers, I have another suggestion: put Bootstrap 4 into a module, and use CDN to link the Bootstrap files instead.

I assume you know how to create a basic module. If not, you can refer to here. So here is the steps:

  • Create file app/code/Vendor/Module/view/frontend/layout/default_head_blocks.xml with the following codes:

    <?xml version="1.0"?>
    <!--
    /**
     * Copyright © 2013-2017 Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <head>
            <link rel="stylesheet"  type="text/css" src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous" src_type="url" />   
            <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous" src_type="url"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous" src_type="url"></script>
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous" src_type="url"></script>
        </head>
    </page>
    
  • Activate the module and run setup:upgrade command, that's all!

Benefits:

  1. Apply to ALL themes at once, no matter the theme is active or inactive
  2. You don't need to download any files into your system
  3. Very easy to update. Just replace the CDN link is the only task you need to do if needed. You don't need to compile and replace any files when you update to Bootstrap 5, 6, 7...
  4. You can enable and disable Bootstrap with ONE simple command.
  5. You don't need to override anything. Everything is on top of the current codes.
  6. No need to run static deploy which could be time consuming

Tested on Magento 2.2.4, but should work across all Magento 2.X versions. EDIT* changed the link tags href to src to be inline with the devdocs. Reference:

  • CDN can be sometimes slow. – phagento Nov 26 '18 at 13:04
  • I get The attribute 'integrity' is not allowed. – Black Nov 28 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.