首页 > web前端 > css教程 > 正文

SASS 中的文件分割

PHPz
发布: 2023-09-01 14:29:02
转载
839人浏览过

sass 中的文件分割

SASS is a CSS pre-processor, that stands for Syntactically Awesome Style Sheet. The SASS code is written just like a scripting language like JavaScript, but at the time of compilation it is converted into CSS and compiled as CSS in the browser. SASS can be used with any version of CSS. It is used to enhance and advance the way of writing the code in normal CSS.

在正常的工作空间中,我们习惯于将整个代码写在一个单独的文件中,这使得我们的代码对其他开发人员来说难以阅读和理解。SASS允许我们将文件拆分并将代码分成多个文件。

The process of splitting a file includes the breaking of a one big file into multiple sub files and then link them with each other, which can be easily done by using the below methods in SASS −

  • By using @import and partials

  • 通过使用 @use 和 partials

让我们现在详细了解上述方法,通过代码示例来链接SASS中单个文件的多个子文件。

通过使用@import和部分文件

In this method, we will write the styles as we normally writes in CSS files. But there will be a common file that contains the @import statement for all the other files to include or link them together and get the code of all those files in that file.

After all the sub files are linked or included into the common file, you need to run the below command in the same folder where all SASS files exists −

Sass –-watch common_file_name.scss final_output_file_name.scss
登录后复制

The above command will link or include the whole code of the common SASS file into the final output file that will be linked to the HTML document to style the page.

让我们通过代码示例详细讨论上述方法的实现方式 -

步骤

  • Step 1 − In this step, we will create multiple SASS file with .scss extension

  • 第二步 - 现在,我们将创建一个包含在上一步中创建的所有SASS文件的@import语句的SASS文件。

  • 第三步 - 在最后一步中,我们将使用上述命令将公共文件包含或链接到最终的CSS文件中,然后将其与HTML文档链接。

Explanation

的中文翻译为:

解释

  • File 1 − let us create a file named test.scss and put some SASS code inside that file.

test.css −

div{
   color: #fff;
   background: #444;
   margin: 15px;
}
登录后复制
  • File 2 − Now, create a file named common.scss. This file will link all the sub files using the @import statement.

common.scss −

@import "test.scss";
div{
   font-size: 22px;
   font-weight: bold;
   padding: 15px;
}
登录后复制
  • 文件3 − 这将是我们的最终文件final.css,其中包含所有的SASS代码,并且将链接到HTML文档。

Run below command −

sass –-watch common.scss final.css
登录后复制
登录后复制

final.css −

final.css:
/* imported code from test.scss */
div{
   color: #fff;
   background: #444;
   margin: 15px;
}
/* code from common.scss */
div{
   font-size: 22px;
   font-weight: bold;
   padding: 15px;
}
登录后复制

Now, we can link the final.css file with the HTML document to style the page with the CSS of all the SASS files as done in the below example.

Example

The below example will you how you can create and link multiple SASS file together and style a page −

<html>
<head>
   <style>
      /* imported code from test.scss */
      div{
         color: #fff;
         background: #444;
         margin: 15px;
      }
      /* code from common.scss */
      div{
         font-size: 22px;
         font-weight: bold;
         padding: 15px;
      }
   </style>
</head>
<body>
   <div>
      <h2>This is Heading of First Div.</h2>
   </div>
   <div>
      <h2>This is Heading of Second Div.</h2>
   </div>
</body>
</html>
登录后复制

In the above example, we have used the final final.css file to style the document with all the styles of SASS files.

注意 - 请确保您的系统中已经预先安装了SASS,以实现上述代码示例。

通过使用@use和局部文件

使用@use方法嵌入样式与@import方法几乎相似。您只需要在文件名前面加上下划线作为前缀,并使用@use语句导入它们。这还将允许我们访问在SASS文件中定义的函数和混合。

Explanation

的中文翻译为:

解释

  • File 1 − File 1 will be a SASS file that contains the functions, mixins and simple CSS styles defined with a underscore as prefix.

  • _test.scss −

    @function my_space(){
       $padding: "15px";
       return $padding;
    }
    
    登录后复制
    • 文件 2 − 这将是一个常见的文件,使用 @use 语句将所有的 SASS 文件链接在一起。

    common.scss

    @use "test";
    div{
       color: #fff;
       padding: test.my_space();
    }
    
    登录后复制
    • 文件3 − 这个文件是最终的CSS文件,它是来自所有SASS文件的所有样式的最终版本。

    Run below command −

    sass –-watch common.scss final.css
    
    登录后复制
    登录后复制

    final.css −

    /* combined code from both files */
    div{
       color: #fff;
       padding: 15px;
    }
    
    登录后复制

    In this way you can implement the SASS by splitting the files and add styles to the HTML document with a final outputting CSS file.

    在本文中,我们学习了两种将拆分的SASS文件链接或嵌入到一个单独文件中,并使用该最终的CSS文件向我们的HTML页面添加样式的方法。

以上就是SASS 中的文件分割的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号