Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use grandparent selector in scss

Tags:

css

sass

I need to style my button component in 5 states with differently colored icon and use for this following css

#add-member-dialog #add-username-to-list .to-list-button-icon:before {
    content: url("../images/func_personplus_16_ena.png");
}
#add-member-dialog #add-username-to-list:hover .to-list-button-icon:before {
    content: url("../images/func_personplus_16_hov.png");
}
#add-member-dialog #add-username-to-list:disabled .to-list-button-icon:before {
    content: url("../images/func_personplus_16_dis.png");
}
#add-member-dialog #add-username-to-list:active .to-list-button-icon:before {
    content: url("../images/func_personplus_16_act.png");
}
#add-member-dialog #add-username-to-list:active:hover .to-list-button-icon:before {
    content: url("../images/func_personplus_16_onb.png");
}

such items differs in pseudoclasses related to #add-username-to-list. I tried to switch entire css file to scss and wanted to optimize this style but I was not able move further than:

#add-member-dialog {
  #add-username-to-list {
    .to-list-button-icon:before {
      content: url("../images/func_personplus_16_ena.png");
    }
    &:hover .to-list-button-icon:before {
      content: url("../images/func_personplus_16_hov.png");
    }
    &:disabled .to-list-button-icon:before {
      content: url("../images/func_personplus_16_dis.png");
    }
    &:active .to-list-button-icon:before {
      content: url("../images/func_personplus_16_act.png");
    }
    &:active:hover .to-list-button-icon:before {
      content: url("../images/func_personplus_16_onb.png");
    }
  }
}

Is it possible to do something like this?

#add-member-dialog {
  #add-username-to-list {
    .to-list-button-icon:before {
      content: url("../images/func_personplus_16_ena.png");

      &&:hover & {
        content: url("../images/func_personplus_16_hov.png");
      }
      ...
    }
  }
}

where && will represents grandparent selector #add-username-to-list.

I also tried to apply pattern #{$grandparent}:tmp $ but the result selector looked like this: #add-member-dialog #add-username-to-list:tmp #add-member-dialog #add-username-to-list .to-list-button-icon:before

#add-member-dialog {
  #add-username-to-list {
    $grandparent: &;

    .to-list-button-icon:before {
      content: url("../images/func_personplus_16_ena.png");

      #{$grandparent}:hover & {
        content: url("../images/func_personplus_16_hov.png");
      }
      ...
    }
  }
}

Any advice whether is this possible?

like image 271
Jan Stanicek Avatar asked Oct 15 '25 14:10

Jan Stanicek


1 Answers

You can use interpolation to print out your grandparent selector (note the @at-root):

#add-member-dialog {
    #add-username-to-list {
        $g: &;                               // grandparent
        $p: '../images/func_personplus_16_'; // icon path
        @at-root {
            .to-list-button-icon:before { 
                #{$g} &          { content: url(#{$p}ena.png); }                
                #{$g}:hover &    { content: url(#{$p}hov.png); }
                #{$g}:disabled & { content: url(#{$p}dis.png); }
                #{$g}:active &   { content: url(#{$p}act.png); }                
                #{$g}:active:hover &   { content: url(#{$p}onb.png); }                                
            }
        }
    }
}
like image 81
Jakob E Avatar answered Oct 17 '25 09:10

Jakob E