CSS3 完全教程

第一章:CSS3 选择器

1.1 属性选择器

CSS3 提供了更强大的属性选择器。

练习 1.1:属性选择器示例

展示属性选择器的使用。

属性选择器示例
/* 选择所有带有 title 属性的元素 */ [title] { color: blue; } /* 选择 title 属性值为 "example" 的元素 */ [title="example"] { color: red; } /* 选择 title 属性值包含 "example" 的元素 */ [title*="example"] { color: green; } /* 选择 title 属性值以 "example" 开头的元素 */ [title^="example"] { color: purple; } /* 选择 title 属性值以 "example" 结尾的元素 */ [title$="example"] { color: orange; }

1.2 伪类选择器

CSS3 新增的伪类选择器。

练习 1.2:伪类选择器示例

展示伪类选择器的使用。

伪类选择器示例
/* 选择第一个子元素 */ :first-child { color: red; } /* 选择最后一个子元素 */ :last-child { color: blue; } /* 选择第 n 个子元素 */ :nth-child(2n) { color: green; } /* 选择第 n 个特定类型的子元素 */ :nth-of-type(2n) { color: purple; } /* 选择没有子元素的元素 */ :empty { display: none; } /* 选择被选中的元素 */ :checked { background-color: yellow; }

第二章:CSS3 布局

2.1 Flexbox 布局

使用 Flexbox 实现灵活的布局。

练习 2.1:Flexbox 示例

展示 Flexbox 的使用。

Flexbox 示例
.container { display: flex; flex-direction: row; /* 或 column */ justify-content: center; /* 主轴对齐 */ align-items: center; /* 交叉轴对齐 */ flex-wrap: wrap; /* 换行 */ } .item { flex: 1; /* 弹性增长 */ min-width: 200px; margin: 10px; } /* 响应式布局 */ @media (max-width: 768px) { .container { flex-direction: column; } }

2.2 Grid 布局

使用 Grid 实现网格布局。

练习 2.2:Grid 示例

展示 Grid 的使用。

Grid 示例
.container { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: auto; gap: 20px; grid-template-areas: "header header header" "main main sidebar" "footer footer footer"; } .header { grid-area: header; } .main { grid-area: main; } .sidebar { grid-area: sidebar; } .footer { grid-area: footer; } /* 响应式布局 */ @media (max-width: 768px) { .container { grid-template-columns: 1fr; grid-template-areas: "header" "main" "sidebar" "footer"; } }

第三章:CSS3 动画和过渡

3.1 过渡效果

使用 transition 实现平滑过渡。

练习 3.1:过渡示例

展示过渡效果的使用。

过渡示例
.button { background-color: blue; color: white; padding: 10px 20px; border-radius: 5px; transition: all 0.3s ease; } .button:hover { background-color: darkblue; transform: scale(1.1); box-shadow: 0 5px 15px rgba(0,0,0,0.3); } /* 多属性过渡 */ .box { width: 100px; height: 100px; background-color: red; transition: width 0.3s ease, height 0.3s ease, background-color 0.3s ease; } .box:hover { width: 200px; height: 200px; background-color: blue; }

3.2 动画效果

使用 @keyframes 创建动画。

练习 3.2:动画示例

展示动画效果的使用。

动画示例
@keyframes slideIn { from { transform: translateX(-100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } } @keyframes pulse { 0% { transform: scale(1); } 50% { transform: scale(1.1); } 100% { transform: scale(1); } } .element { animation: slideIn 1s ease forwards; } .button { animation: pulse 2s infinite; } /* 动画属性控制 */ .animated { animation-name: slideIn; animation-duration: 1s; animation-timing-function: ease; animation-delay: 0.5s; animation-iteration-count: infinite; animation-direction: alternate; animation-fill-mode: forwards; }

第四章:CSS3 变换和特效

4.1 2D 变换

使用 transform 实现 2D 变换。

练习 4.1:2D 变换示例

展示 2D 变换的使用。

2D 变换示例
/* 平移 */ .translate { transform: translate(50px, 100px); } /* 旋转 */ .rotate { transform: rotate(45deg); } /* 缩放 */ .scale { transform: scale(1.5); } /* 倾斜 */ .skew { transform: skew(30deg, 20deg); } /* 组合变换 */ .combined { transform: translate(50px, 100px) rotate(45deg) scale(1.5); } /* 变换原点 */ .origin { transform-origin: 50% 50%; }

4.2 3D 变换

使用 transform 实现 3D 变换。

练习 4.2:3D 变换示例

展示 3D 变换的使用。

3D 变换示例
/* 3D 容器 */ .container { perspective: 1000px; transform-style: preserve-3d; } /* 3D 旋转 */ .rotate3d { transform: rotateX(45deg) rotateY(45deg) rotateZ(45deg); } /* 3D 平移 */ .translate3d { transform: translate3d(50px, 100px, 50px); } /* 3D 缩放 */ .scale3d { transform: scale3d(1.5, 1.5, 1.5); } /* 背面可见性 */ .backface { backface-visibility: hidden; }

第五章:响应式设计

5.1 媒体查询

使用媒体查询实现响应式设计。

练习 5.1:媒体查询示例

展示媒体查询的使用。

媒体查询示例
/* 移动设备 */ @media (max-width: 576px) { .container { padding: 10px; } .menu { display: none; } .mobile-menu { display: block; } } /* 平板设备 */ @media (min-width: 577px) and (max-width: 992px) { .container { padding: 20px; } .grid { grid-template-columns: repeat(2, 1fr); } } /* 桌面设备 */ @media (min-width: 993px) { .container { padding: 30px; } .grid { grid-template-columns: repeat(4, 1fr); } } /* 高分辨率屏幕 */ @media (min-resolution: 2dppx) { .image { background-image: url('image@2x.png'); } } /* 深色模式 */ @media (prefers-color-scheme: dark) { body { background-color: #333; color: #fff; } }

5.2 响应式单位

使用响应式单位实现弹性布局。

练习 5.2:响应式单位示例

展示响应式单位的使用。

响应式单位示例
/* 视口单位 */ .container { width: 100vw; height: 100vh; padding: 5vmin; } /* 相对单位 */ .text { font-size: clamp(1rem, 2vw, 2rem); line-height: 1.5; margin: 1em; } /* 弹性布局 */ .flex-container { display: flex; gap: 1rem; } .flex-item { flex: 1; min-width: 0; } /* 网格布局 */ .grid-container { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1rem; }

第六章:CSS3 新特性

6.1 变量

使用 CSS 变量实现主题定制。

练习 6.1:CSS 变量示例

展示 CSS 变量的使用。

CSS 变量示例
:root { --primary-color: #007bff; --secondary-color: #6c757d; --font-size: 16px; --spacing: 1rem; } /* 使用变量 */ .button { background-color: var(--primary-color); font-size: var(--font-size); padding: var(--spacing); } /* 变量覆盖 */ .dark-theme { --primary-color: #0056b3; --secondary-color: #495057; } /* 变量计算 */ .container { padding: calc(var(--spacing) * 2); margin: calc(var(--spacing) / 2); }

6.2 新选择器和伪元素

使用新的选择器和伪元素。

练习 6.2:新选择器示例

展示新选择器的使用。

新选择器示例
/* :is() 选择器 */ :is(h1, h2, h3) { color: var(--primary-color); } /* :where() 选择器 */ :where(article, section) h1 { margin-bottom: 1em; } /* :has() 选择器 */ div:has(> p) { border: 1px solid #ccc; } /* ::marker 伪元素 */ ul ::marker { color: var(--primary-color); } /* ::selection 伪元素 */ ::selection { background-color: var(--primary-color); color: white; }

学习建议

建议按照章节顺序学习,每完成一个练习后再进行下一个。可以通过创建实际项目来实践所学知识。