简体   繁体   English

SASS / SCSS用相对路径sass cli编译css文件

[英]SASS/SCSS compile css file with relative paths sass cli

I'm having issues compiling scss with relative paths. 我在用相对路径编译scss时遇到问题。

Here is my file structure: 这是我的文件结构:

src/
├── css/
│   ├── bootstrap.scss
│   ├── main.scss
├── fonts/
│   ├── fonts.css
│   ├── font1.woff2
├── index.html
├── main.css    /* Compiled css */

main.scss: main.scss:

@import './bootstrap-init';
@import '../fonts/fonts';

fonts.css: fonts.css:

@font-face {
  font-family: 'font1';
  src: url('./font1.woff2') format('woff2');
}

If you look at my fonts.css , src: url() , the url is pointing to the right file which is src/fonts/font1.woff2 . 如果您查看我的fonts.csssrc: url() ,则该URL指向正确的文件src/fonts/font1.woff2 However, after when its compiled to src/main.css , src: url() now points to src/font1.woff2 which does not exits. 但是,在将其编译为src/main.csssrc: url()现在指向不会退出的src/font1.woff2

A similar question asked here for LESS: LESS importing CSS and relative paths 在这里向LESS询问类似的问题: LESS导入CSS和相对路径

I'm using sass CLI and this is what I have at the moment: 我正在使用sass CLI,这是我目前所拥有的:

sass --watch css/main.scss main.css

How would I go about fixing this issue? 我将如何解决此问题? I can replace my paths to always point from the root folder, but that doesn't seem like a good approach... 我可以替换路径以始终指向根文件夹,但这似乎不是一个好方法……

You are mixing up CSS imports and SASS imports. 您正在混合CSS导入和SASS导入。

The @import statement in a CSS file will cause the browser to download the imported css file as if it were another separate <style> tag in your document. CSS文件中的@import语句将使浏览器下载导入的css文件,就好像它是文档中的另一个单独的<style>标记一样。 So all the paths in that imported file are relative to itself . 因此,该导入文件中的所有路径都是相对于其自身的

The @import statement in a SCSS file will cause the SASS compiler to compile the imported css file into your main sass output as if you had copied and pasted it in there. SCSS文件中的@import语句将使SASS编译器将导入的css文件编译到您的主sass输出中,就好像您已在其中复制并粘贴了该文件一样。 So the paths in a SASS file are always relative to the outputted file . 因此,SASS文件中的路径始终相对于输出文件

As mentioned in the comments, it's a good idea not to mix .CSS files into your .SCSS files. 如评论中所述,最好不要将.CSS文件混合到您的.SCSS文件中。 Rather always use SASS for everything and you can use SASS variables to store the path to the font files. 而是始终对所有内容都使用SASS,并且您可以使用SASS变量来存储字体文件的路径。

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

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