I want to use Twitter Bootstrap, but only on specific elements, so I need to figure out a way to prefix all Twitter Bootstrap classes with my prefix, or use the less mixins. I'm not experienced with this yet so I don't quite understand how to do this. Here's an example of the HTML that I'm trying to style:
<div class="normal-styles">
<h1>dont style this with bootstrap</h1>
<div class="bootstrap-styles">
<h1>use bootstrap</h1>
</div>
</div>
In this example, Twitter Bootstrap would normal style both h1
s, but I want to be more selective about which areas I apply the Twitter Bootstrap styles.
To explain better all the steps what Andrew was talking about for those who don't know what Less is. Here is a link that explain pretty well how it is done step by step How to isolate Bootstrap or other CSS libraries
Most simple solution - start to use custom build isolated css classes for Bootstrap.
For example, for Bootstrap 4.1 download files from css4.1 directory -
https://github.com/cryptoapi/Isolate-Bootstrap-4.1-CSS-Themes
and wrap your HTML in a div with the class bootstrapiso :
Page template with isolated bootstrap 4 will be
@Andrew great answer. You'll also want to note that bootstrap modifies the body {}, mainly to add font. So when you namespace it via LESS/SASS your output css file looks like this: (in bootstrap 3)
but obviously there will not be a body tag inside your div, so your bootstrap content will not have the bootstrap font (since all bootstrap inherits font from parent)
To fix, the answer should be:
Namespacing breaks the Modal plugin's backdrop div, since that is always added directly to the <body> tag, bypassing your namespacing element which has the styles applied to it.
You can fix this by changing the plugin's reference to
$body
before it shows the backdrop:The
$body
property decides where the backdrop will be added.As a further note for everyone. To get modals working with a backdrop you can simply copy these styles from bootstrap3
This turned out to be easier than I thought. Both Less and Sass support namespacing (using the same syntax even). When you include bootstrap, you can do so within a selector to namespace it:
Update: For newer versions of LESS, here's how to do it:
A similar question was answered here.