最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Twitter Bootstrap isn't working with less.js - Stack Overflow

programmeradmin0浏览0评论

I have broken it down to it simplest form but still cannot find out why this is not working. All files resolve and the imports in Bootstrap are loaded yet, the styles aren't loaded.

bootstrap 1.4.0 less 1.1.3

<html>
    <head>
        <title>ahhhhhh...</title>

        <link rel="stylesheet/less" href="/less/bootstrap/1.4.0/bootstrap.less">
        <script src="/less/less-1.1.3.min.js"></script>

    </head>
    <body>

        <h1>WTF!!!</h1>

    </body>
</html>

I made a simple style.less which works fine! Am I missing something glaringly obvious ?

Update:

style.less as requested by Todd :

@primary_color: green;

h1 {
    color: @primary_color;
}

I have broken it down to it simplest form but still cannot find out why this is not working. All files resolve and the imports in Bootstrap are loaded yet, the styles aren't loaded.

bootstrap 1.4.0 less 1.1.3

<html>
    <head>
        <title>ahhhhhh...</title>

        <link rel="stylesheet/less" href="/less/bootstrap/1.4.0/bootstrap.less">
        <script src="/less/less-1.1.3.min.js"></script>

    </head>
    <body>

        <h1>WTF!!!</h1>

    </body>
</html>

I made a simple style.less which works fine! Am I missing something glaringly obvious ?

Update:

style.less as requested by Todd :

@primary_color: green;

h1 {
    color: @primary_color;
}
Share Improve this question edited Sep 28, 2014 at 1:09 Devin 7,7206 gold badges43 silver badges56 bronze badges asked Nov 11, 2011 at 14:40 briangallagherbriangallagher 5394 silver badges15 bronze badges 7
  • Can you post your style.less? – Todd Commented Nov 11, 2011 at 14:48
  • @Todd updated the post. The style.less works fine though. – briangallagher Commented Nov 11, 2011 at 14:56
  • does it show the loaded/rendered stylesheet within the console? – Manuel van Rijn Commented Nov 11, 2011 at 14:58
  • sorry if i don't understand, but you see the less files converted to css files and the response contains something like h1 { color: green; } but yet you don't see the h1 element green? – Manuel van Rijn Commented Nov 11, 2011 at 15:17
  • 1 When I test using style.less the h1 turns green. But when I replace style.less for bootstrap.less no styles are applied. I can see all the imports loading in the console, but no styles are applied. – briangallagher Commented Nov 11, 2011 at 15:21
 |  Show 2 more comments

4 Answers 4

Reset to default 6

Update 3/5/2012: The Bootstrap guys have fixed this problem in version 2.0.2 of Bootstrap (not yet released). See this commit.

The underlying bug is that the present version of less.js doesn't allow you to use a variable for for the url() value directly (e.g. url(@iconWhiteSpritePath)) -- instead you have to use string interpolation (e.g. url("@{iconWhiteSpritePath}")), which accomplishes the same thing. The bootstrap guys just updated their syntax to take this quirk into account.

Original Answer

I ran into the exact problem today and figured out the cause of it. Since your stack is a few versions before mine (I'm using Bootstrap 2.0 and less.js 1.2.2) I can't be sure it's the same issue, but it's possibly related.

Anyhow, the problem for me was that Twitter Bootstrap is defining some variables:

@iconSpritePath:          "../img/glyphicons-halflings.png";
@iconWhiteSpritePath:     "../img/glyphicons-halflings-white.png";

And then using them directly in the url() value for a background-image in sprites.less:

[class^="icon-"],
[class*=" icon-"] {
  display: inline-block;
  width: 14px;
  height: 14px;
  line-height: 14px;
  vertical-align: text-top;
  background-image: url(@iconSpritePath);
  background-position: 14px 14px;
  background-repeat: no-repeat;

  .ie7-restore-right-whitespace();
}
.icon-white {
  background-image: url(@iconWhiteSpritePath);

}

Per the discussion in this Github issue that's causing a major problem. When less.js chokes on this value, the whole compilation fails.

The good news is there is a fix in that issue, provided by csnover. Just change this line in tree.URL:

    if (typeof(window) !== 'undefined' && !/^(?:https?:\/\/|file:\/\/|data:|\/)/.test(val.value) && paths.length > 0) {

to

    if (typeof(window) !== 'undefined' && typeof(val.value) !== 'undefined' && !/^(?:https?:\/\/|file:\/\/|data:|\/)/.test(val.value) && paths.length > 0) {

and you should be set. In other words, we just ensure that val.value is set so that that charAt() doesn't choke on an undefined.

Hopefully this fix will be committed to the project soon and Bootstrap will work with less.js in the browser environment out of the box.

If you are developing this locally, make sure that the browser is using the correct protocol. It should display http: in the address bar, not file:.

On my Windows 7 machine using Chrome (with a XAMPP setup), I was having the same CSS problem using less.js with Bootstrap when accessing the .html file at:

file:///C:/xampp/htdocs/index.html

However, it did render properly via http at:

http://localhost/index.html

Not certain why, but I imagine that less.js relies on HTTP headers.

for me worked

[class^="icon-"],
[class*=" icon-"] {
  display: inline-block;
  width: 14px;
  height: 14px;
  line-height: 14px;
  vertical-align: text-top;
  background-image: url(../img/glyphicons-halflings.png);
  background-position: 14px 14px;
  background-repeat: no-repeat;

  .ie7-restore-right-whitespace();
}
.icon-white {
  background-image: url(../img/glyphicons-halflings-white.png);
}

so replace

url(@iconSpritePath);

with

url(../img/glyphicons-halflings.png);

on less.js v1.2.1 and Bootstrap v2.0.1

Is it the problem with your file structure? /less/bootstrap/1.4.0/bootstrap.less will point to the root of your domain regardless of your html file.

If you are hosting with apache and you have the following project structure:

www/
    your project/
        less/
        index.html
    another project/
    others/

When you access index.html from http://localhost, it will look up the less file from the domain root which is www/. Thus as no less folder is under this root directory, it will returned as 404 (not found).

So, the solution is to use relative url for your less files. If you have the above structure, remove the '/' and use less/bootstrap/1.4.0/bootstrap.less instead.

发布评论

评论列表(0)

  1. 暂无评论