Tag Archive for: bootstrap

Bootstrap and SharePoint Online – how to avoid css ricochet

04 Mar 2016
March 4, 2016

In my current project I had the challenge to improve the user experience for some pages without messing too much with the MasterPage (because you shouldn’t, right?). The client explicitly requested components like accordions, responsive tables and more control of the page alignments. So what would be easier to use a mobile first, responsive made by the @twitter guys? Apparently SharePoint Online does not like bootstrap too much – one you add the css files, you will see notable changes.

In this post I show you how to fix that.

Bootstrap and SharePoint OnlineBootstrap

 

Bootstrap (currently 3.3.6 and shortly 4.0) brings a lot of nice components that can be embedded within a standard SharePoint Page. Its not end-user friendly, an editor needs a least basic html skills to use it – but if you can use it, its fairly easy to improve the user experience. And because the twitter guys are really smart (and their css skills are way better than mine) it even works decently on mobile devices.

You can simply (that was my first attempt) add the css tag to a page:

On the basic team site landing page I found at least 5 changes:

twitter bootstrap overrides the SharePoint css

twitter bootstrap overrides the SharePoint css

  1. the heading has an increased font size and the position has changed
  2. the title of a webpart has an increased font size
  3. the paginator icon is displaced
  4. the search icon of a doclib is displaced.
  5. the full screen icon is gone

How to restrain/scope bootstrap?

At first I thought its rather easy, just change a couple of lines within bootstrap and you should be good to go. Right, that took actually longer than the good solution – I stopped and googled. I found a solution on stackoverflow from 2012 – and guess what – it still works. In order to to that you need to do the following:

  1. Install nodejs
  2. Download the twitter bootstrap source code and unzip it
  3. Open the nodejs command prompt
  4. Install grunt-cli with the following command

  5. Switch to the twitter bootstrap directory
  6. Open the bootstrap.less file in the less folder and wrap every @import with:

    1.  

      Bootstrap-wrap

      Wrap everything – dont forget the closing } down below.

  7. Now switch back to the node command prompt. Change to the bootstrap directory.
  8. Run npm install to install missing packages

    1. Install missing packages with npm-install

      Install missing packages with npm-install

  9. Run grunt dist to compile your new css

    1. grunt-dist to compile your new scoped css

      grunt-dist to compile your new scoped css

       

  10. Et voila – then you have your perfectly scoped css in the folder dist/css:
    1. Scoped bootstrap

      Scoped bootstrap

       

Now you only have to upload this file to SharePoint Online (e.g. a style library) and either reference it within a Script Editor Webpart (your add it to your MasterPage if you are brave enough!). If you want to use it, you have to add the class “bootstrap” before:

add the bootstrap class in the first element or just wrap everything with a div

add the bootstrap class in the first element or just wrap everything with a div

And the you can use twitter bootstrap without jeopardizing your SharePoint Online look and feel:

twitter bootstrap without impact

twitter bootstrap without impact

 

The SASS way

If you want to do the same thing with the current version of bootstrap (and bootstrap 4 once its out), then you can simple download the SASS version:

Then open the _bootstrap.scss file with your favorite editor and wrap the imports with a class:

Compile the sass and check the css:

The sass approach took me like 5 minutes with VS Code – awesome.

If you have read it so far…

If you want my pre-compiled version, please take it from here (version 3.3.6, use on your own risk!).

And then leave a comment 🙂