Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you have an array inside a sass object value?

Tags:

css

sass

I'm trying to have an array inside an object's value.

$background-things: (
    'a': ['100vh', 'transparent', 'column'],
    'higher': ['70vh', '#000', 'column'],
    'lower': ['30vh', '#fff', 'row'],
);

and then later call them like this:

@each $name, $value in $background-things {
    #background {
        &-#{$name}: {
            @include column($height:$value[0], $background-color:$value[1], $flex-direction:$value[2]);
        }
    }
}

This doesn't work. Is there any way to do this? On a side note, does the value $name work, because in the object's property I put '&-a'.

like image 803
yurin Avatar asked Dec 07 '25 02:12

yurin


1 Answers

It is possible. @each part needs some modification:

  1. &-#{$name}: should convert to &-#{$name}.
  2. Access to list (array) items is possible with nth function; for example nth($value, 1).
  3. lists indexes start from 1.
  4. Also '100vh' doesn't need '.

More information about lists: https://sass-lang.com/documentation/modules/list

    @each $name, $value in $background-things {
        #background {
            &-#{$name} {
                @include column($height:nth($value, 1), $background-color:nth($value, 2), $flex-direction:nth($value, 3));
            }
        }
    }

like image 50
Amirreza Noori Avatar answered Dec 08 '25 16:12

Amirreza Noori



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!