Joomla LESS: Difference between revisions

From Joomla! Documentation

Pwiseman (talk | contribs)
Cmb (talk | contribs)
Several markup changes. URL corrections. Changes for Words2Watch compliance.
 
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
Most of the Joomla 3.0 default template stylesheets are written using [[wikipedia:LESS_(stylesheet_language)|LESS]] and then compiled to generate the [[wikipedia:CSS|CSS]] files.
<noinclude><languages /></noinclude>


== Where can you find the .less stylesheets and compiler? ==
<translate>
<!--T:1-->
Most of the Joomla 3.0 default template style sheets are written using [[wikipedia:LESS_(stylesheet_language)|LESS]] and then compiled to generate the [[wikipedia:CSS|CSS]] files.
</translate>


The <code>.less</code> building blocks are located in <code>media/jui/less/</code>. The template specific <code>.less</code>files are located in <code>templates/&lt;templates&gt;/less/</code>.
<translate>
== Where Can You Find the ''.less'' Style Sheets and Compiler? == <!--T:2-->
</translate>


The CSS generation wrapper script, LESS compiler, and other similar build tools are located in the <code>build/</code> directory of the Joomla source located on GitHub. Refer to [[Git_for_Coders]] for more information on using GitHub. The build directory is only available from the Joomla source, it is not included in an official Joomla release.  
<translate>
<!--T:3-->
The ''.less'' building blocks are located in ''media/jui/less/''. The template-specific ''.less'' files are located in ''templates/&lt;templatename&gt;/less/''.


== How to re-generate the CSS stylesheets ==
<!--T:4-->
The CSS generation wrapper script, LESS compiler, and other similar build tools are located in the ''build/'' directory of the Joomla source located on GitHub. Refer to [[S:MyLanguage/Git_for_Coders|Git for Coders]] for more information on using GitHub. The build directory is only available from the Joomla source. It is not included in an official Joomla release.
</translate>


To re-generate all the CSS files from a Joomla core distribution, you will need to execute the generation scripts as a [[wikipedia:Command-line_interface|CLI application]].
<translate>
== How to Regenerate the CSS Style Sheets == <!--T:5-->
</translate>
 
<translate>
<!--T:6-->
To regenerate all the CSS files from a Joomla core distribution, execute the generation scripts as a [[wikipedia:Command-line_interface|CLI application]]:</translate>


For example:
<pre>cd joomla-cms/build
<pre>cd joomla-cms/build
c:\xampp\php\php.exe generatecss.php
c:\xampp\php\php.exe generatecss.php
</pre>
</pre>


== Compiling your own LESS files for your template ==
<translate>
== Compiling Your Own LESS Files For Your Template == <!--T:8-->
 
<!--T:9-->
To compile LESS files for your own template, you will need to take a copy of the ''generatecss.php'' script and adjust it to suit your template.
 
<!--T:10-->
Alternatively you can use a [https://extensions.joomla.org/extension/miscellaneous/development/less-compiler/ LESS Compiler plugin] that compiles your ''.less'' files automatically on page reload.
 
<!--T:11-->
More alternatives and tools are to be found in [https://magazine.joomla.org/all-issues/issue-may-2013/tools-to-do-less a Joomla! Magazine article].
</translate>
 
<translate>
== Not All LESS Compilers are Equal == <!--T:12-->
The LESS compiler used for the Joomla core for code style consistency is obtained from [https://leafo.net/lessphp/ leafo.net/lessphp].
 
<!--T:13-->
If you're working on your own template, use any compiler you like.
</translate>
 
<translate>
== Difference CSS vs LESS Imports == <!--T:14-->
 
<!--T:15-->
The main ''.less'' file and starting point in Protostar is located in ''/templates/protostar/less/template.less''.
If you open that file you will find many ''@imports''—imports from many places. Unlike imports in CSS, imports in LESS get compiled to a single CSS file. This results in fewer HTTP requests and speeds up your site.
</translate>
 
<translate>
== From Existing CSS to LESS/Import CSS Files == <!--T:16-->
 
<!--T:17-->
You may want to add your existing CSS files and classes to your LESS-powered template or start with what you have. All CSS declarations are compatible with LESS so just rename your ''.css'' files to ''.less'' and you can compile and use them. You can then make use of the great dynamic features LESS has to offer: variables, mixins, operations and functions. See the [https://lesscss.org/ lesscss.org website].
</translate>
 
<translate>
<!--T:18-->
Notice: The examples below reference the default Protostar template for clarity. Your paths may be different.
 
Notice: If you want to customize the Protostar template, copy the template and customize it afterwards so that Joomla! updates do not overwrite your customizations.
</translate>
 
<translate>
=== Option 1: Import Your ''.css'' as ''.less'' Files === <!--T:19-->
(A little bit work but worth it.)
</translate>
 
<translate>
<!--T:20-->
Now let's assume you want to include your custom files and get them parsed by LESS compiler. You do '''not''' need to rewrite your ''.css'' to LESS because plain ''.css'' works as well. The only thing you have to do is to rename your ''.css'' files to ''.less'' and add an @import statement into ''/templates/protostar/less/template.less''.
</translate>
 
<translate>
<!--T:21-->
You put your custom ''.css'' files renamed to ''.less'' into the ''/templates/protostar/less'' folder. Now open ''/templates/protostar/less/template.less'' and at the bottom of the file, import your custom ''.less'' files so that they override existing declarations:
</translate>
 
<pre>
@import "modules.less";
@import "components.less";
@import "articles.less";
</pre>
 
<translate>
<!--T:22-->
If you now compile your main ''template.less'' file, you end up with one main ''template.css'' optimized and minified (if you wish). You also reduced your HTTP requests and the site should load faster. Now you can start in your ''custom.less'' files to use LESS variables and other features [https://lesscss.org/# lesscss.org] has to offer.
</translate>


To compile less files for your own template, you will need to take a copy of the <code>generatecss.php</code> script and adjust it to suite your template.
<translate>
=== Option 2: Just Import Your ''.css''/Does Not Work for Overriding === <!--T:23-->
In ''/templates/protostar/less/template.less'', import your ''.css'' files and they will be included as imports into main ''template.css'':
</translate>
 
<pre>
@import "../css/modules.css";
@import "../css/components.css";
@import "../css/articles.css";
</pre>


== Not all LESS compilers are equal ==
<translate>
The LESS compiler used for the Joomla core is obtained from [http://leafo.net/lessphp leafo.net/lessphp].
<!--T:24-->
This way you do not get any optimization. Another big issue is that this import is always on top before other declarations so you can't override an existing declaration.


If you're working on your own template you can use any compiler you like.
<!--T:25-->
More information: [https://lesscss.org/# lesscss.org]
</translate>


<noinclude>[[Category:New in Joomla! 3.0]][[Category:Bug Squad]]</noinclude>
<noinclude><translate>
<!--T:28-->
[[Category:New in Joomla! 3.0]]
[[Category:Bug Squad]]
[[Category:Template Development]]
[[Category:Development]]
</translate>
</noinclude>

Latest revision as of 22:48, 13 November 2022

Most of the Joomla 3.0 default template style sheets are written using LESS and then compiled to generate the CSS files.

Where Can You Find the .less Style Sheets and Compiler?

The .less building blocks are located in media/jui/less/. The template-specific .less files are located in templates/<templatename>/less/.

The CSS generation wrapper script, LESS compiler, and other similar build tools are located in the build/ directory of the Joomla source located on GitHub. Refer to Git for Coders for more information on using GitHub. The build directory is only available from the Joomla source. It is not included in an official Joomla release.

How to Regenerate the CSS Style Sheets

To regenerate all the CSS files from a Joomla core distribution, execute the generation scripts as a CLI application:

cd joomla-cms/build
c:\xampp\php\php.exe generatecss.php

Compiling Your Own LESS Files For Your Template

To compile LESS files for your own template, you will need to take a copy of the generatecss.php script and adjust it to suit your template.

Alternatively you can use a LESS Compiler plugin that compiles your .less files automatically on page reload.

More alternatives and tools are to be found in a Joomla! Magazine article.

Not All LESS Compilers are Equal

The LESS compiler used for the Joomla core for code style consistency is obtained from leafo.net/lessphp.

If you're working on your own template, use any compiler you like.

Difference CSS vs LESS Imports

The main .less file and starting point in Protostar is located in /templates/protostar/less/template.less. If you open that file you will find many @imports—imports from many places. Unlike imports in CSS, imports in LESS get compiled to a single CSS file. This results in fewer HTTP requests and speeds up your site.

From Existing CSS to LESS/Import CSS Files

You may want to add your existing CSS files and classes to your LESS-powered template or start with what you have. All CSS declarations are compatible with LESS so just rename your .css files to .less and you can compile and use them. You can then make use of the great dynamic features LESS has to offer: variables, mixins, operations and functions. See the lesscss.org website.

Notice: The examples below reference the default Protostar template for clarity. Your paths may be different.

Notice: If you want to customize the Protostar template, copy the template and customize it afterwards so that Joomla! updates do not overwrite your customizations.

Option 1: Import Your .css as .less Files

(A little bit work but worth it.)

Now let's assume you want to include your custom files and get them parsed by LESS compiler. You do not need to rewrite your .css to LESS because plain .css works as well. The only thing you have to do is to rename your .css files to .less and add an @import statement into /templates/protostar/less/template.less.

You put your custom .css files renamed to .less into the /templates/protostar/less folder. Now open /templates/protostar/less/template.less and at the bottom of the file, import your custom .less files so that they override existing declarations:

@import "modules.less";
@import "components.less";
@import "articles.less";

If you now compile your main template.less file, you end up with one main template.css optimized and minified (if you wish). You also reduced your HTTP requests and the site should load faster. Now you can start in your custom.less files to use LESS variables and other features lesscss.org has to offer.

Option 2: Just Import Your .css/Does Not Work for Overriding

In /templates/protostar/less/template.less, import your .css files and they will be included as imports into main template.css:

@import "../css/modules.css";
@import "../css/components.css";
@import "../css/articles.css";

This way you do not get any optimization. Another big issue is that this import is always on top before other declarations so you can't override an existing declaration.

More information: lesscss.org