Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESS CSS not working locally

Tags:

css

less

I really want to learn LESS but have had NO luck getting it to work at all. I would like to get it to work locally. Please help.

I just want to do a simple test. Here is my LESS code:

@color: red;

.header {
    color: @color;
}

h2 {
    color: @color;
}

And here is my HTML code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>LESS TEST</title>
    <link rel="stylesheet/less" type="text/css" href="style.less">
    <script src="less.js" type="text/javascript"></script>
</head>

<body>
    <div class="header"> <h2>This is a test</h2> </div>
</body>
</html>

So the header class should be red and also the H2 should be red. But they do not display with any styling on any browser. Please help!

like image 660
user1898404 Avatar asked May 15 '13 20:05

user1898404


People also ask

Can I use CSS in LESS?

Less (which stands for Leaner Style Sheets) is a backwards-compatible language extension for CSS. This is the official documentation for Less, the language and Less.js, the JavaScript tool that converts your Less styles to CSS styles.

Does LESS need to be compiled?

Using Less. js in the browser is the easiest way to get started and convenient for developing with Less, but in production, when performance and reliability is important, we recommend pre-compiling using Node.

How do I include LESS CSS in HTML?

Create a stylesheet with . less extension and link it in your document using the rel="stylesheet/less" attribute. You are all set and can compose styles within the . less .

What is the difference between CSS and LESS?

CSS and LESS are not completely different, but LESS does offer additional features that make your projects easier. More than that, the compiled version of LESS is actual CSS. Feel free to share your feedback as comments to this blog.


3 Answers

Your code works perfectly, as shown in this screenshot:

enter image description here

Note that I downloaded a minified version of less.js locally prior to running this. I suggest you verify:

  1. that your files are properly named and located: check for 404 errors in your web console (F12 in Chrome and Firefox).
  2. that your files are being served by a webserver. Less files will not be served if you try to access them through at a file:///path/less.js URL -- Chrome gives me a "cross origin request" error.
like image 160
Régis B. Avatar answered Oct 03 '22 13:10

Régis B.


Disable cache in Chrome, worked in my case.

Disable cache in Chrome

like image 27
Geraldo Magalhães Avatar answered Oct 03 '22 11:10

Geraldo Magalhães


I use WINLESS. It's free, it's easy to use and very powerful on minifying the code.

  • Download, install and open Winless;
  • Open the LESS file with the compliler and keep in mind that every time you save the LESS file, Winless will compile it into a CSS file;
  • Include the CSS file that result into your header.

You don't have to emulate an Apache server to use it.

Good luck!

like image 42
PAdrian Avatar answered Oct 03 '22 13:10

PAdrian