Z
ZHANK
CSS 入门

CSS 语法

选择器 + 声明块、属性:值 格式、CSS 规则集结构

CSS 语法

CSS 由选择器 + 声明块组成——选择器指定目标,声明块定义样式。

学完本章你将: 掌握选择器+声明块结构、属性和值的格式。


基本结构

css
选择器 {
  属性: 值;
  属性: 值;
}
css
/* 选择所有 p 元素 */
p {
  color: red;
  font-size: 16px;
}

/* 选择 class="title" 的元素 */
.title {
  text-align: center;
  font-weight: bold;
}

/* 选择 id="header" 的元素 */
#header {
  background-color: #333;
  color: white;
}

语法要点

css
/* ✅ 属性: 值; —— 分号结尾 */
p {
  color: red;        /* 正确 */
  font-size: 16px    /* 最后一条可省略分号 */
}

/* ✅ 值不带引号(通常) */
p { font-family: Arial; }      /* 正确 */
p { font-family: "Times New Roman"; }  /* 空格名需引号 */

/* ✅ 大小写不敏感 */
P { COLOR: RED; }   /* 也能用但不推荐 */

/* ✅ 注释用 /* */
/* 这是注释 */