CSS 为什么在scss/css中文件名前面加“_”或“_”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34889962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why put in front of the file name "_" or "_" in scss/css?
提问by Niko_D
Why put _
in front of the filename in scss?
_
scss中为什么要放在文件名前面?
_filename.scss
- Why does it need _
?
_filename.scss
- 为什么需要_
?
采纳答案by Fabian Parra
The _ (underscore) is a partial for scss. That means the stylesheet its going to be imported (@import) to a main stylesheet i.e. styles.scss. The advantage on using partials is that you can use many files to organize your code and everything will be compiled on a single file.
_(下划线)是 scss 的一部分。这意味着样式表将被导入(@import)到主样式表,即styles.scss。使用 partials 的优势在于您可以使用多个文件来组织您的代码,并且所有内容都将被编译到一个文件中。
回答by cameronjonesweb
A sass file starting with an underscore is a partial. It is a good way to keep your styles separated into logical sections. These files all get merged on compilation when you use @import
.
以下划线开头的 sass 文件是部分文件。这是将样式分隔为逻辑部分的好方法。当您使用@import
.
From the Sass language guide:
来自 Sass 语言指南:
You can create partial Sass files that contain little snippets of CSS that you can include in other Sass files. This is a great way to modularize your CSS and help keep things easier to maintain. A partial is simply a Sass file named with a leading underscore. You might name it something like _partial.scss. The underscore lets Sass know that the file is only a partial file and that it should not be generated into a CSS file. Sass partials are used with the @import directive.
您可以创建部分 Sass 文件,其中包含可以包含在其他 Sass 文件中的小 CSS 片段。这是模块化 CSS 并有助于使事情更易于维护的好方法。部分只是一个以下划线开头的 Sass 文件。您可以将其命名为 _partial.scss。下划线让 Sass 知道该文件只是部分文件,不应将其生成为 CSS 文件。Sass 部分与 @import 指令一起使用。
回答by dmcshehan
When you include "_" in front of the file name, it won't be generated into CSS unless you import it into another sass files which is not partial.
当你在文件名前包含“_”时,它不会被生成到 CSS 中,除非你将它导入到另一个非部分的 sass 文件中。
suppose your folder structure is like this
假设你的文件夹结构是这样的
/scss
style.scss
_list.scss
/css
if you run the command
如果你运行命令
sass --watch scss:css
only style.css and style.css.map files will be created, sass compiler will omit _list.scss without converting its content into a CSS file.
只会创建 style.css 和 style.css.map 文件,sass 编译器将省略 _list.scss 而不将其内容转换为 CSS 文件。
/scss
style.scss
_list.scss
/css
style.css
style.css.map
the only way that you can use partials is to import them into another .scss file with
您可以使用部分的唯一方法是将它们导入另一个 .scss 文件
@import 'list.scss';
if you remove the '_' in front of _list.scss the outcome of the command will be
如果删除 _list.scss 前面的“_”,命令的结果将是
/scss
style.scss
list.scss
/css
style.css
style.css.map
list.css
list.css.map
The main purpose of using partials is to break down our CSS code into several pieces which are easier to maintain. Hope this helps. Thanks.
使用 partials 的主要目的是将我们的 CSS 代码分解成更易于维护的几部分。希望这可以帮助。谢谢。
回答by Tahi Reu
Files with _ (underscore) are ignored by compiler. However, all those files are imported into single, main SCSS file (i.e. styles.scss) which is actually the file that is compiled (it doesn't have _ (underscore) in it's name)
带有 _(下划线)的文件被编译器忽略。但是,所有这些文件都导入到单个主 SCSS 文件(即 style.scss)中,该文件实际上是编译的文件(名称中没有 _(下划线))
The final goal is to compile only one SCSS file, and to have only one CSS file as a result of that, which has various advantages.
最终的目标是只编译一个 SCSS 文件,因此只有一个 CSS 文件,这具有各种优点。
回答by Joachim
Also using the watcher of node-sass in a node environment will result in error messages if you do without the underscore prefix, see https://github.com/sass/node-sass/issues/2762
如果没有下划线前缀,在节点环境中使用 node-sass 的观察者也会导致错误消息,请参阅https://github.com/sass/node-sass/issues/2762