awesome-interview/book2/css-preprocessor.html

22 lines
33 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="Docusaurus v2.0.0-beta.5">
<link rel="search" type="application/opensearchdescription+xml" title="HZFE - 剑指前端 Offer" href="/awesome-interview/opensearch.xml">
<link rel="preconnect" href="https://hm.baidu.com">
<script>var _hmt=_hmt||[];!function(){var e=document.createElement("script");e.src="https://hm.baidu.com/hm.js?c7cd0fd77ac518cc6ef46461cdc9524b";var c=document.getElementsByTagName("script")[0];c.parentNode.insertBefore(e,c)}()</script><title data-react-helmet="true">谈谈 CSS 预处理器 | HZFE - 剑指前端 Offer</title><meta data-react-helmet="true" name="twitter:card" content="summary_large_image"><meta data-react-helmet="true" property="og:url" content="https://febook.hzfe.org/awesome-interview/book2/css-preprocessor"><meta data-react-helmet="true" name="docsearch:language" content="en"><meta data-react-helmet="true" name="docsearch:version" content="current"><meta data-react-helmet="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-react-helmet="true" property="og:title" content="谈谈 CSS 预处理器 | HZFE - 剑指前端 Offer"><meta data-react-helmet="true" name="description" content="相关问题"><meta data-react-helmet="true" property="og:description" content="相关问题"><link data-react-helmet="true" rel="shortcut icon" href="/awesome-interview/img/favicon.ico"><link data-react-helmet="true" rel="canonical" href="https://febook.hzfe.org/awesome-interview/book2/css-preprocessor"><link data-react-helmet="true" rel="alternate" href="https://febook.hzfe.org/awesome-interview/book2/css-preprocessor" hreflang="en"><link data-react-helmet="true" rel="alternate" href="https://febook.hzfe.org/awesome-interview/book2/css-preprocessor" hreflang="x-default"><link data-react-helmet="true" rel="preconnect" href="https://BH4D9OD16A-dsn.algolia.net" crossorigin="anonymous"><link rel="stylesheet" href="/awesome-interview/assets/css/styles.304d13b7.css">
<link rel="preload" href="/awesome-interview/assets/js/runtime~main.e4536af0.js" as="script">
<link rel="preload" href="/awesome-interview/assets/js/main.52f9823f.js" as="script">
</head>
<body>
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}()</script><div id="__docusaurus">
<div><a href="#" class="skipToContent_1oUP">Skip to main content</a></div><nav class="navbar navbar--fixed-top navbarHideable_2qcr"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Navigation bar toggle" class="navbar__toggle clean-btn" type="button" tabindex="0"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/awesome-interview/"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><b class="navbar__title">剑指前端 Offer</b></a></div><div class="navbar__items navbar__items--right"><a href="https://github.com/hzfe/awesome-interview" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link"><span>GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_3J9K"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></span></a><div class="searchBox_1Doo"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div class="main-wrapper docs-wrapper docs-doc-page"><div class="docPage_31aa"><button class="clean-btn backToTopButton_35hR" type="button"><svg viewBox="0 0 24 24" width="28"><path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z" fill="currentColor"></path></svg></button><aside class="docSidebarContainer_3Kbt"><div class="sidebar_15mo sidebarWithHideableNavbar_267A"><a tabindex="-1" class="sidebarLogo_3h0W" href="/awesome-interview/"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--light_3UqQ"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--dark_hz6m"><b>剑指前端 Offer</b></a><nav class="menu thin-scrollbar menu_Bmed menuWithAnnouncementBar_2WvA"><ul class="menu__list"><li class="menu__list-item"><a class="menu__link" href="/awesome-interview/about">关于我们</a></li><li class="menu__list-item"><a class="menu__link" href="/awesome-interview/">前言</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">模拟题一</a></li><li class="menu__list-item"><a class="menu__link menu__link--sublist menu__link--active" href="#">模拟题二</a><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/browser-render-mechanism">浏览器:浏览器渲染机制</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/browser-garbage">浏览器:垃圾回收机制</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/engineer-babel">工程化Babel 的原理</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/frame-react-fiber">框架React Fiber 的作用和原理</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/frame-react-hoc-hooks">框架HOC vs Render Props vs Hooks</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/js-inherite">基础ES5、ES6 如何实现继承</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/js-new">基础New 操作符的原理</a></li><li class="menu__list-item"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="/awesome-interview/book2/css-preprocessor">样式:谈谈 CSS 预处理器</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/network-http-cache">网络HTTP 缓存机制</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/coding-throttle-debounce">编码:实现节流去抖函数</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/algorithm-reverse-linked-list">算法:反转链表</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book2/topic-multi-pics-site-optimize">综合:多图站点性能优化</a></li></ul></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">模拟题三</a></li></ul></nav><button type="button" title="Collapse sidebar" aria-label="Collapse sidebar" class="button button--secondary button--outline collapseSidebarButton_1CGd"><svg width="20" height="20" aria-hidden="true" class="collapseSidebarButtonIcon_3E-R"><g fill="#7a7a7a"><path d="M9.992 10.023c0 .2-.062.399-.172.547l-4.996 7.492a.982.982 0 01-.828.454H1c-.55 0-1-.453-1-1 0-.2.059-.403.168-.551l4.629-6.942L.168 3.078A.939.939 0 010 2.528c0-.548.45-.997 1-.997h2.996c.352 0 .649.18.828.45L9.82 9.472c.11.148.172.347.172.55zm0 0"></path><path d="M19.98 10.023c0 .2-.058.399-.168.547l-4.996 7.492a.987.987 0 01-.828.454h-3c-.547 0-.996-.453-.996-1 0-.2.059-.403.168-.551l4.625-6.942-4.625-6.945a.939.939 0 01-.168-.55 1 1 0 01.996-.997h3c.348 0 .649.18.828.45l4.996 7.492c.11.148.168.347.168.55zm0 0"></path></g></svg></button></div></aside><main class="docMainContainer_3ufF"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_3FnS"><div class="docItemContainer_33ec"><article><div class="tocCollapsible_1PrD tocMobile_3Hoh"><button type="button" class="clean-btn tocCollapsibleButton_2O1e">On this page</button></div><div class="markdown"><header><h1>谈谈 CSS 预处理器</h1></header><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="相关问题"></a>相关问题<a class="hash-link" href="#相关问题" title="Direct link to heading">#</a></h2><ul><li>CSS 主要有哪些预处理器</li><li>为什么需要用预处理器</li><li>各预处理器优缺点</li></ul><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="回答关键点"></a>回答关键点<a class="hash-link" href="#回答关键点" title="Direct link to heading">#</a></h2><p><code>Sass</code> <code>Less</code> <code>Stylus</code> <code>PostCSS</code> <code>工程化</code> <code>提升效率</code></p><p>CSS 本身不属于可编程语言,当前端项目逐渐庞大之后 CSS 的维护也愈加困难。CSS 预处理器所做的本质上是为 CSS 增加一些可编程的特性,通过<strong>变量</strong><strong>嵌套</strong><strong>简单的程序逻辑</strong><strong>计算</strong><strong>函数</strong>等特性,通过<strong>工程化</strong>的手段让 CSS 更易维护,提升开发效率。</p><p>目前主流的 CSS 预处理器主要有 Sass、Less、Stylus、PostCSS。</p><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="知识点深入"></a>知识点深入<a class="hash-link" href="#知识点深入" title="Direct link to heading">#</a></h2><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="1-postcss1"></a>1. PostCSS<sup>[1]</sup><a class="hash-link" href="#1-postcss1" title="Direct link to heading">#</a></h3><p>PostCSS 是目前最为流行的 CSS 预/后处理器。PostCSS 本体功能比较单一,它提供一种用 JavaScript 来处理 CSS 的方式。PostCSS 会把 CSS 解析成 ASTAbstract Syntax Tree 抽象语法树),之后由其他插件进行不同的处理。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="功能"></a>功能<a class="hash-link" href="#功能" title="Direct link to heading">#</a></h4><p>PostCSS 本体功能比较单一,大多数的 CSS 处理功能都由插件提供,下面是一些常用的插件:</p><ul><li><a href="https://github.com/postcss/autoprefixer" target="_blank" rel="noopener noreferrer">Autoprefixer</a> 为 CSS 中的属性添加浏览器特定的前缀。</li><li><a href="https://github.com/csstools/postcss-preset-env" target="_blank" rel="noopener noreferrer">postcss-preset-env</a> 根据 <code>browserslist</code> 指定的目标浏览器将一些 CSS 的新特性转换为目标浏览器所支持的语法。</li><li><a href="https://github.com/cssnano/cssnano" target="_blank" rel="noopener noreferrer">cssnano</a> 提供 CSS 压缩功能。</li><li><a href="https://github.com/postcss/postcss-nested" target="_blank" rel="noopener noreferrer">postcss-nested</a> 提供 CSS 嵌套功能。</li><li><a href="https://github.com/evrone/postcss-px-to-viewport" target="_blank" rel="noopener noreferrer">postcss-px-to-viewport</a> 提供 px 转 vw 功能。</li><li><a href="https://github.com/postcss/postcss-custom-properties" target="_blank" rel="noopener noreferrer">postcss-custom-properties</a> 支持 CSS 的自定义属性。</li></ul><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="优点"></a>优点<a class="hash-link" href="#优点" title="Direct link to heading">#</a></h4><ul><li>插件系统完善,扩展性强。</li><li>配合插件功能齐全。</li><li>生态优秀。</li></ul><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="缺点"></a>缺点<a class="hash-link" href="#缺点" title="Direct link to heading">#</a></h4><ul><li>配置相对复杂。</li></ul><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="2-sass2"></a>2. Sass<sup>[2]</sup><a class="hash-link" href="#2-sass2" title="Direct link to heading">#</a></h3><p>Sass 在完全兼容 CSS 语法的前提下,给 CSS 提供了变量、嵌套、混合、操作符、自定义函数等可编程能力。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="功能-1"></a>功能<a class="hash-link" href="#功能-1" title="Direct link to heading">#</a></h4><p>Sass 常用的有几种功能:</p><ul><li>变量:变量中可以存储颜色、字体或任何 CSS 值。</li><li>嵌套:可嵌套 CSS 选择器,提供清晰的层次结构。</li><li>混合:可以定义&amp;重用代码块。</li><li>扩展/集成:可以在一个选择器内继承另一个选择器。</li><li>操作符:可以在 CSS 中使用操作符进行计算。</li><li>条件/循环语句:可以循环/条件生成 CSS。</li><li>自定义函数:可以自定义复杂操作的函数。</li></ul><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="优点-1"></a>优点<a class="hash-link" href="#优点-1" title="Direct link to heading">#</a></h4><ul><li>使用广泛。</li><li>功能支持完善。</li><li>可编程能力强。</li></ul><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="缺点-1"></a>缺点<a class="hash-link" href="#缺点-1" title="Direct link to heading">#</a></h4><ul><li>CSS 的复杂度不可控。</li><li>node-sass 国内安装不易(非 Sass 本身的缺点dart-sass 可代替)。</li></ul><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="3-less3"></a>3. Less<sup>[3]</sup><a class="hash-link" href="#3-less3" title="Direct link to heading">#</a></h3><p>Less 和 Sass 类似,完全兼容 CSS 语法,并给 CSS 提供了变量、嵌套、混合、运算等可编程能力。Less 通过 JavaScript 实现,可在浏览器端直接使用。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="功能-2"></a>功能<a class="hash-link" href="#功能-2" title="Direct link to heading">#</a></h4><p>Less 常用的有几种功能:</p><ul><li>变量:变量中可以存储颜色、字体或任何 CSS 值。</li><li>嵌套:可嵌套 CSS 选择器,提供清晰的层次结构。</li><li>混合:可以定义&amp;重用的代码块。</li><li>扩展/集成:可以在一个选择器内继承另一个选择器。</li><li>运算:可以在 CSS 中进行计算。</li><li>条件/循环语句:可以循环/条件生成 CSS。</li></ul><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="优点-2"></a>优点<a class="hash-link" href="#优点-2" title="Direct link to heading">#</a></h4><ul><li>使用广泛。</li><li>可以在浏览器中运行,容易实现主题定制功能。</li></ul><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="缺点-2"></a>缺点<a class="hash-link" href="#缺点-2" title="Direct link to heading">#</a></h4><ul><li>不支持自定义函数(可通过 mixins 实现简单逻辑)。</li><li>编程能力相对较弱。</li></ul><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="4-stylus4"></a>4. Stylus<sup>[4]</sup><a class="hash-link" href="#4-stylus4" title="Direct link to heading">#</a></h3><p>Stylus 基础功能和 Sass / Less 十分类似。Stylus 的特点是冒号、分号、逗号和括号都是可选项,所以可以写出非常简洁的 CSS示例如下</p><div class="codeBlockContainer_K1bP"><div class="codeBlockContent_hGly stylus"><pre tabindex="0" class="prism-code language-stylus codeBlock_23N8 thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_39YC"><span class="token-line" style="color:#393A34"><span class="token selector" style="color:#00009f">body</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token property-declaration property" style="color:#36acaa">background-color</span><span class="token property-declaration punctuation" style="color:#393A34">:</span><span class="token property-declaration"> </span><span class="token property-declaration hexcode">#000</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block">
</span></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token selector" style="color:#00009f">body::after</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token property-declaration property" style="color:#36acaa">content</span><span class="token property-declaration punctuation" style="color:#393A34">:</span><span class="token property-declaration"> </span><span class="token property-declaration string" style="color:#e3116c">&#x27;HZFEStudio&#x27;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token property-declaration property" style="color:#36acaa">color</span><span class="token property-declaration punctuation" style="color:#393A34">:</span><span class="token property-declaration"> </span><span class="token property-declaration hexcode">#fff</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token property-declaration property" style="color:#36acaa">font-size</span><span class="token property-declaration punctuation" style="color:#393A34">:</span><span class="token property-declaration"> </span><span class="token property-declaration number" style="color:#36acaa">20</span><span class="token property-declaration unit">px</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_Ue-o clean-btn">Copy</button></div></div><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="扩展阅读"></a>扩展阅读<a class="hash-link" href="#扩展阅读" title="Direct link to heading">#</a></h2><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="1-css-modules5"></a>1. CSS Modules<sup>[5]</sup><a class="hash-link" href="#1-css-modules5" title="Direct link to heading">#</a></h3><p>CSS Modules 和前文介绍的预处理器不同,不是可编程化的 CSS而仅是给 CSS 文件加入了作用域和模块依赖,主要是为了解决 CSS 全局污染的痛点以及为了解决全局污染而嵌套过深的问题。使用示例如下:</p><div class="codeBlockContainer_K1bP"><div class="codeBlockContent_hGly css"><pre tabindex="0" class="prism-code language-css codeBlock_23N8 thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_39YC"><span class="token-line" style="color:#393A34"><span class="token comment" style="color:#999988;font-style:italic">/* style.css */</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token selector class" style="color:#00009f">.hzfeTitle</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token property" style="color:#36acaa">color</span><span class="token punctuation" style="color:#393A34">:</span><span class="token plain"> </span><span class="token hexcode color">#666</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token property" style="color:#36acaa">font-size</span><span class="token punctuation" style="color:#393A34">:</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">20</span><span class="token unit">px</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">}</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_Ue-o clean-btn">Copy</button></div></div><div class="codeBlockContainer_K1bP"><div class="codeBlockContent_hGly javascript"><pre tabindex="0" class="prism-code language-javascript codeBlock_23N8 thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_39YC"><span class="token-line" style="color:#393A34"><span class="token keyword module" style="color:#00009f">import</span><span class="token plain"> </span><span class="token imports">style</span><span class="token plain"> </span><span class="token keyword module" style="color:#00009f">from</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">&quot;./style.css&quot;</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block">
</span></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token dom variable" style="color:#36acaa">document</span><span class="token punctuation" style="color:#393A34">.</span><span class="token property-access">body</span><span class="token punctuation" style="color:#393A34">.</span><span class="token property-access">innerHTML</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token template-string template-punctuation string" style="color:#e3116c">`</span><span class="token template-string string" style="color:#e3116c">&lt;h1 class=&quot;</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:#393A34">${</span><span class="token template-string interpolation">style</span><span class="token template-string interpolation punctuation" style="color:#393A34">.</span><span class="token template-string interpolation">hzfeTitle</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:#393A34">}</span><span class="token template-string string" style="color:#e3116c">&quot;&gt;HZFEStudio&lt;/h1&gt;</span><span class="token template-string template-punctuation string" style="color:#e3116c">`</span><span class="token punctuation" style="color:#393A34">;</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_Ue-o clean-btn">Copy</button></div></div><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="2-css-in-js"></a>2. CSS-in-JS<a class="hash-link" href="#2-css-in-js" title="Direct link to heading">#</a></h3><p>如名字所见CSS-in-JS 是一种在 JavaScript 里写 CSS 的方式。利用 JS 的优势可实现非常灵活的可扩展的样式。CSS-in-JS 有很多实现,目前比较流行的是 <a href="https://www.styled-components.com/" target="_blank" rel="noopener noreferrer">Styled-components</a></p><p>通过 Styled-components 写 CSS 的示例如下:</p><div class="codeBlockContainer_K1bP"><div class="codeBlockContent_hGly javascript"><pre tabindex="0" class="prism-code language-javascript codeBlock_23N8 thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_39YC"><span class="token-line" style="color:#393A34"><span class="token keyword module" style="color:#00009f">import</span><span class="token plain"> </span><span class="token imports maybe-class-name">React</span><span class="token plain"> </span><span class="token keyword module" style="color:#00009f">from</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">&quot;react&quot;</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token keyword module" style="color:#00009f">import</span><span class="token plain"> </span><span class="token imports">styled</span><span class="token plain"> </span><span class="token keyword module" style="color:#00009f">from</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">&quot;styled-components&quot;</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block">
</span></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token keyword" style="color:#00009f">function</span><span class="token plain"> </span><span class="token function" style="color:#d73a49">hzfe</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token keyword" style="color:#00009f">const</span><span class="token plain"> </span><span class="token maybe-class-name">Title</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> styled</span><span class="token punctuation" style="color:#393A34">.</span><span class="token property-access">h1</span><span class="token template-string template-punctuation string" style="color:#e3116c">`</span><span class="token template-string string" style="color:#e3116c"></span></span><span class="token-line" style="color:#393A34"><span class="token template-string string" style="color:#e3116c"> font-size: 1.5em;</span></span><span class="token-line" style="color:#393A34"><span class="token template-string string" style="color:#e3116c"> text-align: center;</span></span><span class="token-line" style="color:#393A34"><span class="token template-string string" style="color:#e3116c"> color: #666;</span></span><span class="token-line" style="color:#393A34"><span class="token template-string string" style="color:#e3116c"> </span><span class="token template-string template-punctuation string" style="color:#e3116c">`</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token keyword control-flow" style="color:#00009f">return</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">&lt;</span><span class="token maybe-class-name">Title</span><span class="token operator" style="color:#393A34">&gt;</span><span class="token maybe-class-name">HZFEStudio</span><span class="token operator" style="color:#393A34">&lt;</span><span class="token operator" style="color:#393A34">/</span><span class="token maybe-class-name">Title</span><span class="token operator" style="color:#393A34">&gt;</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">}</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_Ue-o clean-btn">Copy</button></div></div><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="3-tailwind-和-utility-first-css"></a>3. Tailwind 和 Utility-first CSS<a class="hash-link" href="#3-tailwind-和-utility-first-css" title="Direct link to heading">#</a></h3><p>近几年随着 Tailwind 的流行功能类优先Utility-first CSS的理念也再次流行起来。这里简单介绍一下 Tailwind CSS。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="31-tailwind6"></a>3.1 Tailwind<sup>[6]</sup><a class="hash-link" href="#31-tailwind6" title="Direct link to heading">#</a></h4><p>Tailwind CSS 是一个功能类优先的 CSS 框架通过组合不同的类名实现页面设计。Tailwind 主要优势如下:</p><ol><li>不用考虑 class 的命名。</li><li>CSS 文件大小增长可控,通过 <code>purge</code> 可生成非常小的 CSS 文件。</li><li>统一设计系统下的样式与布局。</li><li>IDE 集成优秀。</li></ol><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="参考资料"></a>参考资料<a class="hash-link" href="#参考资料" title="Direct link to heading">#</a></h2><ol><li><a href="https://postcss.org/" target="_blank" rel="noopener noreferrer">PostCSS - A tool for transforming CSS with JavaScript</a></li><li><a href="https://sass-lang.com/" target="_blank" rel="noopener noreferrer">Sass - CSS with superpowers</a></li><li><a href="https://lesscss.org/" target="_blank" rel="noopener noreferrer">Less - It&#x27;s CSS, with just a little more</a></li><li><a href="https://www.stylus.com/" target="_blank" rel="noopener noreferrer">Stylus - Foresight has never been so crucial</a></li><li><a href="https://github.com/css-modules/css-modules" target="_blank" rel="noopener noreferrer">CSS Modules</a></li><li><a href="https://tailwindcss.com/" target="_blank" rel="noopener noreferrer">Tailwind CSS</a></li></ol></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages navigation"><div class="pagination-nav__item"><a class="pagination-nav__link" href="/awesome-interview/book2/js-new"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">« 基础New 操作符的原理</div></a></div><div class="pagination-nav__item pagination-nav__item--next"><a class="pagination-nav__link" href="/awesome-interview/book2/network-http-cache"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">网络HTTP 缓存机制 »</div></a></div></nav></div></div><div class="col col--3"><div class="tableOfContents_35-E thin-scrollbar"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#相关问题" class="table-of-contents__link">相关问题</a></li><li><a href="#回答关键点" class="table-of-contents__link">回答关键点</a></li><li><a href="#知识点深入" class="table-of-contents__link">知识点深入</a><ul><li><a href="#1-postcss1" class="table-of-contents__link">1. PostCSS<sup>1</sup></a></li><li><a href="#2-sass2" class="table-of-contents__link">2. Sass<sup>2</sup></a></li><li><a href="#3-less3" class="table-of-contents__link">3. Less<sup>3</sup></a></li><li><a href="#4-stylus4" class="table-of-contents__link">4. Stylus<sup>4</sup></a></li></ul></li><li><a href="#扩展阅读" class="table-of-contents__link">扩展阅读</a><ul><li><a href="#1-css-modules5" class="table-of-contents__link">1. CSS Modules<sup>5</sup></a></li><li><a href="#2-css-in-js" class="table-of-contents__link">2. CSS-in-JS</a></li><li><a href="#3-tailwind-和-utility-first-css" class="table-of-contents__link">3. Tailwind 和 Utility-first CSS</a></li></ul></li><li><a href="#参考资料" class="table-of-contents__link">参考资料</a></li></ul></div></div></div><div class="row"><div class="col"><div class="react-utterences"><div>Loading script...</div></div></div><div class="col col--3"></div></div></div></main></div></div></div>
<script src="/awesome-interview/assets/js/runtime~main.e4536af0.js"></script>
<script src="/awesome-interview/assets/js/main.52f9823f.js"></script>
</body>
</html>