Less conditional style inclusion - css

I'm kinda new to LESS, so I'm not sure if that's possible and how.
So my idea is this :
I have a main-stylesheet.less and a dozen smaller stylesheets each of which contains several versions of styling for a particular element i.e. header.less, footer.less, main-nav.less etc.
So in header.less every variant of styling is linked to a condition and when you include this LESS file in the main-stylesheet.less via this condition, just that particular chunk of code is compiled and not the other ones that are linked to other conditions.
I hope I've been thorough enough for you to best understand me.
Is this possible and how ?

Keeping it Generic
By using conditional mixins, the code in main-stylesheet.less stays generic, with only the variables changing to control what is actually compiled.
Example header.less
.header() when (#header = 1) {
#header {
your: properties;
for: header1;
}
}
.header() when (#header = 2) {
#header {
your: properties;
for: header2;
}
}
This could continue on, and similar code would be in the other footer.less files, etc.
Example main-sytlesheet.less
#import header.less;
#import main-nav.less;
#import footer.less;
/* set your variables for your conditional mixins */
#header: 1;
#main-nav: 3;
#footer: 3;
/* call the mixins */
.header();
.main-nav();
.footer();
The global variables you set will only choose the .header() mixin to generate the #header, etc. that #header variable is set to.

I think maybe your looking for a LESS mixin?
/* header.less */
.header1 () {
// including selector
.header {
position: relative;
z-index: 1001;
width: 100%;
margin: 0 0 20px;
overflow: hidden;
}
}
.header2 () {
position: relative;
z-index: 1001;
width: 50%;
margin: 20px;
}
.header3 () {
position: relative;
z-index: 1;
width: 90%;
margin: 0 0 50px;
overflow: hidden;
}
And include it like this:
/* main-stylesheet.less */
#import 'header.less';
body {
.header1();
}
You could also get smart and use one mixin:
.header (#margin: 0, #width: 100%) {
position: relative;
z-index: 1001;
width: #width;
margin: #margin;
overflow: hidden;
}
header {
.header(0 0 20px, 50%);
}

Related

Issue with minified LESS CSS code

I have some code that is not rendering correctly in the minified output.
Here's the basics:
I have a mixin:
.Aspect(#widthRatio:16; #heightRatio:9; #useableWidth:100%) {
display:block;
overflow:hidden;
max-width:#useableWidth;
&::before {
content:"";
float:left;
padding-top:percentage(#heightRatio / #widthRatio);
}
}
... and some styles:
.backdrop {
position:absolute;
top:0;
left:0;
width:100%;
.Aspect(1; 1);
.Landscape({
.Aspect(16; 9);
});
}
.app-bar-spacer-3 {
height:250px!important;
background-color:Lime;
}
Here's what's happening.
The .app-bar-spacer-3 style is not working, however it's due to the rendering of the previous style .backdrop.
I can make it go away by removing the pseudo element in .Aspect() but obviously that isn't a fix.
The code seems ok in the none minified stylesheet but on inspection in Chrome, this is what is being output:
.backdrop {
position: absolute;
top: 0;
left: 0;
width: 100%;
display: block;
overflow: hidden;
max-width: 100%;
}
.backdrop::before {
content: "";
float: left;
padding-top: 100%;
}
#media screen and (orientation: landscape) {
.backdrop {
display: block;
overflow: hidden;
max-width: 100%;
}
.backdrop::before {
content: "";float:left;padding-top:56.25%}.app-bar-spacer-3{height:250px!important;background-color:#0f0}
There's actually more but as you can see all the code following is being output inside the curly brackets belonging to the pseudo element.
I've looked at it so long I'm not sure whether it's my code or LESS.
Anyone advise?
After much, much testing, I found the root cause of the issue.
It does in fact eminate from code further up the stylesheet, but doesn't actually break until it hits the above code.
Here's the code that broke LESS:
.md-text-tab {
&:not(:last-child)::after {
content:'\\';
}
}
More specifically, it's the escaped slash.

What does “#keyframe doesn't cascade" mean?

I was reading this, it says
#keyframes rules don't cascade, so animations never derive keyframes
from more than one rule set.
what does "cascade" mean here? English is not my native language and there is no more detailed explanation so I don't understand what it means. Can anyone explain this with an example?
An example of CSS cascading: -
h1 {
font-size: 12px;
width: 200px; /* Sets width */
}
h1 {
font-size: 14px; /* Overrides 12px rule above */
height: 200px; /* Sets height */
}
In the above example the h1 elements font size is first set to 12px in the first rule and then overridden to be 14px by the second rule. The width is set in the first rule and the height is set in the second rule. This is cascading: multiple rules determine the final styles applied, with priority given to properties in the rules descending order.
An example of Keyframes cascading
/* WILL NOT CASCADE */
#keyframes exampleAnimation {
0% { top: 0; left: 0; margin: 10px; }
100% { top: 100px; margin: 20px; }
}
#keyframes exampleAnimation {
0% { top: 0; left: 0; }
100% { top: 0; left: 100px; }
}
The above example will not cascade. That is to say, only the last rule declaration is used for the animation. The animation will move the animating element 100px to the left, it will ignore the top and margin animations set in the previous rule declaration.

SCSS - merging two the same selectors? [duplicate]

I'm trying to contain general styles/tricks in a separate mixin file which can be applied to any project when they're needed. Some of these styles require multiple elements to work together in order to work.
For example:
_mixins.scss
====================
#mixin footer_flush_bottom {
html {
height: 100%;
}
body {
min-height: 100%;
position: relative;
}
#footer {
position: absolute;
bottom: 0;
}
}
main.scss
====================
#import "mixins";
#include footer_flush_bottom;
html {
background-color: $bg;
//More stuff
}
body {
margin: 0 auto;
//More stuff
}
footer.scss
====================
#footer {
height: 40px;
}
As it is, the mixin works but the generated css separates the mixin from the main code, even when their selectors are the same. The downside to this is ugly css and larger file size when I start including more of these.
/* line 14, ../../sass/modules/_mixins.scss */
html {
height: 100%; }
/* line 18, ../../sass/modules/_mixins.scss */
body {
min-height: 100%;
position: relative; }
/* line 22, ../sass/modules/_mixins.scss */
#footer {
position: absolute;
bottom: 0; }
/* line 19, ../../sass/modules/main.scss */
html {
overflow-y: scroll; }
/* line 37, ../../sass/modules/main.scss */
body {
margin: 0 auto;
/* line 1, ../sass/modules/footer.scss */
#footer {
height: 40px;
Is there anyway I can do this so that same selectors can be merged? Like this:
/* line 19, ../../sass/modules/main.scss */
html {
height: 100%;
overflow-y: scroll; }
/* line 37, ../../sass/modules/main.scss */
body {
min-height: 100%;
position: relative;
margin: 0 auto;
/* line 1, ../sass/modules/footer.scss */
#footer {
position: absolute;
bottom: 0;
height: 40px;}
No. Sass has no way of merging selectors (this could be considered undesirable, as it would alter the ordering of the selectors).
The only thing you can really do is something like this (or write 2 separate mixins):
#mixin footer_flush_bottom {
height: 100%;
body {
min-height: 100%;
position: relative;
#content;
}
}
html {
// additional html styles
#include footer_flush_bottom {
// additional body styles
}
}

Overwriting rules in Sass [duplicate]

I created the carousel and I need to override styles Indicators buttons. I have style:
.carousel-indicators {
position: absolute;
bottom: 10px;
left: 50%;
z-index: 1;
width: 60%;
padding-left: 0;
margin-left: -30%;
text-align: center;
list-style: none;
display: inline-block;
}
and the need to get:
.carousel-indicators {
z-index: 1;
padding-left: 0;
list-style: none;
display: inline-block;
}
How do I override styles or remove the default?
You mean styles to its default css?
.carousel-indicators {
z-index: 1;
padding-left: 0;
list-style: none;
display: inline-block;
/*lets override other properties*/
position: static;/*or relative*/
width: auto;
margin: 0;
text-align: left;
}
top, left aren't required to modify as it is using static position won't sense for those
You should also keep an eye in which order your scripts are loaded to get everything working correctly. If you overwrite CSS, the overrided code should be loaded at last. Also interesting is to make use of important:
.exampleClass {
margin: 0 !important;
}
.exampleClass {
margin: 5px;
}
The first one will overwrite the second one so that .exampleClass will have a margin of 0 because with !important you can tell the browsers that this directive has a higher weight than the others. But keep in mind that CSS will use the logical loading order of the code when you've multiplice important-statements because in this case the browser can't know which of them is more important than the other one.
Which Bootstrap version are you using?
If it's the CSS version, simply write your styles with a more specific selector. For example:
#your-carousel .carousel-indicators {
/* your styles*/
}
If you use the LESS version of Bootstrap (the option I always recommend), you can easily change it in the carousel.less file and compile to the CSS version.

How to override bootstrap style

I created the carousel and I need to override styles Indicators buttons. I have style:
.carousel-indicators {
position: absolute;
bottom: 10px;
left: 50%;
z-index: 1;
width: 60%;
padding-left: 0;
margin-left: -30%;
text-align: center;
list-style: none;
display: inline-block;
}
and the need to get:
.carousel-indicators {
z-index: 1;
padding-left: 0;
list-style: none;
display: inline-block;
}
How do I override styles or remove the default?
You mean styles to its default css?
.carousel-indicators {
z-index: 1;
padding-left: 0;
list-style: none;
display: inline-block;
/*lets override other properties*/
position: static;/*or relative*/
width: auto;
margin: 0;
text-align: left;
}
top, left aren't required to modify as it is using static position won't sense for those
You should also keep an eye in which order your scripts are loaded to get everything working correctly. If you overwrite CSS, the overrided code should be loaded at last. Also interesting is to make use of important:
.exampleClass {
margin: 0 !important;
}
.exampleClass {
margin: 5px;
}
The first one will overwrite the second one so that .exampleClass will have a margin of 0 because with !important you can tell the browsers that this directive has a higher weight than the others. But keep in mind that CSS will use the logical loading order of the code when you've multiplice important-statements because in this case the browser can't know which of them is more important than the other one.
Which Bootstrap version are you using?
If it's the CSS version, simply write your styles with a more specific selector. For example:
#your-carousel .carousel-indicators {
/* your styles*/
}
If you use the LESS version of Bootstrap (the option I always recommend), you can easily change it in the carousel.less file and compile to the CSS version.

Resources