سؤال

أتساءل عما إذا كان من الممكن استخدام مجموعة مع SASS لأنني أجد نفسي تكرر النوع التالي من الشيء:

.donkey
  h2
    background-color= !donkey

.giraffe
  h2
    background-color= !giraffe

.iguana
  h2
    background-color= !iguana
هل كانت مفيدة؟

المحلول

لا، هذا غير ممكن. أفضل طريقة للقيام بذلك هي تحديد Mixin:

+animal-h2(!name, !color)
  .#{name} h2
    background-color= !color

ثم يمكنك الحصول على سطر واحد لكل نمط، بدلا من ثلاثة:

+animal-h2("donkey", !donkey)
+animal-h2("giraffe", !giraffe)
+animal-h2("iguana", !iguana)

نصائح أخرى

إطلاقا.

$animals: "donkey", "giraffe", "iguana"
@foreach $animal in $animals
  .#{$animal}
    h2
      background-color: !#{$animal}

إجابة NEX3 صحيحة. للحصول على هذا العمل مع SASS ON RAIRS 3.1 كنت بحاجة إلى أن يكون لديك ما يلي في ملف CSS.SSS:

$donkey: #CC4499;

@mixin animal-h2($name, $color) {
  .#{$name} h2 {
    background-color: $color;
  }
}

@include animal-h2("donkey", $donkey);
@include animal-h2("horse", #000);

أي خرج:

.donkey h2 {
    background-color: #CC4499;
}

.horse h2 {
    background-color: black;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top