wrap a .less css definitions in a namespace

前端 未结 5 557
情话喂你
情话喂你 2020-12-24 07:31

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

相关标签:
5条回答
  • 2020-12-24 08:01

    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>

    0 讨论(0)
  • 2020-12-24 08:07

    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;
    }
    
    0 讨论(0)
  • 2020-12-24 08:09

    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.

    0 讨论(0)
  • 2020-12-24 08:17

    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.

    0 讨论(0)
  • 2020-12-24 08:20

    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

    0 讨论(0)
提交回复
热议问题