CSS introduction

需要具备的基础知识

在继续学习之前,你需要对下面的知识有基本的了解:

  • HTML
  • XHTML

如果你希望首先学习这些项目,请在 首页 访问相关教程。

CSS 概述

  • CSS 指层叠样式表 (Cascading Style Sheets)
  • 样式定义如何显示 HTML 元素
  • 样式通常存储在样式表
  • 把样式添加到 HTML 4.0 中,是为了解决内容与表现分离的问题
  • External style sheet可以极大提高工作效率
  • 外部样式表通常存储在 CSS 文件
  • 多个样式定义可层叠为一

样式解决了一个普遍的问题

HTML 标签原本被设计为用于定义文档内容。通过使用

这样的标签,HTML 的初衷是表达“这是标题”、“这是段落”、“这是表格”之类的信息。同时文档布局由浏览器来完成,而不使用任何的格式化标签。

由于两种主要的浏览器(Netscape 和 Internet Explorer)不断地将新的 HTML 标签和属性(比如字体标签和颜色属性)添加到 HTML 规范中,创建文档内容清晰地独立于文档表现层的站点变得越来越困难。

为了解决这个问题,万维网联盟(W3C),这个非营利的标准化联盟,肩负起了 HTML 标准化的使命,并在 HTML 4.0 之外创造出样式(Style)。

所有的主流浏览器均支持层叠样式表。

样式表极大地提高了工作效率

样式表定义如何显示 HTML 元素,就像 HTML 3.2 的字体标签和颜色属性所起的作用那样。样式通常保存在外部的 .css 文件中。通过仅仅编辑一个简单的 CSS 文档,外部样式表使你有能力同时改变站点中所有页面的布局和外观。

由于允许同时控制多重页面的样式和布局,CSS 可以称得上 WEB 设计领域的一个突破。作为网站开发者,你能够为每个 HTML 元素定义样式,并将之应用于你希望的任意多的页面中。如需进行全局的更新,只需简单地改变样式,然后网站中的所有元素均会自动地更新。

Multiple styles will be stacked into one.

Style sheets allow style information to be specified in multiple ways. Styles can be specified within a single HTML element, within the head element of an HTML page, or in an external CSS file. It is even possible to reference multiple external style sheets within the same HTML document.

Cascading order

When the same HTML element is defined by more than one style, which style will be used?

Generally, all styles will be stacked into a new virtual style sheet according to the following rules, where the number 4 has the highest priority.

  1. Browser default settings
  2. External style sheet
  3. Internal style sheet (located within the <head> tag)
  4. Inline style (within HTML elements)

Therefore, inline styles (within HTML elements) have the highest priority, which means they will take precedence over the following style declarations: style declarations in the <head> tag, style declarations in external style sheets, or style declarations in the browser (default value).