{"id":310536,"date":"2024-05-20T21:52:32","date_gmt":"2024-05-20T13:52:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/310536.html"},"modified":"2024-05-20T21:52:40","modified_gmt":"2024-05-20T13:52:40","slug":"%e6%80%8e%e4%b9%88%e6%a0%b7%e4%bd%bf%e7%94%a8-scss-%e8%bd%ac%e6%8d%a2-css-%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/310536.html","title":{"rendered":"\u600e\u4e48\u6837\u4f7f\u7528 scss \u8f6c\u6362 css \u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26231113\/0bf2f1ba-d36d-42b0-a00c-e7e410e670d2.webp\" alt=\"\u600e\u4e48\u6837\u4f7f\u7528 scss \u8f6c\u6362 css \u6587\u4ef6\" \/><\/p>\n<p><p><strong>SCSS\uff08Sassy CSS\uff09\u662f\u4e00\u79cd CSS \u9884\u5904\u7406\u5668\u8bed\u8a00<\/strong>\uff0c\u5b83\u8ba9\u4f60\u80fd\u4f7f\u7528\u53d8\u91cf\u3001\u5d4c\u5957\u89c4\u5219\u3001\u6df7\u5408\u5bbd\u5ea6\u3001\u51fd\u6570\u7b49\u4f18\u70b9\u5728CSS\u4e2d\u3002\u4f7f\u7528SCSS\u8f6c\u6362CSS\u6587\u4ef6\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<strong>\u8bbe\u7f6e\u4e00\u4e2a\u5de5\u4f5c\u73af\u5883\u3001\u7f16\u5199SCSS\u4ee3\u7801\u3001\u4f7f\u7528\u7f16\u8bd1\u5668\u5c06SCSS\u8f6c\u6362\u4e3aCSS\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5173\u952e\u7684\u4e00\u6b65\u662f\u8bbe\u7f6e\u4e00\u4e2a\u53ef\u4ee5\u7f16\u8bd1SCSS\u7684\u73af\u5883\uff0c\u53ea\u6709\u8fd9\u6837\uff0c\u6211\u4eec\u624d\u80fd\u786e\u4fddSCSS\u4ee3\u7801\u80fd\u591f\u65e0\u7f1d\u8f6c\u6362\u4e3a\u6d4f\u89c8\u5668\u53ef\u8bc6\u522b\u7684CSS\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u7f16\u8bd1\u5668\u8fdb\u884c\u8f6c\u6362\u901a\u5e38\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u64cd\u4f5c\u6216\u8005\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u652f\u6301\u7684\u63d2\u4ef6\u6765\u5b8c\u6210\uff0c\u5f88\u591a\u73b0\u4ee3\u524d\u7aef\u5f00\u53d1\u5de5\u5177\u90fd\u5185\u7f6e\u4e86\u5bf9SCSS\u7684\u652f\u6301\uff0c\u5982Visual Studio Code\u3001WebStorm\u7b49\u3002\u5b89\u88c5\u76f8\u5e94\u7684\u63d2\u4ef6\u540e\uff0c\u4fbf\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8fdb\u884c\u7f16\u8bd1\u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u4e00\u4e9b\u81ea\u52a8\u5316\u6784\u5efa\u5de5\u5177\uff08\u5982Gulp\u3001Webpack\uff09\u4e5f\u53ef\u4ee5\u914d\u7f6e\u4e13\u95e8\u7684\u4efb\u52a1\u6765\u5904\u7406SCSS\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4ece\u73af\u5883\u642d\u5efa\u3001\u7f16\u5199SCSS\u5230\u6700\u7ec8\u5c06\u5176\u8f6c\u6362\u6210CSS\u7684\u6574\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u73af\u5883\u642d\u5efa<\/h3>\n<\/p>\n<p><p><strong>\u5b89\u88c5Node.js<\/strong>\uff1a<\/p>\n<p>\u5b89\u88c5Node.js\u662f\u56e0\u4e3a\u5927\u591a\u6570SCSS\u5230CSS\u7684\u8f6c\u6362\u5de5\u5177\u90fd\u662f\u7528Node.js\u7f16\u5199\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5Sass<\/strong>\uff1a<\/p>\n<p>\u901a\u8fc7npm\uff08Node.js\u7684\u5305\u7ba1\u7406\u5668\uff09\u5b89\u88c5Sass\u3002\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">npm install -g sass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199SCSS<\/h3>\n<\/p>\n<p><p>\u5728\u4f60\u7684\u5de5\u4f5c\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684<code>.scss<\/code>\u6587\u4ef6\u3002\u4f8b\u5982\uff0c<code>styles.scss<\/code>\u3002\u7136\u540e\u5728\u8be5\u6587\u4ef6\u4e2d\u7f16\u5199\u4f60\u7684SCSS\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u53d8\u91cf<\/strong>\uff1a<\/p>\n<p>\u5728SCSS\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u53d8\u91cf\u6765\u5b58\u50a8\u989c\u8272\u3001\u5b57\u4f53\u6808\u6216\u4efb\u4f55CSS\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-scss\">$primary-color: #3bbfce;<\/p>\n<p>$margin: 16px;<\/p>\n<p>.content-navigation {<\/p>\n<p>  border-color: $primary-color;<\/p>\n<p>  color: darken($primary-color, 10%);<\/p>\n<p>}<\/p>\n<p>.border {<\/p>\n<p>  padding: $margin \/ 2;<\/p>\n<p>  margin: $margin \/ 2;<\/p>\n<p>  border-color: $primary-color;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5229\u7528\u5d4c\u5957<\/strong>\uff1a<\/p>\n<p>SCSS\u5141\u8bb8\u4f60\u5728\u4e00\u4e2a\u9009\u62e9\u5668\u5185\u90e8\u5d4c\u5957\u53e6\u4e00\u4e2a\u9009\u62e9\u5668\u6765\u4ee3\u8868\u5b50\u5143\u7d20\uff0c\u8fd9\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u5e76\u51cf\u5c11\u91cd\u590d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-scss\">.nav {<\/p>\n<p>  ul {<\/p>\n<p>    margin: 0;<\/p>\n<p>    padding: 0;<\/p>\n<p>    list-style: none;<\/p>\n<p>  }<\/p>\n<p>  li { display: inline-block; }<\/p>\n<p>  a {<\/p>\n<p>    display: block;<\/p>\n<p>    padding: 6px 12px;<\/p>\n<p>    text-decoration: none;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4eceSCSS\u8f6c\u6362\u4e3aCSS<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u547d\u4ee4\u884c\u7f16\u8bd1<\/strong>\uff1a<\/p>\n<p>\u6253\u5f00\u4f60\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5207\u6362\u5230\u5305\u542b<code>.scss<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\u3002\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5c06SCSS\u6587\u4ef6\u8f6c\u6362\u4e3aCSS\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">sass styles.scss styles.css<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u65b0\u7684<code>styles.css<\/code>\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4eceSCSS\u8f6c\u6362\u800c\u6765\u7684CSS\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>\u8bbe\u7f6e\u5b9e\u65f6\u7f16\u8bd1<\/strong>\uff1a<\/p>\n<p>\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u5b9e\u65f6\u7f16\u8bd1\u7684\u8fc7\u7a0b\uff0c\u8fd9\u6837\u6bcf\u5f53\u4f60\u4fdd\u5b58<code>.scss<\/code>\u6587\u4ef6\u65f6\uff0cCSS\u6587\u4ef6\u5c31\u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">sass --watch styles.scss:styles.css<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u914d\u7f6e\u81ea\u52a8\u5316\u5de5\u5177<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528Gulp<\/strong>\uff1a<\/p>\n<p>\u9996\u5148\u5b89\u88c5Gulp CLI\u548cGulp Sass\u63d2\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">npm install --global gulp-cli<\/p>\n<p>npm install --save-dev gulp gulp-sass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u521b\u5efa\u4e00\u4e2a<code>gulpfile.js<\/code>\u6765\u914d\u7f6e\u81ea\u52a8\u7f16\u8bd1\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const gulp = require(&#039;gulp&#039;);<\/p>\n<p>const sass = require(&#039;gulp-sass&#039;)(require(&#039;sass&#039;));<\/p>\n<p>gulp.task(&#039;sass&#039;, function () {<\/p>\n<p>  return gulp.src(&#039;.\/sass\/\/*.scss&#039;)<\/p>\n<p>    .pipe(sass().on(&#039;error&#039;, sass.logError))<\/p>\n<p>    .pipe(gulp.dest(&#039;.\/css&#039;));<\/p>\n<p>});<\/p>\n<p>gulp.task(&#039;watch&#039;, function () {<\/p>\n<p>  gulp.watch(&#039;.\/sass\/\/*.scss&#039;, gulp.series(&#039;sass&#039;));<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u5f53\u4f60\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u8fd0\u884c<code>gulp watch<\/code>\u65f6\uff0c\u4efb\u4f55\u5728<code>sass<\/code>\u6587\u4ef6\u5939\u4e0b\u7684<code>.scss<\/code>\u6587\u4ef6\u66f4\u6539\u90fd\u4f1a\u89e6\u53d1Sass\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Webpack<\/strong>\uff1a<\/p>\n<p>\u82e5\u4f60\u4f7f\u7528\u7684\u662fWebpack\uff0c\u9700\u8981\u914d\u7f6e\u76f8\u5e94\u7684loader\u6765\u5904\u7406Sass\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">module.exports = {<\/p>\n<p>  \/\/ ...<\/p>\n<p>  module: {<\/p>\n<p>    rules: [<\/p>\n<p>      {<\/p>\n<p>        test: \/\\.scss$\/,<\/p>\n<p>        use: [<\/p>\n<p>          &#039;style-loader&#039;, \/\/ \u5c06JS\u5b57\u7b26\u4e32\u751f\u6210\u4e3astyle\u8282\u70b9<\/p>\n<p>          &#039;css-loader&#039;,   \/\/ \u5c06CSS\u8f6c\u5316\u6210CommonJS\u6a21\u5757<\/p>\n<p>          &#039;sass-loader&#039;   \/\/ \u5c06Sass\u7f16\u8bd1\u6210CSS<\/p>\n<p>        ]<\/p>\n<p>      }<\/p>\n<p>    ]<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662f\u4f7f\u7528SCSS\u8f6c\u6362CSS\u6587\u4ef6\u7684\u57fa\u672c\u6d41\u7a0b\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u7684\u9879\u76ee\u9700\u6c42\u548c\u504f\u597d\u8fdb\u884c\u9009\u62e9\u548c\u8c03\u6574\u3002\u901a\u8fc7\u9884\u5904\u7406\u5668\u589e\u5f3a\u4e86CSS\u7684\u529f\u80fd\uff0c\u540c\u65f6\u4e5f\u63d0\u5347\u4e86\u5f00\u53d1\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. SCSS\u548cCSS\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\nSCSS\u662fSass\uff08Syntactically Awesome Style Sheets\uff09\u7684\u4e00\u79cd\u6269\u5c55\uff0c\u5b83\u63d0\u4f9b\u4e86\u6bd4\u539f\u751fCSS\u66f4\u591a\u7684\u529f\u80fd\u548c\u4fbf\u5229\u6027\u3002\u4e0e\u4f20\u7edf\u7684CSS\u76f8\u6bd4\uff0cSCSS\u5177\u6709\u5d4c\u5957\u89c4\u5219\u3001\u53d8\u91cf\u3001\u6df7\u5408\u548c\u7ee7\u627f\u7b49\u7279\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u9ad8\u6548\u5730\u7f16\u5199\u6837\u5f0f\u4ee3\u7801\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5c06SCSS\u6587\u4ef6\u8f6c\u6362\u4e3aCSS\u6587\u4ef6\uff1f<\/strong><br \/>\n\u8981\u5c06SCSS\u6587\u4ef6\u8f6c\u6362\u4e3aCSS\u6587\u4ef6\uff0c\u60a8\u9700\u8981\u4f7f\u7528Sass\u7f16\u8bd1\u5668\u6765\u8fdb\u884c\u8f6c\u6362\u3002\u60a8\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u4e5f\u53ef\u4ee5\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u4f7f\u7528\u6784\u5efa\u5de5\u5177\uff08\u4f8b\u5982Webpack\u6216Gulp\uff09\u6765\u81ea\u52a8\u7f16\u8bd1SCSS\u6587\u4ef6\u4e3aCSS\u6587\u4ef6\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9009\u62e9\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u5168\u5c40\u5b89\u88c5Sass\u7f16\u8bd1\u5668\uff0c\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sass input.scss output.css\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code>input.scss<\/code>\u662f\u60a8\u7684SCSS\u6587\u4ef6\u8def\u5f84\uff0c<code>output.css<\/code>\u662f\u8981\u751f\u6210\u7684CSS\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<p>\u5982\u679c\u60a8\u4f7f\u7528\u6784\u5efa\u5de5\u5177\uff0c\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u76f8\u5e94\u7684\u4efb\u52a1\uff0c\u4ee5\u5b9e\u73b0\u81ea\u52a8\u7f16\u8bd1SCSS\u6587\u4ef6\u4e3aCSS\u6587\u4ef6\u3002\u5177\u4f53\u7684\u914d\u7f6e\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003\u76f8\u5e94\u7684\u6784\u5efa\u5de5\u5177\u6587\u6863\u3002<\/p>\n<p><strong>3. \u662f\u5426\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528SCSS\u800c\u4e0d\u662fCSS\u6587\u4ef6\uff1f<\/strong><br \/>\n\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u4e3a\u4e86\u63d0\u9ad8\u7f51\u7ad9\u6027\u80fd\u548c\u52a0\u8f7d\u901f\u5ea6\uff0c\u901a\u5e38\u4f1a\u4f18\u5316CSS\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7\u538b\u7f29CSS\u4ee3\u7801\u3001\u5408\u5e76\u591a\u4e2aCSS\u6587\u4ef6\u3001\u4f7f\u7528CSS\u9884\u5904\u7406\u5668\u751f\u6210\u7cbe\u7b80\u7684CSS\u7b49\u65b9\u6cd5\u6765\u4f18\u5316CSS\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u4f7f\u7528SCSS\u6765\u7f16\u5199\u548c\u8c03\u8bd5\u6837\u5f0f\u4ee3\u7801\uff0c\u800c\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u5c06\u5176\u8f6c\u6362\u4e3a\u4f18\u5316\u540e\u7684CSS\u6587\u4ef6\uff0c\u5e76\u5728\u7f51\u9875\u4e2d\u5f15\u7528\u8fd9\u4e9bCSS\u6587\u4ef6\u3002\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u5f00\u53d1\u6548\u7387\u548c\u7f51\u7ad9\u6027\u80fd\u7684\u5e73\u8861\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"SCSS\uff08Sassy CSS\uff09\u662f\u4e00\u79cd CSS \u9884\u5904\u7406\u5668\u8bed\u8a00\uff0c\u5b83\u8ba9\u4f60\u80fd\u4f7f\u7528\u53d8\u91cf\u3001\u5d4c\u5957\u89c4\u5219\u3001\u6df7\u5408\u5bbd\u5ea6\u3001\u51fd\u6570\u7b49\u4f18\u70b9\u5728 [&hellip;]","protected":false},"author":3,"featured_media":310547,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/310536"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=310536"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/310536\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/310547"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=310536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=310536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=310536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}