blob: a8c8ff7d5906b21abf3f55652b6427cc3e5902d1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
---
title: CSS 基础
slug: Learn/Getting_started_with_the_web/CSS_basics
tags:
- Web
- 初学者
- 学习
- 层叠样式表
- 样式
translation_of: Learn/Getting_started_with_the_web/CSS_basics
---
<div>{{LearnSidebar}}</div>
<p>{{PreviousMenuNext("Learn/Getting_started_with_the_web/HTML_basics", "Learn/Getting_started_with_the_web/JavaScript_basics", "Learn/Getting_started_with_the_web")}}</p>
<div class="summary">
<p>层叠样式表(<strong>C</strong>ascading <strong>S</strong>tyle <strong>S</strong>heet,简称:CSS)是为网页添加样式的代码。本节将介绍 CSS 的基础知识,并解答类似问题:怎样将文本设置为黑色或红色?怎样将内容显示在屏幕的特定位置?怎样用背景图片或颜色来装饰网页?</p>
</div>
<h2 id="CSS_究竟什么来头?">CSS 究竟什么来头?</h2>
<p>和 HTML 类似,CSS 也不是真正的编程语言,甚至不是标记语言。它是一门样式表语言,这也就是说人们可以用它来选择性地为 HTML 元素添加样式。举例来说,要选择一个 HTML 页面里<strong>所有</strong>的段落元素,然后将其中的文本改成红色,可以这样写 CSS:</p>
<pre class="brush: css notranslate">p {
color: red;
}</pre>
<p>不妨试一下:首先新建一个 <code>styles</code> 文件夹,在其中新建一个 <code>style.css</code> 文件,将这三行 CSS 保存在这个新文件中。</p>
<p>然后再将该 CSS 文件连接至 HTML 文档,否则 CSS 代码不会对 HTML 文档在浏览器里的显示效果有任何影响。(如果你没有完成前几节的实践,请复习 <a href="https://developer.mozilla.org/zh-CN/docs/Learn/Getting_started_with_the_web/Dealing_with_files">处理文件</a> 和 <a href="/zh-CN/docs/Learn/Getting_started_with_the_web/HTML_basics">HTML 基础</a>。)</p>
<ol>
<li>打开 <code>index.html</code> 文件,然后将下面一行粘贴到文档头(也就是 <code><head></code> 和 <code></head></code> 标签之间)。
<pre class="brush: html notranslate"><link href="styles/style.css" rel="stylesheet"></pre>
</li>
<li>保存 <code>index.html</code> 并用浏览器将其打开。应该看到以下页面:</li>
</ol>
<p><img alt="测试页面,文字设置为红色" src="https://mdn.mozillademos.org/files/16480/beginner-site-red.png">如果段落文字变红,那么祝贺你,你已经成功地迈出了 CSS 学习的第一步。</p>
<h3 id="“CSS_规则集”详解">“CSS 规则集”详解</h3>
<p>让我们来仔细看一看上述CSS:</p>
<p><img alt="图解CSS声明" src="https://mdn.mozillademos.org/files/16483/css-declaration.png"></p>
<p>整个结构称为 <strong>规则集</strong>(通常简称“规则”),各部分释义如下:</p>
<dl>
<dt>选择器(<strong>Selector</strong>)</dt>
<dd>HTML 元素的名称位于规则集开始。它选择了一个或多个需要添加样式的元素(在这个例子中就是 <code>p</code> 元素)。要给不同元素添加样式只需要更改选择器就行了。</dd>
<dt>声明(<strong>Declaration</strong>)</dt>
<dd>一个单独的规则,如 <code>color: red;</code> 用来指定添加样式元素的<strong>属性</strong>。</dd>
<dt>属性(<strong>Properties</strong>)</dt>
<dd>改变 HTML 元素样式的途径。(本例中 <code>color</code> 就是 {{htmlelement("p")}} 元素的属性。)CSS 中,由编写人员决定修改哪个属性以改变规则。</dd>
<dt>属性的值(Property value)</dt>
<dd>在属性的右边,冒号后面即<strong>属性的值</strong>,它从指定属性的众多外观中选择一个值(我们除了 <code>red</code> 之外还有很多属性值可以用于 <code>color</code> )。</dd>
</dl>
<p>注意其他重要的语法:</p>
<ul>
<li>每个规则集(除了选择器的部分)都应该包含在成对的大括号里(<code>{}</code>)。</li>
<li>在每个声明里要用冒号(<code>:</code>)将属性与属性值分隔开。</li>
<li>在每个规则集里要用分号(<code>;</code>)将各个声明分隔开。</li>
</ul>
<p>如果要同时修改多个属性,只需要将它们用分号隔开,就像这样:</p>
<pre class="brush: css notranslate">p {
color: red;
width: 500px;
border: 1px solid black;
}</pre>
<h3 id="多元素选择">多元素选择</h3>
<p>也可以选择多种类型的元素并为它们添加一组相同的样式。将不同的选择器用逗号分开。例如:</p>
<pre class="brush: css notranslate">p, li, h1 {
color: red;
}</pre>
<h3 id="不同类型的选择器">不同类型的选择器</h3>
<p>选择器有许多不同的类型。上面只介绍了<strong>元素选择器</strong>,用来选择 HTML 文档中给定的元素。但是选择操作可以更加具体。下面是一些常用的选择器类型:</p>
<table class="standard-table">
<thead>
<tr>
<th scope="col">选择器名称</th>
<th scope="col">选择的内容</th>
<th scope="col">示例</th>
</tr>
</thead>
<tbody>
<tr>
<td>元素选择器(也称作标签或类型选择器)</td>
<td>所有指定(该)类型的 HTML 元素</td>
<td><code>p</code><br>
选择 <code><p></code></td>
</tr>
<tr>
<td>ID 选择器</td>
<td>具有特定 ID 的元素(单一 HTML 页面中,每个 ID 只对应一个元素,一个元素只对应一个 ID)</td>
<td><code>#my-id</code><br>
选择 <code><p id="my-id"></code> 或 <code><a id="my-id"></code></td>
</tr>
<tr>
<td>类选择器</td>
<td>具有特定类的元素(单一页面中,一个类可以有多个实例)</td>
<td><code>.my-class</code><br>
选择 <code><p class="my-class"></code> 和 <code><a class="my-class"></code></td>
</tr>
<tr>
<td>属性选择器</td>
<td>拥有特定属性的元素</td>
<td><code>img[src]</code><br>
选择 <code><img src="myimage.png"></code> 而不是 <code><img></code></td>
</tr>
<tr>
<td>伪(Pseudo)类选择器</td>
<td>特定状态下的特定元素(比如鼠标指针悬停)</td>
<td><code>a:hover</code><br>
仅在鼠标指针悬停在链接上时选择 <code><a></code>。</td>
</tr>
</tbody>
</table>
<p>选择器的种类远不止于此,更多信息请参阅 <a href="/zh-CN/docs/Learn/CSS/Introduction_to_CSS/Selectors">选择器</a>。</p>
<h2 id="字体和文本">字体和文本</h2>
<div class="blockIndicator note">
<p>译注:再一次说明,中文字体文件较大,不适合直接用于 Web Font。</p>
</div>
<p>在探索了一些 CSS 基础后,我们来把更多规则和信息添加至 <code>style.css</code> 中,从而让示例更美观。首先,让字体和文本变得更漂亮。</p>
<ol>
<li>第一步,找到之前 <a href="/zh-CN/docs/Learn/Getting_started_with_the_web/What_will_your_website_look_like#字体">Google Font 输出的地址</a>。并以 {{htmlelement("link")}} 元素的形式添加进 <code>index.html</code> 文档头( {{HTMLElement("head")}} 和 <code></head></code> 之间的任意位置)。代码如下:
<pre class="brush: html notranslate"> <link href="https://fonts.font.im/css?family=Open+Sans" rel="stylesheet" type="text/css"> </pre>
以上代码为当前网页下载 Open Sans 字体,从而使自定义 CSS 中可以对 HTML 元素应用这个字体。</li>
<li>接下来,删除 <code>style.css</code> 文件中已有的规则。虽然测试是成功的了,但是红字看起来并不太舒服。</li>
<li>
<p>将下列代码添加到相应的位置,用你在 Google Fonts 找到的字体替代 <code>font-family</code> 中的占位行。( <code>font-family</code> 意味着你想要你的文本使用的字体。)这条规则首先为整个页面设定了一个全局字体和字号(因为 <code><html></code> 是整个页面的父元素,而且它所有的子元素都会继承相同的 <code>font-size</code> 和 <code>font-family</code>):</p>
<pre class="brush: css notranslate">html {
/* px 表示 “像素(pixels)”: 基础字号为 10 像素 */
font-size: 10px;
/* Google fonts 输出的 CSS */
font-family: 'Open Sans', sans-serif;
}</pre>
<div class="note">
<p><strong>注:</strong>CSS 文档中所有位于 <code>/*</code> 和 <code>*/</code> 之间的内容都是 CSS 注释,它会被浏览器在渲染代码时忽略。你可以在这里写下对你现在要做的事情有帮助的笔记。</p>
</div>
<div class="note">
<p><strong>译注:</strong><code>/*</code><code>*/</code> 不可嵌套,<code>/*这样的注释是/*不行*/的*/</code>。CSS 不接受 <code>//</code> 注释。</p>
</div>
</li>
<li>接下来为文档体内的元素({{htmlelement("h1")}}、{{htmlelement("li")}} 和 {{htmlelement("p")}})设置字号。将标题居中显示,并为正文设置行高和字间距,从而提高页面的可读性。
<pre class="brush: css notranslate">h1 {
font-size: 60px;
text-align: center;
}
p, li {
font-size: 16px;
/* line-height 后而可以跟不同的参数,如果是数字,就是当前字体大小乘上数字 */
line-height: 2;
letter-spacing: 1px;
}</pre>
</li>
</ol>
<p>可以随时调整这些 <code>px</code><strong> </strong>值来获得满意的结果,以下是大体效果:</p>
<p><img alt="测试页面,设置了文字间距和行间距等格式。" src="https://mdn.mozillademos.org/files/16481/beginner-site-text.png" style="display: block; margin: 0px auto;"></p>
<h2 id="一切皆盒子">一切皆盒子</h2>
<p>编写 CSS 时你会发现,你的工作好像是围绕着一个一个盒子展开的——设置尺寸、颜色、位置,等等。页面里大部分 HTML 元素都可以被看作若干层叠的盒子。</p>
<p><img alt="a big stack of boxes or crates sat on top of one another" src="https://mdn.mozillademos.org/files/9441/boxes.jpg" style="display: block; margin: 0px auto;"></p>
<p>并不意外,CSS 布局主要就是基于盒模型的。每个占据页面空间的块都有这样的属性:</p>
<ul>
<li><code>padding</code>:即内边距,围绕着内容(比如段落)的空间。</li>
<li><code>border</code>:即边框,紧接着内边距的线。</li>
<li><code>margin</code>:即外边距,围绕元素外部的空间。</li>
</ul>
<p><img alt="three boxes sat inside one another. From outside to in they are labelled margin, border and padding" src="https://mdn.mozillademos.org/files/9443/box-model.png" style="display: block; margin: 0px auto;"></p>
<p>这里还使用了:</p>
<ul>
<li><code>width</code> :元素的宽度</li>
<li><code>background-color</code> :元素内容和内边距底下的颜色</li>
<li><code>color</code> :元素内容(通常是文本)的颜色</li>
<li><code>text-shadow</code> :为元素内的文本设置阴影</li>
<li><code>display</code> :设置元素的显示模式(暂略)</li>
</ul>
<p>开始在页面中添加更多 CSS 吧!大胆将这些新规则都添加到页面的底部,而不要纠结改变属性值会带来什么结果。</p>
<h3 id="更改页面颜色">更改页面颜色</h3>
<pre class="brush: css notranslate">html {
background-color: #00539F;
}</pre>
<p>这条规则将整个页面的背景颜色设置为 <a href="https://developer.mozilla.org/zh-CN/docs/Learn/Getting_started_with_the_web/What_will_your_website_look_like">所计划的颜色</a>。</p>
<h3 id="文档体格式设置">文档体格式设置</h3>
<pre class="brush: css notranslate">body {
width: 600px;
margin: 0 auto;
background-color: #FF9500;
padding: 0 20px 20px 20px;
border: 5px solid black;
}</pre>
<p>现在是 {{htmlelement("body")}} 元素。以上条声明,我们来逐条查看:</p>
<ul>
<li><code>width: 600px;</code> —— 强制页面永远保持 600 像素宽。</li>
<li><code>margin: 0 auto;</code> —— 为 <code>margin</code> 或 <code>padding</code> 等属性设置两个值时,第一个值代表元素的上方<strong>和</strong>下方(在这个例子中设置为 <code>0</code>),而第二个值代表左边<strong>和</strong>右边(在这里,<code>auto</code><strong> </strong>是一个特殊的值,意思是水平方向上左右对称)。你也可以使用一个,三个或四个值,参考 <a href="/zh-CN/docs/Web/CSS/margin#取值">这里</a> 。</li>
<li><code>background-color: #FF9500;</code> —— 如前文所述,指定元素的背景颜色。我们给 body 用了一种略微偏红的橘色以与深蓝色的 {{htmlelement("html")}} <strong> </strong>元素形成反差,你也可以尝试其它颜色。</li>
<li><code>padding: 0 20px 20px 20px;</code> —— 我们给内边距设置了四个值来让内容四周产生一点空间。这一次我们不设置上方的内边距,设置右边,下方,左边的内边距为20像素。值以上、右、下、左的顺序排列。</li>
<li><code>border: 5px solid black;</code> —— 直接为 body 设置 5 像素的黑色实线边框。</li>
</ul>
<h3 id="定位页面主标题并添加样式">定位页面主标题并添加样式</h3>
<pre class="brush: css notranslate">h1 {
margin: 0;
padding: 20px 0;
color: #00539F;
text-shadow: 3px 3px 1px black;
}</pre>
<p>你可能发现页面的顶部有一个难看的间隙,那是因为浏览器会在没有任何 CSS 的情况下<strong> </strong>给 {{htmlelement("h1")}} 等元素设置一些<strong>默认样式</strong>。但这并不是个好主意,因为我们希望一个没有任何样式的网页也有基本的可读性。为了去掉那个间隙,我们通过设置 <code>margin: 0;</code> 来覆盖默认样式。</p>
<p>至此,我们已经把标题的上下内边距设置为 20 像素,并且将标题文本与 HTML<strong> </strong>的背景颜色设为一致。</p>
<p>需要注意的是,这里使用了一个 <code>text-shadow</code> 属性,它可以为元素中的文本提供阴影。四个值含义如下:</p>
<ul>
<li>第一个值设置<strong>水平偏移值 </strong>—— 即阴影右移的像素数(负值左移)。</li>
<li>第二个值设置<strong>垂直偏移值 </strong>—— 即阴影下移的像素数(负值上移)。</li>
<li>第三个值设置阴影的<strong>模糊半径 </strong>—— 值越大产生的阴影越模糊。</li>
<li>第四个值设置阴影的基色。</li>
</ul>
<p>不妨尝试不同的值看看能得出什么结果。</p>
<h3 id="图像居中">图像居中</h3>
<pre class="brush: css notranslate">img {
display: block;
margin: 0 auto;
}</pre>
<p>最后,我们把图像居中来使页面更美观。可以复用 body 的 <code>margin: 0 auto</code> ,但是需要一点点调整。 {{htmlelement("body")}} 元素是<strong>块级</strong>元素,意味着它占据了页面的空间并且能够赋予外边距和其他改变间距的值。而图片是<strong>内联</strong>元素,不具备块级元素的一些功能。所以为了使图像有外边距,我们必须使用 <code>display: block</code> 给予其块级行为。</p>
<div class="note">
<p><strong>注:</strong>以上说明假定所选图片小于页面宽度(600 pixels)。更大的图片会溢出 body 并占据页面的其他位置。要解决这个问题,可以:<br>
1)使用 <a href="https://en.wikipedia.org/wiki/Raster_graphics_editor">图片编辑器</a> 来减小图片宽度;<br>
2)用 CSS 限制图片大小,即减小 <code><img></code> 元素 {{cssxref("width")}} 属性的值(比如 <code>400 px</code>)。</p>
</div>
<div class="note">
<p><strong>注:</strong>如果你暂时不能理解 <code>display: block</code> 和块级元素与行内元素的差别也没关系;随着你对 CSS 学习的深入,你将明白这个问题。<code>display</code> 属性的更多信息请查看 <a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/display">参考页面</a> 。</p>
</div>
<h2 id="小结">小结</h2>
<p>如果你按部就班完成本文的实践,那么最终可以得到以下页面(可以 <a href="https://roy-tian.github.io/learning-area/extras/getting-started-web/beginner-html-site-styled/">查看我们的版本</a>):</p>
<p><img alt="测试页面,设置了标题、正文字体,图片居中,背景色、文字块颜色。" src="https://mdn.mozillademos.org/files/16482/beginner-site-styled.png"></p>
<p>若遇到问题,可以参考 GitHub 上的 <a href="https://github.com/roy-tian/learning-area/tree/master/extras/getting-started-web/beginner-html-site-styled">完整示例代码</a> 做对比。</p>
<p>本章介绍的 CSS 知识非常有限,更多内容请访问 <a href="/zh-CN/Learn/CSS">CSS学习页面</a>。</p>
<p>{{PreviousMenuNext("Learn/Getting_started_with_the_web/HTML_basics", "Learn/Getting_started_with_the_web/JavaScript_basics", "Learn/Getting_started_with_the_web")}}</p>
<h2 id="本章目录">本章目录</h2>
<ul>
<li id="Installing_basic_software"><a href="/zh-CN/docs/Learn/Getting_started_with_the_web/Installing_basic_software">安装基础软件</a></li>
<li id="What_will_your_website_look_like"><a href="/zh-CN/docs/Learn/Getting_started_with_the_web/What_will_your_website_look_like">设计网站的外观</a></li>
<li id="Dealing_with_files"><a href="/zh-CN/docs/Learn/Getting_started_with_the_web/Dealing_with_files">处理文件</a></li>
<li id="HTML_basics"><a href="/zh-CN/docs/Learn/Getting_started_with_the_web/HTML_basics">HTML 基础</a></li>
<li id="CSS_basics"><a href="/zh-CN/docs/Learn/Getting_started_with_the_web/CSS_basics">CSS 基础</a></li>
<li id="JavaScript_basics"><a href="/zh-CN/docs/Learn/Getting_started_with_the_web/JavaScript_basics">JavaScript 基础</a></li>
<li id="Publishing_your_website"><a href="/zh-CN/docs/Learn/Getting_started_with_the_web/Publishing_your_website">发布网站</a></li>
<li id="How_the_web_works"><a href="/zh-CN/docs/Learn/Getting_started_with_the_web/How_the_Web_works">Web 是如何运作的</a></li>
</ul>
|