Skip to content

CSS

学习 CSS — language 那个 controls 如何 web pages look, 从颜色 and 字体到 布局.

你将学到

  • 什么 CSS 是 and 如何它 样式 HTML
  • 如何 selectors 目标元素到 样式
  • box model — margin, 边框, padding, 内容
  • 如何 use Flexbox for 布局

概念讲解

CSS

CSS (Cascading Style Sheets) is the language that controls the appearance of a web page. HTML provides the structure; CSS decides how that structure looks — colors, fonts, spacing, layout, animations.

Selectors

A selector picks which HTML elements to style. A declaration block lists the properties and values:

h1 {
  color: #2563eb;
  font-size: 2rem;
}

.button {
  background: blue;
  padding: 10px 20px;
}
  • h1 — element selector (all <h1> tags).
  • .button — class selector (elements with 类="button").
  • #nav — id selector (the element with id="nav").

The Box Model

Every element is a rectangular box with four layers:

  1. Content — the text or image itself.
  2. Padding — space inside the border, around the content.
  3. Border — the edge of the box.
  4. Margin — space outside the border, pushing other elements away.

Understanding the box model is essential for spacing. When elements look "squished" or misaligned, the box model is usually why.

Flexbox

Flexbox is the modern way to arrange elements in a row or column. Apply display: flex to a container and its children become flex items you can align and distribute:

.container {
  display: flex;
  justify-content: center;  /* horizontal centering */
  align-items: center;      /* vertical centering */
  gap: 16px;                /* space between items */
}

Flexbox replaced the old float-based layouts. It is the first layout tool every web developer should learn.

The Cascade

"Cascading" means multiple rules can apply to the same element. When conflicts arise, CSS resolves them by specificity (how precise the selector is) and source order (later rules win ties). This is why your styles sometimes do not apply — a more specific rule elsewhere is overriding them.

示例代码

              /* A card component styled with the box model and flexbox */
.card {
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 16px;          /* space inside */
  margin: 16px 0;         /* space outside */
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.card-title {
  color: #2563eb;
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;              /* remove default margin */
}
            

card uses padding for 内部空间 and margin for 外部 separation. 请求头 uses flexbox 到推送它的 title and 操作到 opposite 端.

动手试一试

  • SVG Optimizer

    SVGs 是样式使用 CSS. Optimize one and inspect CSS attributes 内部.

  • 镜像颜色 Picker

    选取颜色从 镜像到 use in 你的 CSS — 获取精确 hex 值.

常见错误

错误写法

Using inline 样式 (样式='...') 相反的 类.

正确做法

Keep 样式 in .CSS file 使用类 selectors. Inline 样式是 困难到 maintain, cannot 是 cached, and override 你的 stylesheet in confusing 方式.

错误写法

Confusing margin (空间之外) 使用 padding (空间内部).

正确做法

Padding 添加空间内部边框, growing 元素's clickable 区域. Margin 添加空间之外, pushing neighbors 远离. Use padding for 轻触目标, margin for separation.

相关代码片段

相关速查表

练习 CSS

7 个练习

练习 CSS

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误