前端零基础教程(二):CSS基础入门

从零开始学习CSS,掌握选择器、布局、响应式设计、动画等核心知识

前端零基础教程(二):CSS 基础入门

CSS(Cascading Style Sheets,层叠样式表)用于控制网页的外观和布局。本文将从零开始,系统学习 CSS 的基础知识,帮助你掌握网页样式的设计。

第一章:CSS 基础认知(第 1 周)

1.1 什么是 CSS

CSS 的定义

  • 样式表语言:用于描述 HTML 元素的样式
  • 分离关注点:将样式与内容分离
  • 层叠机制:多个样式规则可以叠加
  • 继承机制:子元素可以继承父元素的样式

CSS 的作用

  • 美化页面:控制颜色、字体、间距等
  • 布局设计:控制元素的位置和排列
  • 响应式设计:适配不同屏幕尺寸
  • 动画效果:创建过渡和动画效果

1.2 CSS 的引入方式

内联样式

1
<p style="color: red; font-size: 16px;">红色文字</p>

内部样式表

1
2
3
4
5
6
7
8
<head>
  <style>
    p {
      color: red;
      font-size: 16px;
    }
  </style>
</head>

外部样式表(推荐)

1
2
3
<head>
  <link rel="stylesheet" href="style.css" />
</head>
1
2
3
4
5
/* style.css */
p {
  color: red;
  font-size: 16px;
}

1.3 CSS 语法基础

基本语法结构

1
2
3
4
选择器 {
  属性名: 属性值;
  属性名: 属性值;
}

示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/* 选择所有 p 标签 */
p {
  color: blue;
  font-size: 18px;
  margin: 10px;
}

/* 选择类名为 highlight 的元素 */
.highlight {
  background-color: yellow;
}

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

第二章:CSS 选择器(第 1-2 周)

2.1 基础选择器

元素选择器

1
2
3
4
5
6
7
8
9
/* 选择所有 p 元素 */
p {
  color: red;
}

/* 选择所有 h1 元素 */
h1 {
  font-size: 24px;
}

类选择器

1
2
3
4
5
6
7
8
9
/* 选择所有 class="highlight" 的元素 */
.highlight {
  background-color: yellow;
}

/* 多个类名 */
.button.primary {
  background-color: blue;
}

ID 选择器

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

通用选择器

1
2
3
4
5
/* 选择所有元素 */
* {
  margin: 0;
  padding: 0;
}

2.2 组合选择器

后代选择器

1
2
3
4
/* 选择 article 内的所有 p 元素 */
article p {
  color: blue;
}

子元素选择器

1
2
3
4
/* 选择 ul 的直接子元素 li */
ul > li {
  list-style: none;
}

相邻兄弟选择器

1
2
3
4
/* 选择紧跟在 h2 后面的 p 元素 */
h2 + p {
  margin-top: 0;
}

通用兄弟选择器

1
2
3
4
/* 选择 h2 后面的所有 p 元素 */
h2 ~ p {
  color: gray;
}

2.3 属性选择器

 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
/* 选择有 href 属性的 a 元素 */
a[href] {
  color: blue;
}

/* 选择 href 等于指定值的元素 */
a[href="https://example.com"]
{
  color: red;
}

/* 选择 href 包含 "example" 的元素 */
a[href*="example"] {
  text-decoration: underline;
}

/* 选择 href 以 "https" 开头的元素 */
a[href^="https"] {
  color: green;
}

/* 选择 href 以 ".com" 结尾的元素 */
a[href$=".com"] {
  font-weight: bold;
}

2.4 伪类选择器

链接伪类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/* 未访问的链接 */
a:link {
  color: blue;
}

/* 已访问的链接 */
a:visited {
  color: purple;
}

/* 鼠标悬停 */
a:hover {
  color: red;
  text-decoration: underline;
}

/* 激活状态(点击时) */
a:active {
  color: orange;
}

表单伪类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/* 聚焦状态 */
input:focus {
  border: 2px solid blue;
  outline: none;
}

/* 必填字段 */
input:required {
  border-left: 3px solid red;
}

/* 有效输入 */
input:valid {
  border-color: green;
}

/* 无效输入 */
input:invalid {
  border-color: red;
}

结构伪类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* 第一个子元素 */
li:first-child {
  font-weight: bold;
}

/* 最后一个子元素 */
li:last-child {
  border-bottom: none;
}

/* 第 n 个子元素 */
li:nth-child(3) {
  color: blue;
}

/* 奇数子元素 */
li:nth-child(odd) {
  background-color: #f0f0f0;
}

/* 偶数子元素 */
li:nth-child(even) {
  background-color: #fff;
}

2.5 伪元素选择器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/* 元素前插入内容 */
p::before {
  content: "「";
}

/* 元素后插入内容 */
p::after {
  content: "」";
}

/* 首行样式 */
p::first-line {
  font-weight: bold;
}

/* 首字母样式 */
p::first-letter {
  font-size: 2em;
  color: red;
}

第三章:CSS 常用属性(第 2 周)

3.1 文本属性

字体属性

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/* 字体族 */
p {
  font-family: "Microsoft YaHei", Arial, sans-serif;
}

/* 字体大小 */
h1 {
  font-size: 32px;
}

/* 字体粗细 */
.bold {
  font-weight: bold;
}

/* 字体样式 */
.italic {
  font-style: italic;
}

文本样式

 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
/* 文本颜色 */
p {
  color: #333333;
}

/* 文本对齐 */
.text-center {
  text-align: center;
}

/* 文本装饰 */
.underline {
  text-decoration: underline;
}

/* 行高 */
p {
  line-height: 1.6;
}

/* 字间距 */
h1 {
  letter-spacing: 2px;
}

/* 词间距 */
p {
  word-spacing: 5px;
}

3.2 背景属性

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/* 背景颜色 */
.header {
  background-color: #333333;
}

/* 背景图片 */
.hero {
  background-image: url("image.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

/* 背景简写 */
.hero {
  background: #333 url("image.jpg") center/cover no-repeat;
}

3.3 边框属性

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/* 边框样式 */
.box {
  border: 1px solid #ccc;
}

/* 单独设置 */
.box {
  border-width: 2px;
  border-style: solid;
  border-color: #333;
}

/* 圆角 */
.rounded {
  border-radius: 10px;
}

/* 圆形 */
.circle {
  border-radius: 50%;
}

3.4 盒模型

盒模型概念

  • content:内容区域
  • padding:内边距
  • border:边框
  • margin:外边距
1
2
3
4
5
6
7
8
.box {
  width: 200px;
  height: 100px;
  padding: 20px;
  border: 5px solid #333;
  margin: 10px;
  box-sizing: border-box; /* 包含边框和内边距 */
}

内边距和外边距

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* 四个方向相同 */
.box {
  padding: 20px;
  margin: 10px;
}

/* 上下 左右 */
.box {
  padding: 10px 20px;
  margin: 10px 20px;
}

/* 上 左右 下 */
.box {
  padding: 10px 20px 15px;
  margin: 10px 20px 15px;
}

/* 上 右 下 左 */
.box {
  padding: 10px 20px 15px 5px;
  margin: 10px 20px 15px 5px;
}

第四章:CSS 布局(第 3 周)

4.1 传统布局

块级元素和内联元素

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/* 块级元素 */
div {
  display: block;
  width: 100%;
}

/* 内联元素 */
span {
  display: inline;
}

/* 内联块元素 */
.button {
  display: inline-block;
  width: 100px;
  height: 40px;
}

浮动布局

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
/* 左浮动 */
.left {
  float: left;
  width: 200px;
}

/* 右浮动 */
.right {
  float: right;
  width: 200px;
}

/* 清除浮动 */
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

定位

 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
/* 相对定位 */
.relative {
  position: relative;
  top: 10px;
  left: 20px;
}

/* 绝对定位 */
.absolute {
  position: absolute;
  top: 0;
  right: 0;
}

/* 固定定位 */
.fixed {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
}

/* 粘性定位 */
.sticky {
  position: sticky;
  top: 0;
}

4.2 Flexbox 布局

Flex 容器

1
2
3
4
5
6
7
8
.container {
  display: flex;
  flex-direction: row; /* row | column | row-reverse | column-reverse */
  justify-content: center; /* 主轴对齐 */
  align-items: center; /* 交叉轴对齐 */
  flex-wrap: wrap; /* 换行 */
  gap: 20px; /* 间距 */
}

Flex 项目

1
2
3
4
5
6
7
.item {
  flex: 1; /* 自动分配空间 */
  flex-grow: 1; /* 放大比例 */
  flex-shrink: 1; /* 缩小比例 */
  flex-basis: 200px; /* 基础大小 */
  align-self: center; /* 单独对齐 */
}

Flexbox 示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* 水平居中 */
.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 垂直居中 */
.vertical-center {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

/* 等分布局 */
.equal {
  display: flex;
}

.equal > * {
  flex: 1;
}

4.3 Grid 布局

Grid 容器

1
2
3
4
5
6
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* 三列等宽 */
  grid-template-rows: 100px 200px; /* 行高 */
  gap: 20px; /* 间距 */
}

Grid 项目

1
2
3
4
5
.item {
  grid-column: 1 / 3; /* 跨越列 */
  grid-row: 1 / 2; /* 跨越行 */
  grid-area: 1 / 1 / 2 / 3; /* 简写 */
}

Grid 示例

 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
/* 经典布局 */
.layout {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";
  grid-template-columns: 200px 1fr 1fr;
  grid-template-rows: 60px 1fr 60px;
}

.header {
  grid-area: header;
}

.sidebar {
  grid-area: sidebar;
}

.main {
  grid-area: main;
}

.footer {
  grid-area: footer;
}

第五章:响应式设计(第 3-4 周)

5.1 媒体查询

基本语法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/* 屏幕宽度小于 768px */
@media (max-width: 768px) {
  .container {
    flex-direction: column;
  }
}

/* 屏幕宽度大于 1024px */
@media (min-width: 1024px) {
  .container {
    max-width: 1200px;
    margin: 0 auto;
  }
}

/* 横屏 */
@media (orientation: landscape) {
  .header {
    height: 80px;
  }
}

断点设置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/* 移动设备 */
@media (max-width: 576px) {
  /* 样式 */
}

/* 平板 */
@media (min-width: 577px) and (max-width: 768px) {
  /* 样式 */
}

/* 桌面 */
@media (min-width: 769px) {
  /* 样式 */
}

5.2 响应式单位

相对单位

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/* 视口宽度 */
.width-vw {
  width: 50vw; /* 视口宽度的 50% */
}

/* 视口高度 */
.height-vh {
  height: 100vh; /* 视口高度的 100% */
}

/* 根元素字体大小 */
.font-rem {
  font-size: 1.5rem; /* 1.5 倍根元素字体大小 */
}

/* 父元素字体大小 */
.font-em {
  font-size: 1.2em; /* 1.2 倍父元素字体大小 */
}

5.3 响应式图片

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/* 响应式图片 */
img {
  max-width: 100%;
  height: auto;
}

/* 响应式背景 */
.hero {
  background-image: url("large.jpg");
}

@media (max-width: 768px) {
  .hero {
    background-image: url("small.jpg");
  }
}

第六章:CSS3 新特性(第 4 周)

6.1 过渡效果

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/* 基本过渡 */
.button {
  background-color: blue;
  transition: background-color 0.3s ease;
}

.button:hover {
  background-color: red;
}

/* 多属性过渡 */
.card {
  transition: transform 0.3s, box-shadow 0.3s;
}

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}

6.2 动画

关键帧动画

 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
/* 定义动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* 使用动画 */
.fade-in {
  animation: fadeIn 1s ease-in-out;
}

/* 复杂动画 */
@keyframes slide {
  0% {
    transform: translateX(0);
  }
  50% {
    transform: translateX(100px);
  }
  100% {
    transform: translateX(0);
  }
}

.slide {
  animation: slide 2s infinite;
}

6.3 变换

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* 平移 */
.translate {
  transform: translate(50px, 50px);
}

/* 旋转 */
.rotate {
  transform: rotate(45deg);
}

/* 缩放 */
.scale {
  transform: scale(1.2);
}

/* 倾斜 */
.skew {
  transform: skew(10deg, 5deg);
}

/* 组合变换 */
.combined {
  transform: translate(50px, 50px) rotate(45deg) scale(1.2);
}

6.4 阴影和渐变

阴影

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/* 文字阴影 */
.text-shadow {
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

/* 盒子阴影 */
.box-shadow {
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

/* 内阴影 */
.inset-shadow {
  box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.1);
}

渐变

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/* 线性渐变 */
.linear-gradient {
  background: linear-gradient(to right, #ff0000, #0000ff);
}

/* 径向渐变 */
.radial-gradient {
  background: radial-gradient(circle, #ff0000, #0000ff);
}

/* 重复渐变 */
.repeating-gradient {
  background: repeating-linear-gradient(
    45deg,
    #ff0000,
    #ff0000 10px,
    #0000ff 10px,
    #0000ff 20px
  );
}

第七章:CSS 最佳实践(第 4 周)

7.1 代码组织

文件结构

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
styles/
├── base/
│   ├── reset.css
│   └── typography.css
├── components/
│   ├── button.css
│   └── card.css
├── layout/
│   ├── header.css
│   └── footer.css
└── main.css

命名规范

 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
/* BEM 命名法 */
.block {
  /* 块 */
}

.block__element {
  /* 元素 */
}

.block--modifier {
  /* 修饰符 */
}

/* 示例 */
.card {
  /* 卡片块 */
}

.card__title {
  /* 卡片标题元素 */
}

.card--highlighted {
  /* 高亮修饰符 */
}

7.2 性能优化

优化技巧

 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
/* 避免过度嵌套 */
/* 不好的写法 */
.container .wrapper .content .text {
  color: red;
}

/* 好的写法 */
.content-text {
  color: red;
}

/* 使用简写属性 */
/* 不好的写法 */
.box {
  margin-top: 10px;
  margin-right: 20px;
  margin-bottom: 10px;
  margin-left: 20px;
}

/* 好的写法 */
.box {
  margin: 10px 20px;
}

/* 避免使用 !important */
/* 不好的写法 */
.text {
  color: red !important;
}

/* 好的写法 */
.text-primary {
  color: red;
}

7.3 浏览器兼容性

前缀使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
/* 自动添加前缀(使用 PostCSS) */
.transition {
  transition: all 0.3s;
}

/* 手动添加前缀 */
.transition {
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  -o-transition: all 0.3s;
  transition: all 0.3s;
}

第八章:实战项目(第 4-5 周)

8.1 项目一:响应式导航栏

项目要求

  • 使用 Flexbox 布局
  • 支持移动端菜单
  • 添加过渡动画
  • 响应式设计
 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
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <title>响应式导航栏</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      .navbar {
        background-color: #333;
        padding: 1rem;
      }

      .nav-container {
        display: flex;
        justify-content: space-between;
        align-items: center;
        max-width: 1200px;
        margin: 0 auto;
      }

      .logo {
        color: white;
        font-size: 1.5rem;
        font-weight: bold;
      }

      .nav-menu {
        display: flex;
        list-style: none;
        gap: 2rem;
      }

      .nav-menu a {
        color: white;
        text-decoration: none;
        transition: color 0.3s;
      }

      .nav-menu a:hover {
        color: #4caf50;
      }

      .menu-toggle {
        display: none;
        background: none;
        border: none;
        color: white;
        font-size: 1.5rem;
        cursor: pointer;
      }

      @media (max-width: 768px) {
        .menu-toggle {
          display: block;
        }

        .nav-menu {
          display: none;
          flex-direction: column;
          position: absolute;
          top: 100%;
          left: 0;
          width: 100%;
          background-color: #333;
          padding: 1rem;
        }

        .nav-menu.active {
          display: flex;
        }
      }
    </style>
  </head>
  <body>
    <nav class="navbar">
      <div class="nav-container">
        <div class="logo">Logo</div>
        <ul class="nav-menu">
          <li><a href="#home">首页</a></li>
          <li><a href="#about">关于</a></li>
          <li><a href="#services">服务</a></li>
          <li><a href="#contact">联系</a></li>
        </ul>
        <button class="menu-toggle"></button>
      </div>
    </nav>
  </body>
</html>

8.2 项目二:卡片布局

项目要求

  • 使用 Grid 布局
  • 响应式设计
  • 添加悬停效果
  • 使用 CSS 变量

第九章:学习资源与下一步

9.1 学习资源

官方文档

  • MDN CSS 文档:最权威的 CSS 文档
  • CSS-Tricks:CSS 技巧和教程
  • Can I Use:浏览器兼容性查询

在线工具

  • CodePen:在线代码编辑器
  • CSS Grid Generator:Grid 布局生成器
  • Flexbox Froggy:Flexbox 游戏学习

9.2 下一步学习

JavaScript 学习

  • 学习 JavaScript 基础语法
  • 学习 DOM 操作
  • 学习事件处理
  • 学习与 CSS 的交互

框架学习

  • 学习 Vue.js 或 React
  • 学习组件化开发
  • 学习状态管理

结语:CSS 让网页更美观

CSS 是前端开发的重要组成部分,掌握 CSS 能够:

  1. 美化页面:创建美观的用户界面
  2. 布局设计:实现各种布局需求
  3. 响应式设计:适配不同设备
  4. 提升体验:添加动画和交互效果

记住,学习 CSS 需要:

  • 多实践:通过项目练习巩固知识
  • 理解原理:理解布局和选择器的工作原理
  • 关注兼容性:了解浏览器兼容性问题
  • 持续学习:关注 CSS 新特性和最佳实践

下一步,我们将学习 JavaScript,为网页添加交互功能。继续加油!

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计