995 lines
551 KiB
HTML
995 lines
551 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html lang="zh"><head><title>CSS</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&family=Noto Serif Simplified Chinese:wght@400;700&family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="CSS"/><meta property="og:description" content="CSS 样式表."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="CSS 样式表."/><meta name="generator" content="Quartz"/><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Professional/Work/full-stack/基础/CSS"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" viewBox="0 0 100 100" style="enable-
|
|||
|
<h2 id="css">CSS<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#css" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>CSS,全称为层叠样式表(Cascading Style Sheets),是**一种用于描述 HTML 或 XML 文档样式的标记语言。**由于其简洁的语法和强大的功能,CSS 已成为前端开发的核心技术之一。</p>
|
|||
|
<h2 id="选择器">选择器<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#选择器" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>**CSS 的选择器是绑定样式到 HTML 元素的关键。**CSS 选择器大致可以分为以下几类:</p>
|
|||
|
<ul>
|
|||
|
<li><strong>简单选择器</strong>:包括元素选择器、类选择器、ID 选择器。</li>
|
|||
|
<li><strong>属性选择器</strong>:根据元素的属性选择元素。例如,<code>[attr=value]</code> 选择具有特定值的特定属性的元素,<code>[attr^=value]</code> 选择属性值以特定值开始的元素,<code>[attr$=value]</code> 选择属性值以特定值结束的元素,<code>[attr*=value]</code> 选择属性值中包含特定值的元素。</li>
|
|||
|
<li><strong>伪类和伪元素选择器</strong>:伪类选择器是基于元素的特定状态选择元素,例如 <code>:hover</code>、<code>:focus</code>、<code>:checked</code> 等。伪元素选择器是用于选择元素的特定部分,例如 <code>::before</code>、<code>::after</code>、<code>::first-line</code> 等。</li>
|
|||
|
<li><strong>组合选择器</strong>:包括子元素选择器、后代选择器、相邻兄弟选择器和一般兄弟选择器。这些选择器可以与简单选择器组合使用,以选择符合特定关系的元素。</li>
|
|||
|
<li><strong>分组选择器</strong>:用逗号分隔的列表,可以同时选择多个元素。</li>
|
|||
|
</ul>
|
|||
|
<p>常用的 CSS 选择器作用和使用示例:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th style="text-align:left;">选择器类型</th><th style="text-align:left;">作用</th><th style="text-align:left;">示例</th></tr></thead><tbody><tr><td style="text-align:left;">元素选择器</td><td style="text-align:left;">根据元素名称选择元素</td><td style="text-align:left;"><code>p { color: red; }</code></td></tr><tr><td style="text-align:left;">类选择器</td><td style="text-align:left;">根据元素的 class 属性选择元素</td><td style="text-align:left;"><code>.intro { font-size: 20px; }</code></td></tr><tr><td style="text-align:left;">ID 选择器</td><td style="text-align:left;">根据元素的 id 属性选择元素</td><td style="text-align:left;"><code>#first-paragraph { text-align: center; }</code></td></tr><tr><td style="text-align:left;">属性选择器</td><td style="text-align:left;">根据元素的属性选择元素</td><td style="text-align:left;"><code>a[target="_blank"] { color: red; }</code></td></tr><tr><td style="text-align:left;">伪类选择器</td><td style="text-align:left;">根据元素的特定状态选择元素</td><td style="text-align:left;"><code>a:hover { color: red; }</code></td></tr><tr><td style="text-align:left;">伪元素选择器</td><td style="text-align:left;">选择元素的特定部分</td><td style="text-align:left;"><code>p::first-line { color: blue; }</code></td></tr><tr><td style="text-align:left;">子元素选择器</td><td style="text-align:left;">选择特定父元素的直接子元素</td><td style="text-align:left;"><code>div > p { margin-left: 20px; }</code></td></tr><tr><td style="text-align:left;">后代选择器</td><td style="text-align:left;">选择特定元素的所有后代元素</td><td style="text-align:left;"><code>div p { margin-left: 20px; }</code></td></tr><tr><td style="text-align:left;">相邻兄弟选择器</td><td style="text-align:left;">选择紧接在另一元素后的元素,且二者有相同父元素</td><td style="text-align:left;"><code>h1 + p { margin-top: 50px; }</code></td></tr><tr><td style="text-align:left;">一般兄弟选择器</td><td style="text-align:left;">选择所有在另一元素之后的兄弟元素,且二者有相同父元素</td><td style="text-align:left;"><code>h1 ~ p { color: green; }</code></td></tr><tr><td style="text-align:left;">分组选择器</td><td style="text-align:left;">同时选择多个元素</td><td style="text-align:left;"><code>h1, h2, p { color: purple; }</code></td></tr></tbody></table></div>
|
|||
|
<h3 id="权重计算">权重计算<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#权重计算" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>CSS 选择器的权重是由选择器的各个部分组成的,又称为选择器的特异性。**特异性的计算是在冲突解决中用来决定哪个样式规则优先应用的一种方式。**计算特异性的基本规则如下:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th style="text-align:left;">选择器类型</th><th style="text-align:left;">示例</th><th style="text-align:left;">特异性</th></tr></thead><tbody><tr><td style="text-align:left;">内联样式</td><td style="text-align:left;"><code><div style="..."></code></td><td style="text-align:left;">1,0,0,0</td></tr><tr><td style="text-align:left;">ID 选择器</td><td style="text-align:left;"><code>#example</code></td><td style="text-align:left;">0,1,0,0</td></tr><tr><td style="text-align:left;">类选择器、属性选择器、伪类</td><td style="text-align:left;"><code>.example</code>, <code>[type="radio"]</code>, <code>:hover</code></td><td style="text-align:left;">0,0,1,0</td></tr><tr><td style="text-align:left;">元素选择器、伪元素</td><td style="text-align:left;"><code>div</code>, <code>::before</code></td><td style="text-align:left;">0,0,0,1</td></tr></tbody></table></div>
|
|||
|
<p>**特异性的计算方式是逐级相加。**例如,对于选择器 <code>#id .class div:hover</code>,其特异性是 <code>0100 (id选择器) + 0010 (class选择器) + 0001 (元素选择器) + 0010 (伪类) = 0121</code>。</p>
|
|||
|
<p>需要注意的是,**<code>!important</code> 规则可以覆盖其他任何特异性计算。**只有比较两个同时使用 <code>!important</code> 的规则时,才会比较特异性。此外,当样式规则的特异性相同时,后出现的规则将覆盖先出现的规则。</p>
|
|||
|
<p>以下是一个示例:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="html" data-theme="github-light github-dark"><code data-language="html" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"><</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> id</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"id1"</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> class</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"class1"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Hello World</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"></</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"><</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">blue</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; } </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 特异性 0001 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> .class1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">green</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; } </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 特异性 0010 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> #id1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">red</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; } </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 特异性 0100 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"></</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span></code></pre></figure>
|
|||
|
<p>在这个例子中,文本 Hello World 的颜色将会是红色,因为 <code>#id1</code> 的特异性最高。</p>
|
|||
|
<h3 id="层叠基层">层叠基层<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#层叠基层" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>**CSS 的全名是 Cascading Style Sheets,其中 Cascading(层叠)指的是如何解决样式冲突的过程。**当多个样式规则应用于同一个元素时,浏览器会根据一系列规则来决定使用哪个样式。这个过程主要依赖于以下三个因素:</p>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p><strong>特异性</strong>:如前面所述,不同的选择器具有不同的特异性。具有更高特异性的规则会覆盖具有较低特异性的规则。例如,ID 选择器(特异性为 0,1,0,0)会覆盖类选择器(特异性为 0,0,1,0)。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>来源</strong>:样式规则的来源也会影响其优先级。例如,用户代理(浏览器)的默认样式具有最低优先级,作者的样式规则优先级较高,而用户自定义的样式规则优先级最高。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>顺序</strong>:在具有相同特异性的规则中,后出现的规则会覆盖先出现的规则。</p>
|
|||
|
</li>
|
|||
|
</ol>
|
|||
|
<p>另一方面,CSS 的 继承 是指某些 CSS 属性的值可以从其父元素传递给其子元素。例如,如果你为一个 <code><div></code> 元素设置了 <code>color: blue;</code>,那么 <code><div></code> 元素的所有子元素(除非它们有自己的 <code>color</code> 规则)都将默认使用蓝色字体。</p>
|
|||
|
<p>**需要注意的是,并非所有 CSS 属性都是可继承的。**一些影响布局和盒模型的属性,如 <code>width</code>, <code>height</code>, <code>padding</code>, <code>margin</code> 和 <code>border</code>,是不可继承的。而一些与文字样式和颜色相关的属性,如 <code>color</code>, <code>font</code>, <code>letter-spacing</code> 和 <code>line-height</code>,是可以继承的。如果你想强制一个属性继承其父元素的值,可以使用 <code>inherit</code> 关键字,如 <code>width: inherit;</code>。</p>
|
|||
|
<h2 id="盒子模型">盒子模型<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#盒子模型" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>**在 CSS 中,所有的 HTML 元素都可以看作是盒子。**当你在设计一个网页时,你实际上是在操作这些盒子。这种观念被称为 盒子模型(Box Model)。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="text" data-theme="github-light github-dark"><code data-language="text" data-theme="github-light github-dark" style="display:grid;"><span data-line><span>+---------------------------------------------------+</span></span>
|
|||
|
<span data-line><span>| Margin |</span></span>
|
|||
|
<span data-line><span>| +---------------------------------------------+ |</span></span>
|
|||
|
<span data-line><span>| | Border | |</span></span>
|
|||
|
<span data-line><span>| | +---------------------------------------+ | |</span></span>
|
|||
|
<span data-line><span>| | | Padding | | |</span></span>
|
|||
|
<span data-line><span>| | | +---------------------------------+ | | |</span></span>
|
|||
|
<span data-line><span>| | | | Content | | | |</span></span>
|
|||
|
<span data-line><span>| | | +---------------------------------+ | | |</span></span>
|
|||
|
<span data-line><span>| | | Padding | | |</span></span>
|
|||
|
<span data-line><span>| | +---------------------------------------+ | |</span></span>
|
|||
|
<span data-line><span>| | Border | |</span></span>
|
|||
|
<span data-line><span>| +---------------------------------------------+ |</span></span>
|
|||
|
<span data-line><span>| Margin |</span></span>
|
|||
|
<span data-line><span>+---------------------------------------------------+</span></span></code></pre></figure>
|
|||
|
<p>CSS 的盒子模型主要由四个部分组成:</p>
|
|||
|
<ol>
|
|||
|
<li><strong>Content(内容)</strong>:这是 HTML 元素的实际内容,如文字、图片等。</li>
|
|||
|
<li><strong>Padding(内边距)</strong>:包围在内容周围的空间。内边距是透明的。</li>
|
|||
|
<li><strong>Border(边框)</strong>:包围在内边距和内容周围的边框。边框是可见的,你可以定义其样式、宽度和颜色。</li>
|
|||
|
<li><strong>Margin(外边距)</strong>:包围在边框和其他元素之间的空间。外边距是透明的。</li>
|
|||
|
</ol>
|
|||
|
<p>这些部分一起决定了元素的总尺寸。**元素的 <code>width</code> 和 <code>height</code> 属性默认只包含内容的尺寸,不包括内边距、边框和外边距,这是因为 <code>box-sizing</code> 默认值为 <code>content-box</code>。**但是,你可以通过设置 <code>box-sizing: border-box;</code> 来改变这种行为,使 <code>width</code> 和 <code>height</code> 也包括内边距和边框。</p>
|
|||
|
<p>这里是一个样例代码:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> width</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">300</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> padding</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> border</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> solid</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> black</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> margin</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">20</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<p>在这个例子中,<code>div</code> 元素的实际宽度是 350px(300px 的内容宽度 + 2 <em>10px 的内边距 + 2</em> 5px 的边框宽度 = 350px),外边距额外增加了 20px 的空间,但不会增加 <code>div</code> 元素本身的宽度。</p>
|
|||
|
<h3 id="边框">边框<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#边框" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>边框主要由 <code>border</code> 属性控制,这也是一个复合属性,包括以下几个子属性:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>属性</th><th>描述</th><th>示例值</th></tr></thead><tbody><tr><td><code>border-width</code></td><td>设置边框的宽度</td><td><code>1px</code>, <code>medium</code>, <code>thick</code></td></tr><tr><td><code>border-style</code></td><td>设置边框的样式</td><td><code>solid</code>, <code>dashed</code>, <code>dotted</code>, <code>none</code></td></tr><tr><td><code>border-color</code></td><td>设置边框的颜色</td><td><code>black</code>, <code>#000000</code>, <code>rgb(0,0,0)</code></td></tr><tr><td><code>border-radius</code></td><td>设置边框的圆角半径</td><td><code>10px</code>, <code>50%</code></td></tr></tbody></table></div>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example-border</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置边框宽度为 5px */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> border-width</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置边框样式为实线 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> border-style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">solid</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置边框颜色为蓝色 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> border-color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">blue</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置边框的圆角半径为 10px,使得边框具有圆角效果 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> border-radius</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">10</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<h3 id="背景">背景<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#背景" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>背景主要由 <code>background</code> 属性控制,这是一个复合属性,包括以下几个子属性:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th style="text-align:left;">属性</th><th style="text-align:left;">描述</th><th style="text-align:left;">示例值</th></tr></thead><tbody><tr><td style="text-align:left;"><code>background-color</code></td><td style="text-align:left;">设置元素的背景颜色</td><td style="text-align:left;"><code>red</code>, <code>#FF0000</code>, <code>rgb(255,0,0)</code></td></tr><tr><td style="text-align:left;"><code>background-image</code></td><td style="text-align:left;">设置一个或多个背景图像</td><td style="text-align:left;"><code>url("image.jpg")</code></td></tr><tr><td style="text-align:left;"><code>background-repeat</code></td><td style="text-align:left;">设置背景图像是否以及如何重复</td><td style="text-align:left;"><code>no-repeat</code>, <code>repeat-x</code>, <code>repeat-y</code>, <code>repeat</code></td></tr><tr><td style="text-align:left;"><code>background-position</code></td><td style="text-align:left;">设置背景图像的起始位置</td><td style="text-align:left;"><code>center</code>, <code>top right</code>, <code>50% 50%</code></td></tr><tr><td style="text-align:left;"><code>background-size</code></td><td style="text-align:left;">设置背景图像的大小</td><td style="text-align:left;"><code>cover</code>, <code>contain</code>, <code>50% 50%</code></td></tr></tbody></table></div>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example-background</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置背景颜色为浅灰色 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> background-color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">#f8f8f8</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置背景图像,这里使用了一个在线的图片 URL */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> background-image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">url</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'https://example.com/image.jpg'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置背景图像不重复 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> background-repeat</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">no-repeat</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置背景图像的位置在元素的中央 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> background-position</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">center</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置背景图像的大小以完全覆盖元素 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> background-size</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cover</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<h2 id="文本处理">文本处理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#文本处理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>在 CSS 中,你可以使用各种属性来处理和控制文本的样式。以下是一个 CSS 文本处理示例:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example-text</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置文本颜色为红色 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">red</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置字体为 Arial */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> font-family</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Arial</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置字体大小为 14px */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> font-size</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">14</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置字体粗细为粗体 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> font-weight</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">bold</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置字体样式为斜体 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> font-style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">italic</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置文本居中对齐 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> text-align</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">center</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置下划线装饰线 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> text-decoration</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">underline</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置文本全部转换为大写 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> text-transform</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">uppercase</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置行高为 20px */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> line-height</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">20</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置字符间距为 2px */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> letter-spacing</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置单词间距为 5px */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> word-spacing</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置首行文本缩进为 20px */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> text-indent</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">20</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置不换行 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> white-space</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">nowrap</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* 设置文本阴影,水平偏移 1px,垂直偏移 1px,阴影模糊半径 2px,阴影颜色为黑色 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> text-shadow</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> black</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<p>以下是一些常用的 CSS 文本属性:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>属性</th><th>描述</th><th>示例值</th></tr></thead><tbody><tr><td><code>color</code></td><td>设置文本颜色</td><td><code>red</code>, <code>#FF0000</code>, <code>rgb(255,0,0)</code></td></tr><tr><td><code>font-family</code></td><td>设置字体</td><td><code>Arial</code>, <code>"Times New Roman"</code>, <code>sans-serif</code></td></tr><tr><td><code>font-size</code></td><td>设置字体大小</td><td><code>12px</code>, <code>1em</code>, <code>80%</code></td></tr><tr><td><code>font-weight</code></td><td>设置字体的粗细</td><td><code>normal</code>, <code>bold</code>, <code>600</code></td></tr><tr><td><code>font-style</code></td><td>设置字体样式,如斜体</td><td><code>normal</code>, <code>italic</code>, <code>oblique</code></td></tr><tr><td><code>text-align</code></td><td>设置文本对齐方式</td><td><code>left</code>, <code>right</code>, <code>center</code>, <code>justify</code></td></tr><tr><td><code>text-decoration</code></td><td>设置文本装饰线,如下划线</td><td><code>none</code>, <code>underline</code>, <code>overline</code>, <code>line-through</code></td></tr><tr><td><code>text-transform</code></td><td>设置文本转换,如大写</td><td><code>none</code>, <code>uppercase</code>, <code>lowercase</code>, <code>capitalize</code></td></tr><tr><td><code>line-height</code></td><td>设置行高</td><td><code>normal</code>, <code>1.5</code>, <code>20px</code></td></tr><tr><td><code>letter-spacing</code></td><td>设置字符间距</td><td><code>normal</code>, <code>3px</code>, <code>0.5em</code></td></tr><tr><td><code>word-spacing</code></td><td>设置单词间距</td><td><code>normal</code>, <code>10px</code>, <code>2em</code></td></tr><tr><td><code>text-indent</code></td><td>设置首行文本缩进</td><td><code>0</code>, <code>20px</code>, <code>5%</code></td></tr><tr><td><code>white-space</code></td><td>设置如何处理元素内的空白</td><td><code>normal</code>, <code>nowrap</code>, <code>pre</code>, <code>pre-wrap</code>, <code>pre-line</code></td></tr><tr><td><code>text-shadow</code></td><td>设置文本阴影</td><td><code>1px 1px 2px black</code></td></tr></tbody></table></div>
|
|||
|
<h2 id="溢出内容">溢出内容<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#溢出内容" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>在 CSS 中,<code>overflow</code> 属性用于控制当元素的内容超过其指定的宽度和高度时发生的情况。这种情况通常称为溢出。<code>overflow</code> 属性有以下几个值:</p>
|
|||
|
<ul>
|
|||
|
<li><code>visible</code>:这是默认值,表示内容会<strong>溢出元素的边界框</strong>。</li>
|
|||
|
<li><code>hidden</code>:如果内容溢出元素的边界框,则该<strong>内容会被剪裁</strong>,并且其他内容是不可见的。</li>
|
|||
|
<li><code>scroll</code>:如果内容溢出元素的边界框,<strong>浏览器会添加滚动条</strong>以便查看剩余的内容。</li>
|
|||
|
<li><code>auto</code>:如果内容溢出元素的边界框,<strong>浏览器会根据需要添加滚动条。</strong></li>
|
|||
|
</ul>
|
|||
|
<p>另外,还有 <code>overflow-x</code> 和 <code>overflow-y</code> 属性,这两个属性分别用于设置元素的水平和垂直溢出。这些属性有相同的值:<code>visible</code>、<code>hidden</code>、<code>scroll</code> 和 <code>auto</code>。</p>
|
|||
|
<p>以下是一些示例:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 如果其内容溢出,该内容将被剪裁 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> overflow</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">hidden</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 如果其内容在垂直方向上溢出,浏览器将添加滚动条 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> overflow-y</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">scroll</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 如果其内容溢出,浏览器将根据需要添加滚动条 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> overflow</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">auto</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<p>需要注意的是,<strong><code>overflow</code> 属性只有在元素的宽度和高度已经明确指定(例如,通过 CSS 或内联样式)</strong>,并且内容实际上溢出这些指定的尺寸时,才会起作用。</p>
|
|||
|
<h2 id="单位和值">单位和值<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#单位和值" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>在 CSS 中,我们经常需要为各种属性提供值,这些值可能需要单位。下面是一些常见的 CSS 单位和值:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th style="text-align:left;">类型</th><th style="text-align:left;">单位</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">相对长度</td><td style="text-align:left;">em</td><td style="text-align:left;">相对于父元素的字体大小</td></tr><tr><td style="text-align:left;">相对长度</td><td style="text-align:left;">rem</td><td style="text-align:left;">相对于根元素(通常是 <code><html></code>)的字体大小</td></tr><tr><td style="text-align:left;">相对长度</td><td style="text-align:left;">%</td><td style="text-align:left;">相对于父元素的大小</td></tr><tr><td style="text-align:left;"><strong>绝对长度</strong></td><td style="text-align:left;">px</td><td style="text-align:left;">屏幕上的一个点</td></tr><tr><td style="text-align:left;"><strong>绝对长度</strong></td><td style="text-align:left;">cm, mm, in</td><td style="text-align:left;">在屏幕上的大小可能会因设备而异,但在打印中很有用</td></tr><tr><td style="text-align:left;">其他单位</td><td style="text-align:left;">vw</td><td style="text-align:left;">相对于视口的宽度,“1vw” 等于视口宽度的 1%</td></tr><tr><td style="text-align:left;">其他单位</td><td style="text-align:left;">vh</td><td style="text-align:left;">相对于视口的高度,“1vh” 等于视口高度的 1%</td></tr><tr><td style="text-align:left;">其他单位</td><td style="text-align:left;">vmin 和 vmax</td><td style="text-align:left;">分别是视口宽度和高度中较小和较大的值的百分比</td></tr><tr><td style="text-align:left;">颜色值</td><td style="text-align:left;">Hexadecimal</td><td style="text-align:left;">十六进制,如 “#FF0000” 代表红色</td></tr><tr><td style="text-align:left;">颜色值</td><td style="text-align:left;">RGB</td><td style="text-align:left;">如 “rgb(255, 0, 0)” 代表红色</td></tr><tr><td style="text-align:left;">颜色值</td><td style="text-align:left;">RGBA</td><td style="text-align:left;">增加了一个 alpha (透明度) 值,如 “rgba(255, 0, 0, 0.5)” 代表半透明的红色</td></tr><tr><td style="text-align:left;">颜色值</td><td style="text-align:left;">HSL</td><td style="text-align:left;">使用色相(hue)、饱和度(saturation)和亮度(lightness)来指定颜色,如 “hsl(0, 100%, 50%)” 代表红色</td></tr><tr><td style="text-align:left;">颜色值</td><td style="text-align:left;">HSLA</td><td style="text-align:left;">与 HSL 类似,但增加了 alpha (透明度) 值,如 “hsla(0, 100%, 50%, 0.5)” 代表半透明的红色</td></tr><tr><td style="text-align:left;">关键字值</td><td style="text-align:left;">display</td><td style="text-align:left;">可以接受 “block”, “inline”, “inline-block”, “none” 等值</td></tr><tr><td style="text-align:left;">关键字值</td><td style="text-align:left;">position</td><td style="text-align:left;">可以接受 “static”, “relative”, “absolute”, “fixed” 等值</td></tr></tbody></table></div>
|
|||
|
<h2 id="布局技巧">布局技巧<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#布局技巧" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>在 CSS 的世界里,布局是最重要的技巧之一。**Flexbox(弹性盒子)和 Grid(网格)布局是两种现代且强大的布局方法。**Flexbox 主要用于在一维空间(行或列)内对元素进行布局,非常适合用于组件和小型布局。而 Grid 则主要用于在二维空间(行和列)内对元素进行布局,非常适合用于大型布局项目,如整个页面布局。</p>
|
|||
|
<h3 id="display">Display<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#display" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>CSS 中 <code>display</code> 属性用于控制元素的显示类型。以下是一些常见的 <code>display</code> 属性值:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th style="text-align:left;">属性值</th><th style="text-align:left;">描述</th><th style="text-align:left;">示例元素</th></tr></thead><tbody><tr><td style="text-align:left;"><code>block</code></td><td style="text-align:left;">元素作为块级元素显示,前后会有换行符。</td><td style="text-align:left;"><code><div></code>, <code><p></code>, <code><h1></code>-<code><h6></code> 等</td></tr><tr><td style="text-align:left;"><code>inline</code></td><td style="text-align:left;">元素作为内联元素显示,元素前后不会产生换行。</td><td style="text-align:left;"><code><span></code>, <code><a></code>, <code><img></code> 等</td></tr><tr><td style="text-align:left;"><code>inline-block</code></td><td style="text-align:left;">元素作为内联块级元素显示,可以和其他元素在同一行显示,但内部可以像块级元素一样设置宽度和高度。</td><td style="text-align:left;">-</td></tr><tr><td style="text-align:left;"><code>none</code></td><td style="text-align:left;">元素不会被显示。</td><td style="text-align:left;">-</td></tr><tr><td style="text-align:left;"><code>flex</code></td><td style="text-align:left;">元素作为弹性盒子模型显示,用于在一维空间上布局项目,能够管理和分配空间和对齐内容。</td><td style="text-align:left;">-</td></tr><tr><td style="text-align:left;"><code>grid</code></td><td style="text-align:left;">元素作为网格容器显示,用于在二维空间上进行布局。</td><td style="text-align:left;">-</td></tr></tbody></table></div>
|
|||
|
<p>以下是一些使用 <code>display</code> 属性的示例:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 元素设置为块级元素 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> display</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">block</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 元素设置为内联元素 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> display</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">inline</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 元素设置为内联块级元素 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> display</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">inline-block</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 元素设置为不显示 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> display</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">none</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 元素设置为弹性盒子模型 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> display</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">flex</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/* 元素设置为网格模型 */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.example</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> display</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">grid</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<h3 id="flexbox">Flexbox<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#flexbox" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>**Flexbox 是一种一维布局模型,主要用于在一个维度(行或列)上对元素进行排列。**只需要简单的设置 <code>display: flex;</code>,一个元素就可以变为弹性容器,其子元素会成为弹性项目。这种布局是响应式的,这意味着项目的大小和位置可以根据父容器的大小动态改变。</p>
|
|||
|
<p>假设我们有一个包含三个子项的容器:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="html" data-theme="github-light github-dark"><code data-language="html" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"><</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> .flex-container</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> display</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">flex</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"></</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"><</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> class</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"flex-container"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>1</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>2</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>3</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"></</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span></code></pre></figure>
|
|||
|
<p>默认情况下,这会让这三个子项并排显示,并填充整个容器宽度。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="text" data-theme="github-light github-dark"><code data-language="text" data-theme="github-light github-dark" style="display:grid;"><span data-line><span>| Flex Container |</span></span>
|
|||
|
<span data-line><span>| 1 | 2 | 3 |</span></span></code></pre></figure>
|
|||
|
<p>Flexbox 布局中的一些常用属性:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th style="text-align:left;">属性</th><th style="text-align:left;">作用</th><th>示例</th></tr></thead><tbody><tr><td style="text-align:left;"><code>display</code></td><td style="text-align:left;">定义一个弹性容器</td><td><code>.container { display: flex; }</code></td></tr><tr><td style="text-align:left;"><code>flex-direction</code></td><td style="text-align:left;">定义项目的排列方向</td><td><code>.container { flex-direction: row; }</code></td></tr><tr><td style="text-align:left;"><code>justify-content</code></td><td style="text-align:left;">定义项目在主轴上的对齐方式</td><td><code>.container { justify-content: space-between; }</code></td></tr><tr><td style="text-align:left;"><code>align-items</code></td><td style="text-align:left;">定义项目在交叉轴上的对齐方式</td><td><code>.container { align-items: center; }</code></td></tr><tr><td style="text-align:left;"><code>flex-wrap</code></td><td style="text-align:left;">定义项目是否换行</td><td><code>.container { flex-wrap: wrap; }</code></td></tr><tr><td style="text-align:left;"><code>flex</code></td><td style="text-align:left;">定义项目的增长、缩小和基本大小</td><td><code>.item { flex: 1 1 auto; }</code></td></tr></tbody></table></div>
|
|||
|
<h3 id="grid">Grid<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#grid" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>相较于 Flexbox 的一维布局,**Grid 布局则是一个二维布局模型,可以同时在行和列两个维度上对元素进行排列。**通过设置 <code>display: grid;</code>,一个元素就可以变为网格容器,其子元素会成为网格项目。Grid 布局非常适合用于构建符合网格的复杂界面。</p>
|
|||
|
<p>假设我们有一个包含九个子项的容器:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="html" data-theme="github-light github-dark"><code data-language="html" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"><</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> .grid-container</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> display</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">grid</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> grid-template-columns</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">repeat</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">fr</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> grid-template-rows</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">repeat</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">fr</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"></</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"><</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> class</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"grid-container"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>1</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>2</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>3</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>4</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>5</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>6</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>7</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>8</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> <</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">>9</</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"></</span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">></span></span></code></pre></figure>
|
|||
|
<p>这会将子项布局为一个 3x3 的网格,如下所示:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="text" data-theme="github-light github-dark"><code data-language="text" data-theme="github-light github-dark" style="display:grid;"><span data-line><span>| Grid Container |</span></span>
|
|||
|
<span data-line><span>| 1 | 2 | 3 |</span></span>
|
|||
|
<span data-line><span>| 4 | 5 | 6 |</span></span>
|
|||
|
<span data-line><span>| 7 | 8 | 9 |</span></span></code></pre></figure>
|
|||
|
<p>Grid 布局中的一些常用属性:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th>属性</th><th>作用</th><th>示例</th></tr></thead><tbody><tr><td><code>display</code></td><td>定义一个网格容器</td><td><code>.container { display: grid; }</code></td></tr><tr><td><code>grid-template-columns</code> / <code>grid-template-rows</code></td><td>定义列宽/行高</td><td><code>.container { grid-template-columns: 1fr 1fr; }</code></td></tr><tr><td><code>grid-column</code> / <code>grid-row</code></td><td>定义项目的列/行位置</td><td><code>.item { grid-column: 1 / 2; }</code></td></tr><tr><td><code>grid-gap</code></td><td>定义列间隔/行间隔</td><td><code>.container { grid-gap: 10px; }</code></td></tr><tr><td><code>justify-items</code> / <code>align-items</code></td><td>定义项目在单元格中的对齐方式</td><td><code>.container { justify-items: center; }</code></td></tr><tr><td><code>grid-auto-flow</code></td><td>定义项目的排列方式</td><td><code>.container { grid-auto-flow: dense; }</code></td></tr></tbody></table></div>
|
|||
|
<h3 id="position">Position<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#position" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>CSS 定位(Position)非常强大,它可以用于创建各种复杂的布局。**但是,请注意,过度使用 <code>absolute</code> 或 <code>fixed</code> 定位可能会导致布局难以管理,尤其是在响应式设计中。**在可能的情况下,使用 Flexbox 或 Grid 通常是更好的选择。</p>
|
|||
|
<h4 id="static">Static<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#static" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>**<code>static</code> 是元素的默认定位方式。**这种定位方式下,元素会按照正常的文档流进行布局。<code>static</code> 定位的元素不会受到 <code>top</code>、<code>bottom</code>、<code>left</code> 或 <code>right</code> 属性的影响。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> position</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">static</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<h4 id="relative">Relative<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#relative" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>**<code>relative</code> 定位的元素会相对于它在正常文档流中的位置进行定位。**例如,设置 <code>left: 20px</code> 会使元素向右移动 20px。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> position</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">relative</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> left</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">20</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<h4 id="absolute">Absolute<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#absolute" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>**<code>absolute</code> 定位的元素会相对于最近的非 <code>static</code> 定位的祖先元素(如果存在的话)进行定位。**如果没有这样的祖先元素,那么它会相对于 <code>initial containing block</code> 进行定位。<code>absolute</code> 定位的元素会从正常的文档流中移除,并不会影响到其他元素的布局。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> position</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">absolute</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> top</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">20</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> right</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<h4 id="fixed">Fixed<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#fixed" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p><strong><code>fixed</code> 定位的元素会相对于浏览器窗口进行定位,即使页面滚动,它也始终保持在同一位置。</strong><code>fixed</code> 定位的元素会从正常的文档流中移除,并不会影响到其他元素的布局。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> position</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">fixed</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> bottom</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> right</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<h4 id="sticky">Sticky<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#sticky" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
|
|||
|
<p>**<code>sticky</code> 定位是相对定位和固定定位的混合类型。**元素在滚动到某个位置前为相对定位,之后为固定定位。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> position</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">sticky</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> top</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<h2 id="过渡与动画">过渡与动画<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#过渡与动画" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>在 CSS 中,动画和过渡都是创建动态效果的重要工具,它们可以在提升用户体验和增强网页视觉效果方面发挥重要作用。</p>
|
|||
|
<h3 id="transition">Transition<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#transition" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p><strong>过渡是当元素从一个样式逐渐改变为另一个样式时产生的效果。</strong></p>
|
|||
|
<p><code>transition</code> 属性是一个复合属性,用于设置四个过渡效果的属性,包括 <code>transition-property</code>,<code>transition-duration</code>,<code>transition-timing-function</code> 和 <code>transition-delay</code>。这些属性可以单独使用,也可以组合在一起使用。</p>
|
|||
|
<p>例如,你可以创建一个当鼠标悬停时颜色逐渐改变的按钮:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">button</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> background-color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">blue</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> transition</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: background-color </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">s</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">button</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">:hover</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> background-color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">red</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th style="text-align:left;">属性</th><th style="text-align:left;">描述</th><th style="text-align:left;">示例值</th></tr></thead><tbody><tr><td style="text-align:left;"><code>transition-property</code></td><td style="text-align:left;">指定应用过渡效果的 CSS 属性。可以是任何 CSS 属性,如 <code>background-color</code>,<code>height</code>,<code>all</code> 等。</td><td style="text-align:left;"><code>background-color</code></td></tr><tr><td style="text-align:left;"><code>transition-duration</code></td><td style="text-align:left;">指定过渡效果花费的时间,以秒(s)或毫秒(ms)为单位。</td><td style="text-align:left;"><code>2s</code></td></tr><tr><td style="text-align:left;"><code>transition-timing-function</code></td><td style="text-align:left;">规定过渡效果的速度曲线。可选值有 <code>linear</code>,<code>ease</code>,<code>ease-in</code>,<code>ease-out</code>,<code>ease-in-out</code>,<code>cubic-bezier(n,n,n,n)</code>。</td><td style="text-align:left;"><code>ease-in</code></td></tr><tr><td style="text-align:left;"><code>transition-delay</code></td><td style="text-align:left;">指定过渡效果何时开始。时间以秒(s)或毫秒(ms)为单位。</td><td style="text-align:left;"><code>1s</code></td></tr></tbody></table></div>
|
|||
|
<h3 id="animation">Animation<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#animation" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>动画是元素从一系列样式中逐渐改变的过程,可以使用 <code>@keyframes</code> 规则和 <code>animation</code> 属性来创建动画。<code>@keyframes</code> 本身并没有属性,但是你可以在 **<code>@keyframes</code> 规则中定义动画的各个阶段(关键帧)**和它们的样式。</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th style="text-align:left;">关键词</th><th style="text-align:left;">描述</th><th style="text-align:left;">示例值</th></tr></thead><tbody><tr><td style="text-align:left;"><code>@keyframes</code></td><td style="text-align:left;">用于创建新的动画规则。它的值是一个动画名称和一个包含关键帧的块。</td><td style="text-align:left;"><code>@keyframes myAnimation {...}</code></td></tr><tr><td style="text-align:left;"><code>0%</code> 或 <code>from</code></td><td style="text-align:left;">表示动画的开始阶段。你可以在这里定义元素的初始样式。</td><td style="text-align:left;"><code>0% { opacity: 0; }</code></td></tr><tr><td style="text-align:left;"><code>100%</code> 或 <code>to</code></td><td style="text-align:left;">表示动画的结束阶段。你可以在这里定义元素的最终样式。</td><td style="text-align:left;"><code>100% { opacity: 1; }</code></td></tr><tr><td style="text-align:left;"><code>n%</code></td><td style="text-align:left;">表示动画的中间阶段。<code>n</code> 是一个介于 0 和 100 之间的百分数。你可以在这里定义元素在动画中间的样式。</td><td style="text-align:left;"><code>50% { opacity: 0.5; }</code></td></tr></tbody></table></div>
|
|||
|
<p>**而 <code>animation</code> 属性则用于将动画绑定到特定元素。**例如,你可以创建一个循环移动的方块:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">@keyframes</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> move</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> 0%</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">left</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; }</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> 50%</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">left</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">200</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; }</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> 100%</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">left</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; }</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> position</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">absolute</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> width</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">100</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> height</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">100</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> background</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">red</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> animation</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">move</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">s</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> infinite</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<p>在这个例子中,方块将在 2 秒内从左移动到右,然后再移动回左边,这个过程将无限循环。</p>
|
|||
|
<h2 id="响应式设计与媒体查询">响应式设计与媒体查询<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#响应式设计与媒体查询" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>**响应式设计是一种使网站能够适应各种屏幕尺寸和设备的设计和开发方法。**在响应式设计中,布局、图片、文字和其他元素都会根据浏览器窗口的大小自动调整。</p>
|
|||
|
<p>响应式设计的主要原则包括:</p>
|
|||
|
<ul>
|
|||
|
<li><strong>流式布局</strong>:元素的宽度以百分比表示,以便能够随着浏览器窗口的大小变化而变化。</li>
|
|||
|
<li><strong>灵活的图片和媒体</strong>:这些元素的大小也是以百分比表示,以保持其在页面中的比例不变。</li>
|
|||
|
<li><strong>媒体查询</strong>:根据设备的特性,如宽度、高度和方向等,应用不同的样式规则。</li>
|
|||
|
</ul>
|
|||
|
<p><strong>媒体查询是实现响应式设计的主要工具。<strong>媒体查询允许我们根据设备的特性,如宽度、高度和方向等,应用不同的样式规则。在创建响应式布局时,我们也可以使用</strong>视口单位(vw、vh、vmin、vmax)</strong>。这些单位是相对于视口(即浏览器窗口)的大小,根据视口的大小动态变化,非常适合用于创建响应式设计。</p>
|
|||
|
<p>媒体查询的基本语法如下:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">@media</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> media-type </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">and</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (media-feature) {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /* CSS rules go here */</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<ul>
|
|||
|
<li><code>media-type</code> 是可选的,表示媒体类型,如 <code>screen</code>、<code>print</code> 等。</li>
|
|||
|
<li><code>media-feature</code> 表示要查询的设备特性,如 <code>width</code>、<code>height</code> 等。</li>
|
|||
|
</ul>
|
|||
|
<p>例如,我们可以写一个媒体查询,当浏览器窗口的宽度小于 600 像素时,让主要内容的字体大小增大,以便于在小屏幕设备上阅读。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="css" data-theme="github-light github-dark"><code data-language="css" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">@media</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> screen</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> and</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">max-width</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">600</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> .main-content</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> font-size</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">18</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<p>在这个例子中,<code>.main-content { font-size: 18px; }</code> 这条样式规则只会在媒体类型为 <code>screen</code> 并且浏览器窗口的最大宽度为 600px 时应用。以下是一些常见的媒体查询的例子:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th style="text-align:left;">媒体特性</th><th style="text-align:left;">描述</th><th style="text-align:left;">示例</th></tr></thead><tbody><tr><td style="text-align:left;"><code>max-width</code></td><td style="text-align:left;">屏幕宽度的最大值</td><td style="text-align:left;"><code>@media (max-width: 600px) {...}</code></td></tr><tr><td style="text-align:left;"><code>min-width</code></td><td style="text-align:left;">屏幕宽度的最小值</td><td style="text-align:left;"><code>@media (min-width: 768px) {...}</code></td></tr><tr><td style="text-align:left;"><code>max-height</code></td><td style="text-align:left;">屏幕高度的最大值</td><td style="text-align:left;"><code>@media (max-height: 400px) {...}</code></td></tr><tr><td style="text-align:left;"><code>min-height</code></td><td style="text-align:left;">屏幕高度的最小值</td><td style="text-align:left;"><code>@media (min-height: 500px) {...}</code></td></tr><tr><td style="text-align:left;"><code>orientation</code></td><td style="text-align:left;">设备屏幕的方向(横向或纵向)</td><td style="text-align:left;"><code>@media (orientation: landscape) {...}</code></td></tr><tr><td style="text-align:left;"><code>aspect-ratio</code></td><td style="text-align:left;">视窗的宽高比</td><td style="text-align:left;"><code>@media (aspect-ratio: 16/9) {...}</code></td></tr><tr><td style="text-align:left;"><code>resolution</code></td><td style="text-align:left;">设备的像素密度</td><td style="text-align:left;"><code>@media (min-resolution: 300dpi) {...}</code></td></tr><tr><td style="text-align:left;"><code>color</code></td><td style="text-align:left;">设备的颜色位数</td><td style="text-align:left;"><code>@media (color: 8) {...}</code></td></tr><tr><td style="text-align:left;"><code>monochrome</code></td><td style="text-align:left;">黑白设备的像素位数</td><td style="text-align:left;"><code>@media (monochrome: 2) {...}</code></td></tr></tbody></table></div>
|
|||
|
<h2 id="css-预处理器">CSS 预处理器<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#css-预处理器" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>**CSS 预处理器是一种脚本语言,它扩展了 CSS 的功能,增加了变量、嵌套、混合和函数等特性,使得 CSS 更加强大和易于维护。**预处理器将扩展的 CSS 语法转换为普通的 CSS,以便浏览器可以解析。</p>
|
|||
|
<p>SASS 和 LESS 都是流行的 CSS 预处理器,它们提供了许多强大的特性,如变量、嵌套、混合和函数等。SASS 支持两种语法:旧版的缩进语法和新版的 SCSS 语法,而 LESS 的语法更接近于原生 CSS。此外,SASS 提供了更多高级的特性,比如条件语句、循环等。在选择哪种预处理器时,你可以根据你的需求和喜好来决定。</p>
|
|||
|
<h3 id="sass">SASS<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#sass" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p><a href="https://sass-lang.com/" class="external">SASS<svg aria-hidden="true" class="external-icon" viewBox="0 0 512 512"><path d="M320 0H288V64h32 82.7L201.4 265.4 178.7 288 224 333.3l22.6-22.6L448 109.3V192v32h64V192 32 0H480 320zM32 32H0V64 480v32H32 456h32V480 352 320H424v32 96H64V96h96 32V32H160 32z"></path></svg></a>(Syntactically Awesome StyleSheets)是一种 CSS 预处理器,它提供了许多有用的特性,如变量、嵌套、混合和函数等。SASS 支持两种语法:旧版的缩进语法(Indented Syntax)和新版的 SCSS。旧版的语法更简洁,但新版的 SCSS 语法更接近于原生 CSS。</p>
|
|||
|
<p>以下是一个使用 SASS 特性的例子:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="scss" data-theme="github-light github-dark"><code data-language="scss" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">$font-stack</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">Helvetica</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">sans-serif</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">$primary-color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">#333</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">body</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> font</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">100</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">%</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> $font-stack</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">$primary-color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.container</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> width</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">100</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">%</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> .sidebar</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> width</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">30</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">%</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> .content</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> width</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">70</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">%</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<h3 id="less">LESS<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#less" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p><a href="https://lesscss.org/" class="external">LESS<svg aria-hidden="true" class="external-icon" viewBox="0 0 512 512"><path d="M320 0H288V64h32 82.7L201.4 265.4 178.7 288 224 333.3l22.6-22.6L448 109.3V192v32h64V192 32 0H480 320zM32 32H0V64 480v32H32 456h32V480 352 320H424v32 96H64V96h96 32V32H160 32z"></path></svg></a> 是另一种流行的 CSS 预处理器,它的语法和 CSS 非常相似,所以学习曲线相对较平缓。LESS 提供了变量、嵌套、混合、函数和运算等特性。</p>
|
|||
|
<p>以下是一个使用 LESS 特性的例子:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="less" data-theme="github-light github-dark"><code data-language="less" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">@</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">base-color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">#f938ab</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.box-shadow</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(@</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, @</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">c</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">when</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">iscolor</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(@</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">c</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)) {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -webkit-box-shadow</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: @</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> @</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">c</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> box-shadow</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: @</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> @</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">c</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.box-shadow</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(@</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, @</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">alpha</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">50</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">%</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">when</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">isnumber</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(@</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">alpha</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)) {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> .box-shadow</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(@</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">style</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">rgba</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, @</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">alpha</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">));</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.box</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">saturate</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(@</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">base-color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">%</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> border-color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">lighten</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(@</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">base-color</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">30</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">%</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> div</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">.box-shadow</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 5</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">px</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">30</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">%</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) }</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
|
|||
|
<p>总的来说,CSS 预处理器如 SASS 和 LESS 可以让你的 CSS 更加强大和易于维护,它们是现代前端开发工具链的重要组成部分。</p></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{"drag":true,"zoom":true,"depth":1,"scale":1.1,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[],"focusOnHover":false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
|
|||
|
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
|
|||
|
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
|
|||
|
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
|
|||
|
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
|
|||
|
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
|
|||
|
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
|
|||
|
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
|
|||
|
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
|
|||
|
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
|
|||
|
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{"drag":true,"zoom":true,"depth":-1,"scale":0.9,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[],"focusOnHover":true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#css" data-for="css">CSS</a></li><li class="depth-0"><a href="#选择器" data-for="选择器">选择器</a></li><li class="depth-1"><a href="#权重计算" data-for="权重计算">权重计算</a></li><li class="depth-1"><a href="#层叠基层" data-for="层叠基层">层叠基层</a></li><li class="depth-0"><a href="#盒子模型" data-for="盒子模型">盒子模型</a></li><li class="depth-1"><a href="#边框" data-for="边框">边框</a></li><li class="depth-1"><a href="#背景" data-for="背景">背景</a></li><li class="depth-0"><a href="#文本处理" data-for="文本处理">文本处理</a></li><li class="depth-0"><a href="#溢出内容" data-for="溢出内容">溢出内容</a></li><li class="depth-0"><a href="#单位和值" data-for="单位和值">单位和值</a></li><li class="depth-0"><a href="#布局技巧" data-for="布局技巧">布局技巧</a></li><li class="depth-1"><a href="#display" data-for="display">Display</a></li><li class="depth-1"><a href="#flexbox" data-for="flexbox">Flexbox</a></li><li class="depth-1"><a href="#grid" data-for="grid">Grid</a></li><li class="depth-1"><a href="#position" data-for="position">Position</a></li><li class="depth-0"><a href="#过渡与动画" data-for="过渡与动画">过渡与动画</a></li><li class="depth-1"><a href="#transition" data-for="transition">Transition</a></li><li class="depth-1"><a href="#animation" data-for="animation">Animation</a></li><li class="depth-0"><a href="#响应式设计与媒体查询" data-for="响应式设计与媒体查询">响应式设计与媒体查询</a></li><li class="depth-0"><a href="#css-预处理器" data-for="css-预处理器">CSS 预处理器</a></li><li class="depth-1"><a href="#sass" data-for="sass">SASS</a></li><li class="depth-1"><a href="#less" data-for="less">LESS</a></li></ul></div></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{"path":"Personal","collapsed":true},{"path":"Personal/Blog","collapsed":true},{"path":"Personal/Blog/2018","collapsed":true},{"path":"Personal/Blog/2020","collapsed":true},{"path":"Personal/Blog/2021","collapsed":true},{"path":"Personal/Blog/2022","collapsed":true},{"path":"Personal/Blog/2023","collapsed":true},{"path":"Personal/Blog/2024","collapsed":true},{"path":"Personal/Book","collapsed":true},{"path":"Personal/Book/个人成长","collapsed":true},{"path":"Personal/Book/医学健康","collapsed":true},{"path":"Personal/Book/历史","collapsed":true},{"path":"Personal/Book/哲学宗教","collapsed":true},{"path":"Personal/Book/心理","collapsed":true},{"path"
|
|||
|
</script><script type="module">
|
|||
|
let mermaidImport = undefined
|
|||
|
document.addEventListener('nav', async () => {
|
|||
|
if (document.querySelector("code.mermaid")) {
|
|||
|
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
|
|||
|
const mermaid = mermaidImport.default
|
|||
|
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
|
|||
|
mermaid.initialize({
|
|||
|
startOnLoad: false,
|
|||
|
securityLevel: 'loose',
|
|||
|
theme: darkMode ? 'dark' : 'default'
|
|||
|
})
|
|||
|
|
|||
|
await mermaid.run({
|
|||
|
querySelector: '.mermaid'
|
|||
|
})
|
|||
|
}
|
|||
|
});
|
|||
|
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>
|