css – SASS,何时扩展?

前端之家收集整理的这篇文章主要介绍了css – SASS,何时扩展?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在使用一个使用SASS的团队.我看到我们扩展的样式很简单,对我来说,我看不到这样做的好处.我错过了什么吗?

以下是导入并在其他sass文件中使用的_Common.scss的一些示例:

  1. .visibility-hidden{visibility: hidden;}
  2. .display-inline { display: inline; }
  3. .display-inline-block { display: inline-block; }
  4. .display-block { display: block; }
  5. .display-none { display: none; }
  6. .display-Box { display: Box; }
  7.  
  8. .float-left { float: left; }
  9. .float-right { float: right; }
  10. .clear-both { clear: both; }
  11.  
  12. .width-percent-100 { width: 100%; }
  13. .width-percent-65 { width: 65%; }
  14. .width-percent-50 { width: 50%; }
  15. .width-percent-45 { width: 45%; }
  16. .width-percent-40 { width: 40%; }
  17. .width-percent-33 { width: 33%; }
  18. .width-percent-30 { width: 30%; }
  19. .width-percent-20 { width: 20%; }
  20.  
  21. .height-percent-100 { height: 100%; }
  22.  
  23. .cursor-pointer { cursor: pointer; }
  24.  
  25. .underline { text-decoration: underline; }
  26. .text-decoration-none { text-decoration: none; }
  27. .bold { font-weight: bold; }
  28. .font-weight-normal { font-weight: normal; }
  29. .text-align-center { text-align: center; }
  30. .text-align-left { text-align: left; }
  31. .text-align-right { text-align: right; }
  32.  
  33. .font-10 { font-size: 10px; }
  34. .font-11 { font-size: 11px; }
  35. .font-12 { font-size: 12px; }
  36. .font-13 { font-size: 13px; }
  37. .font-14 { font-size: 14px; }
  38. .font-15 { font-size: 15px; }
  39. .font-16 { font-size: 16px; }
  40. .font-17 { font-size: 17px; }
  41. .font-18 { font-size: 18px; }
  42.  
  43. .font-percent-65 { font-size: 65%; }
  44. .font-percent-80 { font-size: 80%; }
  45. .font-percent-90 { font-size: 90%; }
  46. .font-percent-100 { font-size: 100%; }
  47. .font-percent-110 { font-size: 110%; }
  48. .font-percent-120 { font-size: 120%; }
  49. .font-percent-130 { font-size: 130%; }
  50. .font-percent-140 { font-size: 140%; }
  51. .font-percent-150 { font-size: 150%; }
  52. .font-percent-160 { font-size: 160%; }
  53. .font-percent-170 { font-size: 170%; }
  54. .font-percent-180 { font-size: 180%; }

例:

  1. #CategoriesContainer
  2. {
  3. ul{
  4. li{
  5. &:first-child{
  6. @extend .font-11;
  7. }
  8. a
  9. {
  10. @extend .font-11;
  11. @extend .text-decoration-none;
  12. }
  13. }
  14. }
  15. }

解决方法

只有当您具有将被多次使用的特定属性集时,才应使用扩展.用一个具有单位值的属性的类扩展类的纯粹愚蠢是不可理解的.

reference guide中可以找到一个更好的扩展原因的例子

说我们有2个班

  1. .error {
  2. border: 1px #f00;
  3. background-color: #fdd;
  4. }
  5. .serIoUsError {
  6. border-width: 3px;
  7. }

.错误是一般的没有有趣的风格,但严重的错误应该是很清楚的.

创建了.serIoUsError来增加行,唯一的问题是现在我们必须使用html中的这两个类来组合样式.

因为我们很懒惰,只想使用一个类,而不是将来可能会更改的重复代码,我们可以用.error来扩展.serIoUsError

  1. .serIoUsError {
  2. @extend .error;
  3. border-width: 3px;
  4. }

现在我们没有在我们的sass文件中复制代码,但在页面上获得了正确的样式.

查看参考指南了解更多/更好的例子.

只是为了小猫停止扩展类与一个属性类.并且不要隐式地声明选择器中的值/属性,这不是非常语义.

你和你的团队应该是read this post,它解释了你在这里采取的aproach与语义代码的一些问题.找不到一个更好的调整发布这个快速.

猜你在找的CSS相关文章