开发者

accessing parent classes name while naming child classname in SASS

开发者 https://www.devze.com 2022-12-07 19:17 出处:网络
.project{ $self=&; .card{ #{$self}__name{ font-weight:bold; } .card__body{ display:flex } }} I want to add classname "card__body" with variable some开发者_如何学Cthing like $self but he
.project{
  $self=&;
.card{
  #{$self}__name{
     font-weight:bold;
  }
.card__body{
     display:flex
  }
  }}

I want to add classname "card__body" with variable some开发者_如何学Cthing like $self but here $self denotes "project" instead of hardcoding "card__body". Thanks in advance.


we need to use interpolation in sass using & and #{&} we are using second & inside #{} because we cannot use &&

.project{
  $self=&;
  .card{
   #{$self}__name{
     font-weight:bold;
   }
   & #{&}_body{     //this will result in '.card .card_body{ }'
     display:flex;
   }

   &_body{     //this will result in '.card_body{ }' without parent nesting
     display:flex;
   }
 }
}

get more details on sass interpolation here on official docs

0

精彩评论

暂无评论...
验证码 换一张
取 消