CSS(层叠样式表)是网页设计的核心技术之一,本教程将带你系统学习CSS的各个方面,从基础语法到高级布局技巧,助你掌握专业的网页样式设计能力。
一、CSS基础概念
1.1 什么是CSS?
CSS(Cascading Style Sheets)全称"层叠样式表",是一种样式表语言,用于描述HTML和XML文档的呈现。CSS的主要作用是将网页的内容(HTML)与表现(样式)分离,使网页结构更加清晰,维护更加方便。
1.2 CSS发展历史
版本 | 发布时间 | 主要特点 |
---|---|---|
CSS1 | 1996年 | 基础样式功能(字体、颜色、文本属性等) |
CSS2 | 1998年 | 定位、z-index、媒体类型等 |
CSS2.1 | 2004年 | 修正CSS2中的错误,成为最稳定版本 |
CSS3 | 持续更新 | 模块化设计,新增动画、渐变、阴影等现代特性 |
二、CSS基本语法与引入方式
2.1 CSS基本语法结构
选择器 {
属性1: 值1;
属性2: 值2;
/* 注释 */
}
2.2 CSS三种引入方式
1. 行内样式(内联样式)
<div style="color: red; font-size: 16px;">内容</div>
2. 内部样式表
<head>
<style>
div {
color: red;
font-size: 16px;
}
</style>
</head>
3. 外部样式表(推荐)
<head>
<link rel="stylesheet" href="styles.css">
</head>
三、CSS选择器详解
3.1 基础选择器
1. 元素选择器
p {
color: blue;
}
2. 类选择器
.class-name {
font-weight: bold;
}
3. ID选择器
#element-id {
background-color: yellow;
}
4. 通用选择器
* {
margin: 0;
padding: 0;
}
3.2 组合选择器
1. 后代选择器
div p {
color: red;
}
2. 子元素选择器
div > p {
color: green;
}
3. 相邻兄弟选择器
h1 + p {
font-size: 1.2em;
}
4. 通用兄弟选择器
h1 ~ p {
color: purple;
}
3.3 属性选择器
/* 有title属性的元素 */
[title] {
color: red;
}
/* title属性值为"example"的元素 */
[title="example"] {
border: 1px solid blue;
}
3.4 伪类选择器
/* 未访问链接 */
a:link {
color: blue;
}
/* 已访问链接 */
a:visited {
color: purple;
}
/* 鼠标悬停 */
a:hover {
color: red;
}
/* 被点击时 */
a:active {
color: green;
}
/* 获取焦点的输入框 */
input:focus {
background-color: #eee;
}
3.5 伪元素选择器
/* 首字母样式 */
p::first-letter {
font-size: 2em;
}
/* 在元素前插入内容 */
p::before {
content: "→ ";
}
/* 在元素后插入内容 */
p::after {
content: " ←";
}
四、CSS常用属性
4.1 文本样式
p {
/* 字体 */
font-family: "Microsoft YaHei", sans-serif;
/* 大小 */
font-size: 16px;
/* 粗细 */
font-weight: bold;
/* 颜色 */
color: #333;
/* 行高 */
line-height: 1.5;
/* 对齐 */
text-align: center;
/* 装饰 */
text-decoration: underline;
/* 缩进 */
text-indent: 2em;
}
4.2 背景样式
div {
/* 背景色 */
background-color: #f5f5f5;
/* 背景图 */
background-image: url("bg.jpg");
/* 背景图重复 */
background-repeat: no-repeat;
/* 背景图位置 */
background-position: center top;
/* 背景固定/滚动 */
background-attachment: fixed;
/* 简写 */
background: #f5f5f5 url("bg.jpg") no-repeat center fixed;
}
4.3 边框样式
.box {
/* 边框宽度 */
border-width: 1px;
/* 边框样式 */
border-style: solid;
/* 边框颜色 */
border-color: #ccc;
/* 圆角 */
border-radius: 5px;
/* 单边设置 */
border-top: 2px dashed red;
/* 简写 */
border: 1px solid #ccc;
}
4.4 盒模型
.box {
/* 内容区宽度 */
width: 300px;
/* 内容区高度 */
height: 200px;
/* 内边距 */
padding: 20px;
/* 外边距 */
margin: 10px;
/* 边框 */
border: 1px solid #000;
/* 盒模型计算方式 */
box-sizing: border-box; /* 或 content-box */
}
五、CSS布局技术
5.1 浮动布局
.float-left {
float: left;
width: 200px;
}
.float-right {
float: right;
width: 200px;
}
/* 清除浮动 */
.clearfix::after {
content: "";
display: block;
clear: both;
}
5.2 定位布局
/* 相对定位 */
.relative {
position: relative;
top: 10px;
left: 20px;
}
/* 绝对定位 */
.absolute {
position: absolute;
top: 0;
right: 0;
}
/* 固定定位 */
.fixed {
position: fixed;
bottom: 20px;
right: 20px;
}
/* 粘性定位 */
.sticky {
position: sticky;
top: 0;
}
5.3 Flex弹性布局
.container {
display: flex;
justify-content: center; /* 主轴对齐 */
align-items: center; /* 交叉轴对齐 */
flex-wrap: wrap; /* 换行 */
}
.item {
flex: 1; /* 弹性比例 */
min-width: 200px;
}
5.4 Grid网格布局
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr; /* 列定义 */
grid-template-rows: 100px auto; /* 行定义 */
gap: 20px; /* 间距 */
}
.item {
grid-column: 1 / 3; /* 跨越列 */
grid-row: 1; /* 行位置 */
}
六、CSS高级特性
6.1 过渡与动画
/* 过渡效果 */
.button {
transition: all 0.3s ease;
}
.button:hover {
transform: scale(1.1);
}
/* 关键帧动画 */
@keyframes slidein {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
.slide {
animation: slidein 1s forwards;
}
6.2 媒体查询(响应式设计)
/* 小屏幕设备 */
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}
/* 打印样式 */
@media print {
.no-print {
display: none;
}
}
6.3 变量(CSS自定义属性)
:root {
--main-color: #3498db;
--padding: 15px;
}
.element {
color: var(--main-color);
padding: var(--padding);
}
七、CSS最佳实践
-
语义化命名:使用有意义的类名(如
.main-nav
而非.red-box
) -
模块化组织:将CSS分成多个文件(布局、组件、工具类等)
-
避免过度嵌套:保持选择器简洁
-
使用预处理器:如Sass/Less提高开发效率
-
性能优化:
-
减少重绘和回流
-
使用transform和opacity实现动画
-
压缩生产环境CSS文件
-
八、学习资源推荐
-
官方文档:
-
在线学习:
-
书籍推荐:
-
《CSS权威指南》
-
《CSS揭秘》
-
九、总结
CSS是网页设计的核心技术之一,从简单的文本样式到复杂的布局系统,CSS提供了丰富的功能来美化网页。通过本教程,你已经掌握了CSS的基础知识和常用技巧。记住,CSS学习的关键在于实践,建议你通过实际项目来巩固所学知识。