简体   繁体   English

有没有办法在 SASS 样式表中组合和优化选择器?

[英]Is there a way to combine and optimize selectors in SASS stylesheets?

Is there any good practices or tools for merging similar selectors and optimizing SCSS source?有没有什么好的做法或工具可以合并类似的选择器和优化 SCSS 源?

For example having this:例如有这个:

#left .menu 
{
     //Content a 
}

#left .menu span
{
     //Content b 
}

#left .menu 
{
     //Content c 
}

Turn into this:变成这样:

#left .menu 
{
     //Content a
     //Content c 

     span
     {
          //Content b 
     }

}

It's tedious to do this by hand, especially for larger stylesheets where the structure might not be so apparent.手动执行此操作很乏味,尤其是对于结构可能不那么明显的较大样式表。 One could put more effort into writing leaner and cleaner SCSS, but it should be some tidy SCSS tool out there, or is there a best practice I'm missing?可以付出更多努力来编写更精简、更干净的 SCSS,但它应该是一些整洁的 SCSS 工具,或者是否有我遗漏的最佳实践?

I would agree with the comment by @cimmanon on your question, but if you must, try this css2sass converter .我同意@cimmanon 对您的问题的评论,但如果必须,请尝试使用此 css2sass 转换器

The input I gave it:我给它的输入:

#left .menu 
{
     //Content a 
}

#left .menu span
{ 
     //Content b 
}

#left .menu 
{
     //Content c 
}

The output:输出:

#left .menu {
  //Content a
  span {
    //Content b

  }
  //Content c
}

sass-convert should meet your needs: sass-convert应该可以满足您的需求:

$ sass-convert --to scss your-example.css $ sass-convert --to scss your-example.css

#left .menu {
  //Content a
  span {
    //Content b

 }
 //Content c
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM