In base.less:
html{font-size:16px}
body{color:green}
In theme.less:
html{color:red}
In main.less:
#import "base.less"
#import "theme.less"
When compiled it outputs:
html{font-size:16px}
body{color:green}
html{color:red}
How can I output this?
html{font-size:16px;color:red;}
body{color:green}
I tried this in theme.less without success:
html:extend(html) { color:red }
Do I need to use a CSS optimizer or is there something in LESS I can use?
Manual Way
I found an answer, Devilo.us. I tried your code and it outputs what you want. This is an external tool though. You've to do it manually.
Grunt Way
Zach Moazeni made a tool that handles this. Check that page for docs.
If you're using Grunt, you can add the grunt-csscss task which allows you to compile LESS to CSS (without redundancy) just by saving your LESS when grunt watch is running. I guess this is definitively what you're looking for.
It is possible to force "consolidated" selector styles via mixins:
html {.html()}
.html() {font-size: 16px}
body {color: green}
.html() {color: red}
.html() {background-color: blue}
// etc.
Though usually this kind of theming is achieved via variables:
// base.less
#text-color: blue;
html {
font-size: 16px;
color: #text-color;
}
// theme.less:
#text-color: yellow;
Related
Is there a postcss solution for importing a file into / nesting inside a selector? I can't get postcss-import or postcss-nested to do what I'm after.
.some-selector {
#import 'some.css';
}
Given a file e.g. import-me.css containing
div {
color: red;
}
I'd like to process entry.css
.some-class {
#import 'import-me.css';
}
And see the output
.some-class div {
color: red;
}
Thanks!
UPDATE: for the trivial example, you can bodge it by using postcss-nested-import AND postcss-nested but this has a couple of drawbacks because (a) postcss-nested-import paths are relative to the script running it, whereas css convention is that imports should be relative to the calling file (b) the maintainer has abandoned it https://github.com/eriklharper/postcss-nested-import/issues/2 <--- this issue in turn references https://github.com/postcss/postcss-import/issues/214 which is a dead thread :-(
postcss-partial-import seems to do the trick.
https://github.com/jonathantneal/postcss-partial-import
In this Bootstrap 4 tutorial https://www.youtube.com/watch?v=MDSkqQft92o&time_continue=24&app=desktop # 14:44, he redefines Bootstrap classes in his style.scss file.
style.scss:
.navbar {
width:100%;
background: none !important;
#media(max-width:34em){
background:black !important;
}
.nav-bar-toggler {
cursor:pointer;
outline:0;
}
}
What is the advantage? Since it is just CSS, shouldn't it go in the style.css file?
SASS is a CSS preprocessor and whatever you code in SASS will become CSS eventually. Basically SASS is just CSS with Variables, Nesting, and many more. You can check more here http://sass-lang.com/guide.
I checked the vid for like 3 secs and I think he's just showing how to use SASS specifically the Nesting feature.
It does go into the style.css file: You don't write that file yourself, but it's rendered from the sass file, therfore you write everything into the sass file.
I'm trying to import some classes from a CSS file like bootstrap.css to my site.scss SASS file, not all of them. The problem with following code is that I get all bootstrap classes in my compiled site.css file:
site.scss
#import "bootstrap";
.my-div-md-6
{
/*some other styles*/
#extend .col-md-6;
}
On the other hand, It is possible to do this with LESS by importing bootstrap.css as reference using this code:
site.less
#import (less, reference) "bootstrap.css";
.my-div-md-6{
/*some other styles*/
&:extend(.col-md-6);
}
The compiled output of LESS is very light as below:
site.css
.my-div-md-6 {
position: relative;
min-height: 1px;
padding-right: 15px;
padding-left: 15px;
}
#media (min-width: 992px) {
.my-div-md-6 {
float: left;
}
.my-div-md-6 {
width: 50%;
}
}
.my-div-md-6 {
/*some other styles*/
}
Is it possible to achieve this with SASS? If yes, giving a quick example would help.
Unfortunately, there is not simple answer and at the time of writing this, Ruby Sass does not natively support the LESS import (reference) feature.
TLDR; Suggestions:
use uncss or postcss to remove the compiled css from file before finalising stylesheet.
if you can, use mixins and placeholder classes as a rewrite of the scss file, but this is the MOST time consuming.
import "file" as partial such that file="_file.scss" and #extend .class if you absolutely have to, (manual method but suppose it'll work)
UNCSS
You can use uncss as a package from npm to remove the compiled css (I know this isn't efficient, but if you had to use SASS), then you'd remove the chaff that's generated from the example bootstrap import.
HOW?
QUOTE: SO-Answer-Joesph
How? The process by which UnCSS removes the unused rules is as follows:
The HTML files are loaded by PhantomJS and JavaScript is executed.
Used stylesheets are extracted from the resulting HTML.
The stylesheets are concatenated and the rules are parsed by css-parse.
document.querySelector filters out selectors that are not found in the HTML files.
The remaining rules are converted back to CSS.
So yes, it removes selectors not in the DOM at runtime. If you have dynamically added selectors, you can make uncss ignore them by commenting: /* uncss:ignore */ before them, e.g...
MAKE SURE YOU ADD THE MEDIA OPTION IN UNCSS
REF: SO-Answer-Deksden
SASS Background research:
Summarising above:
nex3: one of the core leads for sass, has been at google and working on dart. They released dart-sass (unstable release) as a rewrite in favour to replace and improve upon ruby sass. This is interesting as this rewrite also explains the lack of feature development in Ruby Sass as well as the need for a rewrite. Since a core contributor of a ruby sass port: i.e. libsass (C++ implementation of ruby-sass) left the libsass team, it brings a further impetus to improve on sass performance.
Credit:
Joesph
Deksden
I want to define CSS variables like in Sass.
Something like:
.someClass {
background-image: $imageLink;
color: $someColor;
}
And i Want to define $imageLink and $someColor in my class or in json file.
Can i do this without Sass or Less? (Or without css variables, because it does not supported in IE.)
I an using angular2 and webpack in my project.
Of course you cannot set SASS variables at run-time, because by that time the SASS has already been compiled.
A classic approach is to instead think in terms of "themes", and select a theme by a class on a higher-level element (such as body). So you could have
.darktheme .someClass { color: white; }
.lighttheme .someClass { color: black; }
Now from your JS you can change the theme with
document.body.classList.add('darktheme');
I'm working with a friend on a project with a huge CSS file.
There is a lot of duplication like:
h1 {
color : black;
}
h1 {
color : blue;
width: 30px;
}
The first h1 can be removed, because it will never be used, because fully rewrited by the second. (because it is in the same CSS file)
I would know if it exists a tool that factorizes (and compress) this kind of stuff.
To only have at the end:
h1 {color:blue;width:30px}
PS: If it can be an online tool, it will be perfect!
There's a nice one in ruby: http://zmoazeni.github.io/csscss
In node.js: https://github.com/rbtech/css-purge
Both are very easy to use from command line.
This is also a nice once: http://cssmerge.sourceforge.net
And a plugin for Firefox: https://addons.mozilla.org/en-us/firefox/addon/css-usage
First you can try
CSS usage checker
Then Try these
CSS Compressor
Javascript Compressor
If you are using Firefox, you can use this addon which will help you achieve it.
https://addons.mozilla.org/en-us/firefox/addon/css-usage/
It creates a new css which tells you only used rules and sideline unused one. It also lets you export that css.