I want to add !important to a mixin. I tried both the following ways, and they return an error:
#include linear-gradient(hsl(68%, 94%, 90%), hsl(68%, 90%, 80%)); !important
#include linear-gradient(hsl(68%, 94%, 90%), hsl(68%, 90%, 80%)) !important;
Is there a way to do this correctly?
For some situations, I use a optional parameter named $important which I can pass in true.
Example:
my-mixin($important: true);
It would look something like that, with a helper function to avoid repetition on the properties that I have to toggle important:
#function if-important($important){
#return #{if($important, '!important', '')};
}
#mixin my-mixin($important: false) {
border-radius: 0; //whatever
border: 1px solid #ccc if-important($important);
background-color: #fff if-important($important);
color: #000 if-important($important);
}
!important cannot be used in a mixin. Refer the following links.
Adding !important using a Compass Mixin
https://github.com/cloudhead/less.js/issues/547
):
You cannot use !important on a Mixin.
it will end up giving you a SASS syntax error.
See this question for more information
Try passing it as a parameter:
#mixin anim($params...){
$values: null;
#for $i from 0 to length($params) {
#debug #{nth($params, $i + 1)};
#if '#{nth($params, $i + 1)}' != '!important' {
$values: #{nth($params, $i + 1)} $animation__time $animation__easing, $values;
}
}
#if '#{nth($params, length($params))}' != '!important' {
transition: $values;
}
#else {
transition: $values !important;
}
}
Usage:
#include anim(background-color, color, !important);
Related
Any reason why this isn't working?
:root {
--color-white: 0 0% 100%;
}
color: hsla(var(--color-white) 1);
I'm getting:
SCSS processing failed: file "_page.scss", line 5, col 16: Function hsla is missing argument $saturation.
I also tried
color: #{hsla(var(--color-white) 1)};
which still does not work.
Try like below. You need comma with hsla() using the old syntax
:root {
--color-white: 0, 0%, 100%;
}
.box {
color: hsla(#{var(--color-white), 1});
}
Or use the new syntax where you need / before the alpha
:root {
--color-white: 0 0% 100%;
}
.box {
color: hsl(#{var(--color-white) / 100%});
}
Reference: https://www.w3.org/TR/css-color-4/#the-hsl-notation
SASS attempts to compile the styles with a SASS-specific hsla() function. However, hsla() is also a native CSS function, so you can use string interpolation to bypass the SASS function.
:root {
--color-white: 0 0% 100%;
}
div {
/* This is valid CSS, but will fail in a scss compilation */
color: hsla(var(--color-white) 1);
/* This is valid scss, and will generate the CSS above */
color: #{'hsla(var(--color-white) 1)'};
}
Taken from this answer on SO.
hsla() takes four inputs and the punctuation needs to be accurate or none of it will work. No hash tags are needed:
--color: 0, 100%, 50%;
color: hsla(var(--color), 1);
This 100% works (pun intended).
Please, add commas between --color-white's percentages and after passing the var (before '1' in color).
This solution should work:
:root {
--color-white: 0, 0%, 100%;
}
color: hsla(var(--color-white), 1);
It looks like rgb() works without commas, but hsla() needs commas.
See here: http://codepen.io/Aleksgrunwald/pen/abpQQrr
I have the following sass code which is giving error. Please help!
main.scss
#function alpha($background, $color, $font-size){
return {
background: $background,
color: $color,
font-size: $font-size
}
}
div {alpha(yellow, violet, 24);}
p {alpha(blue, orange, 20);}
Error:
fatal error: parse error: failed at background: $background, (stdin) on line 3
Note: I know this can be done my Mixin. But, what I read is that
Mixins & Functions can be used inter-changably. So, I want to do this
work by Functions only (to see if they are really do all work of
mixins).
Thanks in advance!
You are mixing mixins and functions here.
Mixins provide for (nested) rules and values while function only return values.
What you need is a mixin like here
#mixin alpha($background, $color, $font-size) {
background: $background;
color: $color;
font-size: $font-size;
}
div {#include alpha(yellow, violet, 24);}
p {#include alpha(blue, orange, 20);}
Explanation
This is how you use Mixins
#mixin red-align($text-align: left) {
color: red;
text-align: $text-align
}
body h1 {
#include red-align(center);
}
This is how you use Functions
#function red($opacity: 1) {
#return rgba(255, 0, 0, $opacity);
}
#function align($text-align: left) {
#return $text-align;
}
body h1 {
color: red();
text-align: align(center);
}
Both examples yield:
body h1 {
color: red;
text-align: center;
}
Previously in SCSS (version 3.4.21), I can use variable interpolation to get a specific item from a map:
$colors: (
color-1: #aaa,
color-2: #bbb,
color-3: #ccc
);
#mixin color($shade) {
color: map-get($colors, #{color-}$shade );
}
.element {
#include color(2);
}
Compiles to:
.element {
color: #bbb;
}
Playground Link
In sass 4 (alpha), it doesn't work (or i'm screwing something up):
#{color-}$shade
It doesn't allow me to do that. Is there a reason this was taken out?
I think your syntax is just a bit backwards.
#mixin color($shade) {
color: map-get($colors, gray-#{$shade} );
}
I'm new to Sass, so if this isn't the best way of doing this type of thing, I apologise!
So, I've got a mixin for button style, like so:
#mixin button ($bg, $color, $padding, $display: inline, $radius: 0, $transition: 0.2s) {
background: $bg;
#if $radius > 0 {
#include border-radius($radius)
}
color: $color;
#if $display != inline {
display: $display;
}
#if $padding > 0 {
padding: $padding;
}
text-decoration: none;
#include transition(all, $transition, linear);
&:hover {
#if lightness($bg) > 50% {
background: darken($bg, 10%);
} #else {
background: lighten($bg, 10%);
}
}
}
and a button, like so:
.btn {
#include button(#095d94, #fff, 10px, inline-block);
}
But, now I need another button with a different background colour. So what I'm wondering is: is there a way to extend a class, and also just change an argument of the mixin that that class includes, without having to do this:
.btn2 {
#extend .btn;
background: #bad78d;
&:hover {
background: darken(#bad78d, 10%);
}
}
Is it possible to feed in another background colour? Something like,
.btn2 {
$bg: #bad78d; //i know this doesn't work
#extend .btn;
}
or like,
.btn2 ($bg: #bad78d) {
#extend .btn; //this one doesn't even make sense, but I think I'm explaining what I need... ish.
}
I think you have two options here.
Also you try to keep it dry, there is nothing too wrong about repading sometimes. So if your mixin is not too huge it'll be ok to this:
.btn {
#include button(#095d94, #fff, 10px, inline-block);
}
.btn2 {
#include button(#bad78d, #fff, 10px, inline-block);
}
But This will only be required if the difference between .btn and .btn2 is big.
If you just want to change certain properties, you may also just use the classig cascading.
.btn,.btn2 {
#include button(#095d94, #fff, 10px, inline-block);
}
.btn2 {
background-color:#bad78d;
...
}
Is there a way to add scope to sass variables?
I want to be able to attach a class to my body element. The class will refer to a set of colours that the rest of the stylesheets can access.
I have tried:
#mixin theme_one{
$color: #000;
}
.theme_one{
#include theme_one;
}
and
.theme_one{
$color: #000;
}
I've just come across the same issue myself. I wanted to have different colour themes for different sections of my site.
Using a mixin seems like the best way to go. It's nicely DRY, and easy to use. The trick is not setting your colours in your main styles blocks, but rather using only the mixin for this.
I've set up the theme colours as variables at the top so they can be edited nicely, and I've set them as lists so that multiple values can be passed without hordes of variable being defined.
So:
// Variable Definitions
$defaultColor: black white grey;
$color2: blue green brown;
$color3: red white blue;
#mixin colorSet($color: $defaultColor) {
$link: nth($color, 1);
$border: nth($color, 2);
$background: nth($color, 3);
border-color: $border;
background-color: $background;
.column {
border-color: lighten($border, 10%);
}
a {
color: $link;
&:hover {
color: darken($link, 15%);
}
}
}
// Default colours
body {
#include colorSet();
}
// Scoped colours
.my-theme-3 {
#include colorSet($color3);
}
.my-theme-2 {
#include colorSet($color2);
}
Will produce something like this:
body {
border-color: white;
background-color: grey; }
body .column {
border-color: white; }
body a {
color: black; }
body a:hover {
color: black; }
.my-theme-3 {
border-color: white;
background-color: blue; }
.my-theme-3 .column {
border-color: white; }
.my-theme-3 a {
color: red; }
.my-theme-3 a:hover {
color: #b30000; }
.my-theme-2 {
border-color: green;
background-color: brown; }
.my-theme-2 .column {
border-color: #00b300; }
.my-theme-2 a {
color: blue; }
.my-theme-2 a:hover {
color: #0000b3; }
Edit: Updated to use default mixin values.
In your case no need to use mixin, If you have set of many styles then use mixin,
ie. if you have
#mixin theme_one{
$color: #000;
height: 50px;
}
then use Mixin
otherwise for single property use only variable
$color: #fff;
.some_class01{
color: $color;
background: $color;
}
.some_class22{
border-color: $color;
}
IMP: Variable should assign at the top of your code, it means don't use it after/below where you assigned it :)
Not sure if this is what you are looking for. It looks like you may have tried something similar to this,
which should probably work. (it may just be a matter of using !default)
Your body tag with a class on it..
<body class="theme_one">
</body>
Sass variables defined in stylesheet..
//THEME ONE VARIABLES
.theme_one{
$borderColor:#333 !default;
$fontColor:#999 !default;
}
//THEME TWO VARIABLES
.theme_two{
$borderColor:#CCC !default;
$fontColor:#000 !default;
}
Pre-existing CSS which will be overwritten depending on which class is used on the body tag.
h1.someheader {
color:$fontColor;
border-bottom:1px solid;
border-color:$borderColor;
}
Otherwise you could maybe try something like this. It looks like you may have tried something similar, however there seems to be an error with your mixin ... see note below.
//mixin used to set variables for properties
#mixin themeOne($fontColor,$borderColor) {
color:$fontColor;
border-color:$borderColor;
}
#include themeOne(#000,#CCC);
Pre-existing CSS
h1.someheader {
color:$fontColor
border-color:$borderColor;
border-bottom:1px solid;
}
Also note in your mixin example you are using $color:#000; ... This won't be interpreited properly as it should be color:#000; You can't use variables as selectors
unless you do something like #{$color}:#000;
I haven't quite tested this yet, so some things might need to be adjusted. If this doesn't solve your problem I hope it at least gives you some ideas.