22

I wanted to use twitter bootstrap CSS only in a specific element in my web page.

I tried to do it like in code below. But after compiling this to a css file nothing was outputted. If I moved @import outside #my_div then I got all css definitions for twitter boostrap.

#my_div {
    @import "../twitter_bootstrap/lib/bootstrap.less";
}

How can I namespace a less css file?

Martins Balodis
  • 1,999
  • 1
  • 17
  • 18

5 Answers5

23

I am not using less on the live site, nor am I manually doing the compiling so this is kind of a "simple" version. It's not as automated as the others but may apply to some users.

  1. Edit bootstrap.css / bootstrap-responsive.css

    .tb {
      // copy/paste the entire bootstrap.css
    }
    
  2. Recompile with less or use an online less compiler - http://winless.org/online-less-compiler

  3. Edit the now-compiled file and change body {} CSS declarations to tb {}.

  4. Use the new CSS file.

  5. Place your "bootstrapped" content inside a <div class='tb'></div>

Ulf Gjerdingen
  • 1,414
  • 3
  • 16
  • 20
Owen Allen
  • 11,348
  • 9
  • 51
  • 63
  • How would this prevent collisions in html, for example:
    When another library has a class definition of 'large-button'? Prefixing is the only way to prevent namespace collisions on both ends.
    – TaylorMac Jul 22 '14 at 22:40
  • I had previously tried using by using all the .less files in the source code and recompiling with winless. However, something was missing in the final CSS file. I was able to correct it by following your procedure -- thanks! – BradByte Dec 16 '14 at 21:24
  • This worked for me. I followed the instructions under "Installing Grunt" at getbootstrap.com/getting-started. Once that was working, I followed this answer, recompiling with `grunt dist`. This way, I maintain CSS vendor prefixes. – Ryan H. Aug 20 '15 at 04:42
11

LESS documentation has a section about namespaces.

So you could define your lib in a separate document:

#ns {
  .twitter () {
    // Instructions to be used later
    // Nothing will appear in compiled CSS except if called later (because of parenthesis)
  }
}

import this file at the beginning of the CSS file to be compiled and use these instructions:

#my_div {
  #ns > .twitter;
}
seemly
  • 1,090
  • 10
  • 20
FelipeAls
  • 21,711
  • 8
  • 54
  • 74
  • 2
    The problem is with `@import`. It doesn't work when using it in a namespace. – Martins Balodis Nov 27 '11 at 21:18
  • @MartinsBalodis The code for lib should be a LESS file (just add `#ns { .anything () {` at the beginning and two `}` at the end of your CSS lib. And the main file must also be a LESS file. The import should take place at the beginning outside `#my_div {}`. Nothing from the first file will be outputted without further instruction, so you can import it anywhere before this selector. Then the `#ns > .twitter;` instruction will finally add your code right where you want to. – FelipeAls Nov 27 '11 at 21:27
  • The twitter lib can only be imported. I cannot edit those files. They are used as git submodule. [Twitter modules main .less file](https://github.com/twitter/bootstrap/blob/master/lib/bootstrap.less) – Martins Balodis Nov 27 '11 at 22:05
  • 1
    Thanks! It worked!! For those making a first attempt, here is a fork with a modified makefile and notes for compiling: https://github.com/majgis/bootstrap/blob/master/notes.txt – Michael Allan Jackson Oct 16 '12 at 09:22
  • For anyone that just wants a quick file to work with, here is a compiled version of v2.2.2 using a ".tbs" namespace class. https://gist.github.com/4504997 – jkriddle Jan 10 '13 at 19:33
4

This is how I have done it. This takes place in the root of the bootstrap folder that is downloaded, or cloned from git.

## ./less/namespace.css (New file)

#ns {
  .twitter() {
    @import "less/bootstrap.less";
  }
}

## ./style.less

@import "less/namespace.less";

.namespace {
  #ns > .twitter;
}

Then run less style.less > style.css

Randy
  • 41
  • 1
3

Here is how I did it, based on majgis's github fork above:

bootstrap-ns.less:

@import "namespace.less"
.bs {
    #ns > .twitter;
    }

namespace.less:

#ns {
  .twitter(){
    @import "bootstrap.less";
  }
}

You then reference bootstrap-ns.less in your html page. This was tested with dotLESS.

muzzamo
  • 1,721
  • 2
  • 14
  • 18
0

if you have control over the compilation parameters just set strictImports to false and work as you intended to everything should be fine. consider looking at less-strictimports or at this issue.

Community
  • 1
  • 1