如何使用Sass正确避免在HTML上嵌入Twitter Bootstrap类名称

css CSS

Tom小宇宙

2020-03-24

我正在一个刚刚开始的Rails项目中工作。我们希望使用twitter bootstrap作为样式的基础,一开始,我们将直接在HTML代码上直接使用bootstrap的类名,就像bootstrap的文档中所示,但是在阅读了以下文章之后:

可维护CSS的经验教训

请停止在您的HTML中嵌入Bootstrap类

很清楚为什么不使用引导程序是不正确的,所以在读了一些之后:

将CSS与HTML分离

smacss

除其他外,似乎使用sass @extend是避免使用bootstrap的类名的正确方法,所以不要这样做:

<button type="submit" class="btn">Search</button>

我们可以这样做:

<button type="submit" class="button">Search</button>

并且我们的sass代码如下所示:

.button {
   @extend ".btn";
}

The problem with that approach, besides the bunch of extra selectors that will be added each time we extend a bootstrap class just to use a different name, is that in cases where bootstrap uses selectors like this:

.form-search .input-append .btn, .form-search .form-input-append .btn {
  border-radius: 0 14px 14px 0;
} 

the button won't get the right style because sass will not apply that same rule to our custom class name, I mean, sass is not doing this:

.form-search .input-append .btn, .form-search .input-append .button, 
.form-search .form-input-append .btn, .form-search .form-input-append .button {
  border-radius: 0 14px 14px 0;
} 

So I wonder, is this the right way to avoid embedding bootstrap's class names into HTML, if so, how should I handle this problem (it happens frequently)? if not, what would be a better way to use custom class names but still get the styles from bootstrap.

I really appreciate your thoughts on this. Please keep in mind that I am just learning about overall web design (css, sass, less, html, js, etc.).

第3354篇《如何使用Sass正确避免在HTML上嵌入Twitter Bootstrap类名称》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

4个回答
小胖Gil 2020.03.24

对于大多数新手来说,您是在正确的道路上;您一直在阅读的资源非常棒。但是,我认为您担心的问题可能没有道理:

...按钮将无法获得正确的样式,因为sass不会将相同的规则应用于我们的自定义类名称...

实际上,我认为您一定会误会。根据Sass文档:

@extend通过将扩展选择器(例如.seriousError)插入样式表中扩展选择器(例如.error)出现的任何地方来工作。

有关完整的代码示例,请参见http://sass-lang.com/documentation/file.SASS_REFERENCE.html#how_it_works

您原来的解决方案实际上是正确的。使用扩展,但不带引号:

.button {
  @extend .btn; //no quotes
}

我使用您的示例对此进行了测试,并且可以正常工作。Sass用“ .btn”复制所有选择器,在这些新创建的选择器上,用“ .button”替换“ .btn”。

另外,如果Sass为您的喜好产生过多的重复,也不必担心(只要您遵循发布的链接所指出的最佳实践)即可。如果服务器使用gzip或等效文件,则很容易压缩重复代码;客户端应该不会注意到加载速度变慢,尽管“解压缩” CSS可能会花费更长的时间。至于CSS选择器的速度,也不必担心。速度对选择器至关重要的唯一情况是在JavaScript方面,尤其是jQuery。对于您的Sass代码,只需遵循关于可维护性的最佳实践(尤其是在尝试模块化时,即SMACSS),那么一切都会很好。

小胖理查德 2020.03.24

使用@extend,但不要引用选择器:

.button { @extend .btn; }

然后,您会看到Sass也像您想要的那样扩展了相关的选择器(.form-search .input-append .btn等)。

…除了每次我们扩展引导类以使用其他名称时都会添加的额外选择器之外…

@extend通过复制选择器起作用。如果您不希望这样做,可以改为在HTML中“扩展”-即添加Bootstrap的类名。:)

阿飞米亚 2020.03.24

当您将.btn重命名为.button时,还应该将.form-search重命名为.form-searchnew等吗?在这种情况下,上面示例中的sass代码应类似于:

.form-searchnew .input-appendnew .button {
  extend(.form-search .input-append .btn);
}

这是有道理的(我不知道SASS),并导致您期望的CSS。

我认为引导程序不仅仅与CSS有关。Bootstrap是关于CSS,html(structure)和javascript的。即使您将css与html分开,我也不容易迁移到其他框架。除了CSS外,您还必须更改html结构和javascript调用。

示例从Twitter的Bootstrap 2迁移到3(请参阅:将Bootstrap更新到版本3-我必须做什么?)。我还想知道是否可以通过将旧的类扩展到新的CSS来进行迁移(请参阅:http : //bassjobsen.weblogs.fm/migrate-your-templates-from-twitter-bootstrap-2-x-to-twitter-bootstrap -3 /)。阅读迁移指南后,我认为您不能。

其他解决方案。Angular JS将Twitter的Bootstrap与javascript分离。同样在这种情况下,迁移似乎也并非一帆风顺,请参阅:Bootstrap 3的Angular Dialog指令

也许还会阅读这篇文章:http : //www.jasonwong.org/post/45849350459/migrating-from-zurb-foundation-twitter-bootstrap-to它指的是BourdonNeat

他们网站上的示例:

<!-- HTML markup for the section right below this code block -->
<section>
  <aside>What is it about?</aside>
  <article>Neat is an open source semantic grid framework built on top of Sass and Bourbon…</article>
</section>

// Enter Neat
section {
  @include outer-container;
  aside { @include span-columns(3); }
  article { @include span-columns(9); }
}
// And the result is...

正如他们所说:“它完全依赖于Sass mixins,不会污染HTML”,这似乎是您所寻找的方式。

GreenSam 2020.03.24

我建议您看看sass占位符类http://sass-lang.com/documentation/file.SASS_REFERENCE.html#placeholders,以免使您的CSS蒙上阴影。很可能您不会使用引导程序中包含的每个元素,并且占位符只有在代码中实际进行了扩展时才会写入样式表。

另外,我认为人们倾向于对css框架如何工作以及css和html实际如何去耦感到困惑。对于非常大的网站(或您最终希望变得非常大的网站),性能和css文件大小至关重要,那么最好的选择是OOCSS。这不可避免地意味着您直接在HTML中设置了格式代码。

如果您可以降低效率并希望HTML干净,请确保使用语义类(按钮示例:号召性用语,号召性用语辅助,提交,btn-primary,btn-企业颜色等)

还请记住将JS与CSS分离!使用特殊的类来附加行为(例如js-submit,js-call-to-action等)。

最后但并非最不重要的一点:不要计划更新CSS框架。这些框架旨在为您提供领先优势,而不是成为您的整体设计解决方案。扩展它们,使它们适应,使它们成为自己的,投资设计并创建自己的外观,以使您的应用程序与众不同。如果使您想到更新的原因是跟上标准更改的烦恼,请更好地使用指南针mixins。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android