Using negative values fails the execution in SASS - css

I have a couple of SCSS selectors where I use the below code which works fine:
.order-summary{
background-color: #e2f0f3;
padding: $padding-large-horizontal+5;
}
but whenever I change to below code (as negative value) it shows error
.order-summary{
background-color: #e2f0f3;
padding: $padding-large-horizontal-1;
}
$padding-large-horizontal is already defined as base 12px;
Any help would be appreciated. Thanks.

Just use like below
padding: ($padding-large-horizontal - 1);

The problem is only due to the dash. SASS thinks that $padding-large-horizontal-1 is the name of the variable. You have to separate as well:
$padding-large-horizontal -1

Related

CSS variables and SASS functions

We know the advantages of using CSS4 variables but what if we need to get these values from a SASS function like so?:
:root {
--gap-l: toRem(10);
}
toRem is a Sass function that I call to get the sizes dynamically:
$fontSize: 16;
#function toRem($val) {
#return $val / $fontSize * 1.6 + 0rem;
}
This won't fail but won't work either. To have this working we can just have the value directly on --gap-l or keep using SASS vars.
If I try something like --gap-l: #{toRem(10)}; this is the error I get:
It doesn't call the SASS function
You can definitely do that: what you're missing is simply using string interpolation, i.e.:
:root {
--gap-l: #{toRem(10)};
}
The reason is highlighted in their "breaking changes" documentation with regards to CSS variables:
To provide maximum compatibility with plain CSS, more recent versions of Sass require SassScript expressions in custom property values to be written within interpolation. Interpolation will also work for older Sass versions, and so is recommended for all stylesheets.
Try this --gap-l: #{toRem(10)};, the #{} syntax is called interpolation. In the experience of heading bugs myself, when you want to use a SASS expression's raw value along with normal CSS, if you can't use the concise syntax like just toRem(10), try adding interpolation to see if it works.
Another example:
$padding: 5px;
.element {
width: calc(100% - $padding); // will not work, it must be:
width: calc(100% - #{$padding});
}
Here is the code with the updated question: https://jsfiddle.net/bcw763en/.
Notice that if you put :root above the #function, it'll not work.

Less loop missing ")" when compiling

I have been working on a way to build a carousel purely with html and css.
No Javascript.
So far I have been liking what I found in the web and seen some tutorials.
Here is my issue though.
I build a mixin loop with Less to build a bunch of css but for some reason it seems to be missing a closing brace ")" on line 4 (of the pasted code below).
What I tried:
Remove the block of Less code completely -> error dissapeared.
Removed all the code inside the .carousel-reviews -> error persists
removed the the .carousel-reviews around the child selector -> error persists
Changed the variable name from #i to #index -> error persists
Removed all the code from inside the &__activator:nth-of-type( #i ) selector -> error persists
Hope someone can see what I am doing wrong here.
.loop( #i ) when ( #i > 0 ) {
.carousel-reviews {
&__activator:nth-of-type( #i ) {
&:checked ~ .carousel_track {
transform: translateX(calc(#i - 1) * 100%);
}
&:checked ~ .carousel__slide:nth-of-type(#i) {
transition: opacity #slideTransition, transform #slideTransition;
top: 0;
left: 0;
right: 0;
opacity: 1;
transform: scale(1);
}
&:checked ~ .carousel__controls:nth-of-type(#i) {
display: block;
opacity: 1;
}
&:checked ~ .carousel__indicators .carousel__indicator:nth-of-type(#i) {
opacity: 1;
}
}
}
.loop ( ( #i - 1 ) );
}
If I was not complete enough please let me know and I can add the info to the question.
EDIT 1
It seems that the compilers are stopping when they get to the first #i on line 4.
For some reason when I remove that first variable the error moves to line 8.
This suggests that for some reason the variable #i is not allowed inside the :nth-of-type().
Anyone know what is going on here? I will keep searching and updating when I find new answers or questions
EDIT 2
Found the sollution. Check answer
So it seems that I found the issue.
It seems that the problem is with less itself regarding the use of variables inside the :nth-of-type().
When I was removing the variables from the nth-of-type I noticed the error moving to a new line that also included the nth-of-type.
When I went to look up the use of variables in less I couldn't find anything bu later I came across a post here in stack overflow
THIS ANSWER BY MARTIN TURJAK
I would advice to check it out.
But in short, there seems to be an issue with the variable use and you have to use it as if you are using it inside a string like this :nth-of-type(#{i}).
Hope this helps others strugling with this same issue.
I currently don't have the time to find out why this happens an I haven't got a clue either but if there is someone that can explain this that would be awesome.
Anyways, thanks for your time and have a great day!

How come I'm getting unexpected name or hex number error?

I'm wondering why I can't add background-color: red; property on my scss file:
#my_container {
background-color: red; # Added this line and #efefef doesn't work either
Error print on Grunt:
src/sass/app_2/_common/layout/layouts.scss:7:27
✖ 7:27 Unexpected named color red color-named
It might be an error coming from Styelint or compiler. Please let me know if you need any detail.
That error comes from Stylelint because you have a rule that disables named colors: https://stylelint.io/user-guide/rules/color-named/

Invalid Sass variable when using number (with and without px)

I am using Sass for a project and I ran into an issue with variables. I'm specifically using the ".sass" format and not ".scss"
The error says: Invalid variable: "$weightBold = 700"
I can use variables for colors like this "$darkGrey: #333333" without errors, but I do get an error when I try to use a number for font-weight "$weightBold = 700" or width "$mediumWidth = 300px"
There is nothing wrong with the number, use : instead of =
You need to use : instead of = to assign a value to the variables:
$weightBold: 700;
$darkGrey: #333333;
$mediumWidth: 300px;
You can see some examples on the official documentation:
The most straightforward way to use SassScript is to use variables. Variables begin with dollar signs, and are set like CSS properties.
$width: 5em;

LESS.js + #arguments + Skip One Argument

I have been searching in Google etc., but I couldnt find what I was looking for (I hope I didnt overlook something).. So I thought my best bet is to ask you guys :)
I am playing around with LESS-JS for the first time and I really like it. However I have a little problem now.
I am using the #arguments variable like this:
.basicBorder(#width:1px, #type:solid, #color:#black){
border:#arguments;
}
Which works as expected. Now when I want the border to be red, I am adding this to the element in my css:
.basicBorder(1px, solid, #red);
Which also works as expected. However I would like to avoid writing 1px, solid,, since these are my default values already, but when I try this:
.basicBorder(#red);
Or this:
.basicBorder(,,#red);
It doesnt work.
So I was wondering if any1 knows how I could "skip" the first 2 variables so that I can just input the color in case I dont want the border-width and type to be changed.
I hope you get what I am trying to say!
Regards!
You actually can name later parameters and skip the first ones. The syntax for your question is:
.basicBorder(#color:#red);
You can also use normal ordered arguments at the beginning and pluck out named arguments from the rest of the parameters:
.basicBorder(2px, #color:#red);
This sets #width to 2px, #type to the default, and #color to #red. Really nice if you have more seldom used arguments.
The parametric mixins in LESS works sorta like javascript functions, you can't skip the first parameters. So if you want to only change the color, you could rewrite the mixin like this:
.basicBorder(#color:#black, #width:1px, #type:solid){
border:#width #type #color;
}
Then you'd be able to call it like this:
.basicBorder(#red);
.basicBorder(#red, 2px, dotted);
edit
Using your original mixin, you could also create these
.basicBorderType(#type) {
.basicBorder(1px, #type, #black);
}
.basicBorderColor(#color) {
.basicBorder(1px, solid, #color);
}
Now you could overwrite any of the styles:
.basicBorderType(dotted); //1px dotted black;
.basicBorderColor(#red); //1px solid red;
.basicBorder(2px); //2px solid black;
A bit of a hack, but it's the only thing I can think of to help you out...

Resources