I am trying to write a LESS mixin with multiple input parameter values for CSS transforms. The input values are the type of transformation to be done and the value associated with the transformation.
For example, consider the code given below:
.transform(#type; #value){
}
If I give input as type='rotateY' and value='360deg', the output should be transform: rotateY(360deg). I have tried the below options but none of them seem to be working (output is mentioned as comment).
transform: "#{type}(#{value})"; /* Output: "rotateY(360deg)" */
transform: #{type}(#{value}); /* Output: Compiler error */
transform: #type(#value); /* Output: rotateY 360deg */
How should I code it to get the output as required? Please help.
Note: The mixin code has a lot of other items also, I have posted just the line that needs fixing.
Just escape the string using ~ like shown below. Doing this would make sure that the quotes are not printed in the output CSS.
Input Code:
transform: ~"#{type}(#{value})";
or
transform: e("#{type}(#{value})");
Mixin Call:
.transform(rotateY;360deg);
Output CSS:
transform: rotateY(360deg);
Related
Do you know any possibility to combine a heaxadecimal css variable with an alpha value to create a simple "shine through effect"?
e.g. as css variable:
--my-color-red: #ff0000;
Now we want in our scss implementation to add this alpha value as a background and following code snippet doesn't work:
background: rgba(var(--my-color-red), 0.5);
Without a css variable it works:
background: rgba(#ff0000, 0.5);
I think maybe the problem is that sass do the rgba implementation at compile time and the css variable is added at runtime afterwards.
Do you know any solution to solve this problem?
You could use a Sass function which converts hexadecimal to RGB:
#function hexToRGB($hex) {
#return red($hex), green($hex), blue($hex);
}
Then store another version of your CSS variable as RGB:
--my-color-red: #ff0000;
--my-color-red-rgb: #{hexToRGB(#ff0000)};
This will give you the RGB values which you can then tack the alpha value onto within the rgba function:
rgba(var(--my-color-red-rgb), 0.5)
Taken from this article.
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.
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!
In my LESS file, I have this:
:root{
--base-color: red;
}
In my project --base-color may change "on the fly" from a user input so every instance of red may become for example green.
The problem is I have some LESS functions for applying tint, shadow or saturations so I'm trying to do something like this:
.tint{
color: tint(var(--base-color), 80%);
}
But I receive this error:
Error: error evaluating function tint: color2.toHSL is not a
function
Obviously I can't store --base-color in a less variables because I would lose the instance of the variable, so color: tint(#base-color, 80%) is not an acceptable answer.
Is there a way to keep the instance of --base-color in my css minified field?
Thanks.
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...