将 .less css 定义包装在命名空间中

发布于 2024-12-18 04:47:07 字数 309 浏览 3 评论 0原文

我只想在网页的特定元素中使用 twitter bootstrap CSS。

我尝试像下面的代码那样做。但将其编译为 css 文件后,没有任何输出。如果我将 @import 移到 #my_div 之外,那么我就获得了 twitter boostrap 的所有 css 定义。

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

如何命名 less css 文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

兔小萌 2024-12-25 04:47:07

我没有在实时站点上使用 less,也没有手动进行编译,因此这是一个“简单”版本。它不像其他方法那样自动化,但可能适用于某些用户。

  1. 编辑bootstrap.css / bootstrap-responsive.css

    <前><代码>.tb {
    // 复制/粘贴整个 bootstrap.css
    }

  2. 使用 less 重新编译或使用在线 less 编译器 - http://winless.org/online-less-compiler

  3. 编辑现在-编译的文件并将 body {} CSS 声明更改为 tb {}

  4. 使用新的 CSS 文件。

  5. 将“引导”内容放入

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>

GRAY°灰色天空 2024-12-25 04:47:07

LESS 文档有一个关于命名空间的部分。

因此,您可以在单独的文档中定义您的 lib:

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

在要编译的 CSS 文件的开头导入此文件并使用以下说明:

#my_div {
  #ns > .twitter;
}

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;
}
忘年祭陌 2024-12-25 04:47:07

我就是这样做的。这发生在下载的或从 git 克隆的 bootstrap 文件夹的根目录中。

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

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

## ./style.less

@import "less/namespace.less";

.namespace {
  #ns > .twitter;
}

然后运行 ​​less style.less >样式.css

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

奶气 2024-12-25 04:47:07

以下是我的做法,基于上面的 majgis 的 github 分支:

bootstrap-ns.less:

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

namespace.less:

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

然后您在 html 页面中引用 bootstrap-ns.less 。这是用 dotLESS 测试的。

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.

夏末染殇 2024-12-25 04:47:07

如果您可以控制编译参数,只需将 strictImports 设置为 false 并按照您的预期工作,一切都应该没问题。考虑查看 less-strictimports 或在此 问题

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文