首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SASS + SMACSS正确封装模块

SASS + SMACSS正确封装模块
EN

Stack Overflow用户
提问于 2014-03-12 21:01:17
回答 1查看 450关注 0票数 3

这是我的设置:

文件关系

代码语言:javascript
复制
home.php <---styles---- _layout.scss
   |
imports
   |
   v
animation.html <---styles---- _animation.scss

home.php -用于勾勒主页“布局”的文件:

代码语言:javascript
复制
<div id="animation">
    <div class="site-container">

        <div class="animation-container">
            <?php include 'animation.html'; ?>
        </div>

    </div>
</div>

_layout.scss -用于为home.php的非导入内容设置样式的文件:

代码语言:javascript
复制
#animation {
    //styles <div id="animation">
}

    .site-container {margin: 0 auto; max-width: 980px;}

        .animation-container {
            //styles <div class="animation-container">
        }

animation.html -包含上面导入的称为“动画”的“模块”的html。

代码语言:javascript
复制
<div class="animation-wrap">
    <div class="example-selector"></div>
    //more html for animation module
</div>

_animation.scss -在animation.html中使用html样式

问题:,我应该如何在_animation.scss中封装选择器?

Possibilities

1.)我可以在_animation.scss中嵌套所有选择器,如下所示:

代码语言:javascript
复制
.animation-wrap {

    .example-selector {

    }
    //all other selectors are nested here using SASS, thus they will not affect
    //elements outside of the animation-wrap
}

2.)我可以通过添加前缀“_animation.scss -”(以及相应的html)来命名空间中几乎所有的选择器。

代码语言:javascript
复制
.animation-wrap {}

.animation-example-selector {}

3.)可以使用子选择器来减少级联,但我怀疑这是最好的,而且IE支持很差

4.)子类化?但是,我认为这与将模块移到其他地方更相关,而不是封装它以确保它不会泄漏到其他模块/布局代码中。

对这个冗长的问题很抱歉,把它写成单词是很尴尬的.任何关于最佳实践的额外建议或知识都非常感谢

EN

回答 1

Stack Overflow用户

发布于 2014-03-13 19:33:58

对这个可怜的问题很抱歉。对于类似的问题,This是一个措辞更好的问题。

我决定使用SASS3.3全新的“&”灵活性来命名_animation.scss中的选择器,如下所示

代码语言:javascript
复制
.module-animation { 
        &-animation-wrap {

        }
}

这使html保持干净,封装了模块,并且不会使css中出现长前缀。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22363709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档