CSS基础

学习样式设计,创建美观的网页界面

核心概念

什么是CSS?

CSS(层叠样式表)用于控制网页的外观和布局。它可以设置颜色、字体、间距、布局等样式。

/* 内联样式 */
<p style="color: blue;">蓝色文字</p>

/* 内部样式 */
<style>
    p {
        color: blue;
        font-size: 16px;
    }
</style>

/* 外部样式表 */
<link rel="stylesheet" href="styles.css">

CSS选择器

  • 元素选择器 - p { color: red; }
  • 类选择器 - .highlight { background: yellow; }
  • ID选择器 - #header { font-size: 24px; }
  • 后代选择器 - nav a { text-decoration: none; }
  • 伪类选择器 - a:hover { color: blue; }

完整的CSS知识体系

CSS选择器大全

基本选择器

  • * - 通用选择器(选择所有元素)
  • p - 元素选择器(选择所有p标签)
  • .class - 类选择器(选择class属性)
  • #id - ID选择器(选择id属性)

组合选择器

  • div p - 后代选择器
  • div > p - 子选择器
  • div + p - 相邻兄弟选择器
  • div ~ p - 通用兄弟选择器

属性选择器

  • [type] - 有type属性的元素
  • [type="text"] - type等于text的元素
  • [class~="btn"] - 包含btn类的元素
  • [href^="https"] - href以https开头的元素

伪类选择器

  • :hover - 鼠标悬停状态
  • :first-child - 第一个子元素
  • :nth-child(n) - 第n个子元素
  • :not(selector) - 排除选择器

CSS属性大全

文本属性

字体相关
  • font-family
  • font-size
  • font-weight
  • font-style
文本样式
  • color
  • text-align
  • text-decoration
  • line-height
文本效果
  • text-shadow
  • text-transform
  • letter-spacing
  • word-spacing

背景属性

/* 背景颜色 */
background-color: #f0f0f0;

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

/* 背景简写 */
background: #f0f0f0 url('image.jpg') no-repeat center/cover;
渐变背景示例
') repeat;"> 图案背景示例

边框和圆角

/* 边框样式 */
border: 2px solid #333;
border-radius: 10px;

/* 分别设置 */
border-width: 2px;
border-style: dashed;
border-color: #667eea;

/* 圆角 */
border-top-left-radius: 10px;
border-radius: 50%; /* 圆形 */
圆角边框

阴影效果

/* 盒阴影 */
box-shadow: 2px 2px 5px rgba(0,0,0,0.3);

/* 文字阴影 */
text-shadow: 1px 1px 2px #000;

/* 多层阴影 */
box-shadow: 
    0 2px 4px rgba(0,0,0,0.1),
    0 4px 8px rgba(0,0,0,0.1);
基础阴影
深层阴影

布局属性详解

display属性

block
块级元素
inline
行内元素
inline-block
行内块元素
none
隐藏元素

position属性

/* 相对定位 */
position: relative;
top: 10px;
left: 20px;

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

/* 固定定位 */
position: fixed;
bottom: 0;
left: 0;

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

Flexbox布局详解

容器属性

/* 主轴方向 */
flex-direction: row | column;

/* 主轴对齐 */
justify-content: 
    flex-start | center | flex-end | 
    space-between | space-around;

/* 交叉轴对齐 */
align-items: 
    flex-start | center | flex-end | stretch;

项目属性

/* 项目放大比例 */
flex-grow: 1;

/* 项目缩小比例 */
flex-shrink: 0;

/* 项目基础大小 */
flex-basis: 200px;

/* 简写形式 */
flex: 1 0 200px;

Grid布局详解

网格定义

/* 定义网格 */
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: 100px 200px;

/* 网格间距 */
gap: 20px;

/* 简写形式 */
grid-template: 100px 200px / 1fr 2fr 1fr;

网格区域

/* 命名网格线 */
grid-template-columns: 
    [start] 1fr [middle] 2fr [end];

/* 网格区域命名 */
grid-template-areas: 
    "header header"
    "sidebar main"
    "footer footer";

动画和过渡

过渡效果

/* 基础过渡 */
transition: all 0.3s ease;

/* 详细设置 */
transition: 
    background-color 0.3s ease,
    transform 0.5s ease-in-out;

关键帧动画

@keyframes bounce {
    0% { transform: translateY(0); }
    50% { transform: translateY(-20px); }
    100% { transform: translateY(0); }
}

.animation {
    animation: bounce 2s infinite;
}

响应式设计进阶

媒体查询

/* 移动优先 */
.container { width: 100%; }

@media (min-width: 768px) {
    .container { width: 750px; }
}

@media (min-width: 992px) {
    .container { width: 970px; }
}

@media (min-width: 1200px) {
    .container { width: 1170px; }
}

弹性单位

  • em - 相对于父元素字体大小
  • rem - 相对于根元素字体大小
  • % - 相对于父元素百分比
  • vw/vh - 相对于视窗宽度/高度

CSS实用技巧

居中技巧

/* Flex居中 */
display: flex;
justify-content: center;
align-items: center;

/* Grid居中 */
display: grid;
place-items: center;

/* 绝对定位居中 */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

清除浮动

/* 现代方法 */
.clearfix::after {
    content: "";
    display: table;
    clear: both;
}

/* overflow方法 */
.container {
    overflow: hidden;
}

性能优化

  • 使用CSS压缩
  • 避免@import
  • 使用transform代替position
  • 减少重绘和回流

CSS属性速查表

分类 属性 常用值 说明
布局 display block, flex, grid 元素显示方式
position relative, absolute, fixed 元素定位
float left, right, none 元素浮动
z-index 1, 10, 100 层级控制
文本 font-size 16px, 1.2em, 1rem 字体大小
color #333, red, rgb(0,0,0) 文字颜色
text-align left, center, right 文本对齐
line-height 1.5, 24px, 150% 行高
背景 background-color #fff, transparent 背景颜色
background-image url('image.jpg') 背景图片
background-size cover, contain, 100% 背景大小
background-position center, top left 背景位置

CSS最佳实践

命名规范

  • 使用有意义的类名
  • 采用BEM命名法
  • 避免使用ID选择器
  • 使用小写和连字符

代码组织

  • 按功能模块组织CSS
  • 使用CSS预处理器
  • 压缩生产环境的CSS
  • 使用PostCSS处理兼容性

CSS实战案例

案例1:响应式卡片布局

代码示例:

<div class="card-container">
    <div class="card">
        <img src="avatar.jpg" alt="头像">
        <h3>标题</h3>
        <p>描述文字</p>
    </div>
</div>
.card-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
    padding: 20px;
}

.card {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    padding: 20px;
    transition: transform 0.3s ease;
}

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

效果展示:

卡片标题

这是一个响应式卡片示例

案例2:响应式导航栏

/* 基础导航样式 */
.navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    background: #333;
    color: white;
}

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

/* 移动端适配 */
@media (max-width: 768px) {
    .nav-links {
        flex-direction: column;
        position: absolute;
        top: 100%;
        left: 0;
        width: 100%;
        background: #333;
        display: none;
    }
    
    .nav-links.active {
        display: flex;
    }
}

案例3:多样化按钮

/* 基础按钮 */
.btn {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 0.25rem;
    cursor: pointer;
    transition: all 0.3s ease;
}

.btn-primary {
    background: #3b82f6;
    color: white;
}

.btn-primary:hover {
    background: #2563eb;
    transform: translateY(-1px);
}

CSS练习题

练习题1:创建登录表单

创建一个美观的登录表单,包含用户名、密码输入框和登录按钮。

要求:
  • • 使用Flexbox居中表单
  • • 输入框有hover效果
  • • 按钮有渐变背景
  • • 响应式设计
提示代码:
.login-form {
    display: flex;
    flex-direction: column;
    max-width: 400px;
    margin: 0 auto;
}

.input-group {
    margin-bottom: 1rem;
}

练习题2:制作产品卡片

设计一个产品展示卡片,包含图片、标题、价格和购买按钮。

要求:
  • • 使用Grid布局排列卡片
  • • 卡片有悬停阴影效果
  • • 图片自适应大小
  • • 移动端单列显示
提示代码:
.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
}

练习题3:创建导航菜单

制作一个响应式导航菜单,在大屏幕上水平显示,小屏幕上垂直显示。

要求:
  • • 使用Flexbox布局
  • • 移动端使用汉堡菜单
  • • 平滑过渡动画
  • • 固定顶部导航
提示代码:
@media (max-width: 768px) {
    .nav-menu {
        flex-direction: column;
        display: none;
    }
}

练习题4:制作加载动画

创建一个CSS加载动画,可以是旋转的圆圈或其他创意效果。

要求:
  • • 使用@keyframes动画
  • • 无限循环播放
  • • 居中显示
  • • 平滑流畅
提示代码:
@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.spinner {
    animation: spin 1s linear infinite;
}

练习题5:制作时间轴

设计一个垂直时间轴,展示事件或历史记录。

要求:
  • • 使用伪元素创建连接线
  • • 左侧显示时间,右侧显示内容
  • • 响应式设计
  • • 悬停效果
提示代码:
.timeline-item::before {
    content: '';
    position: absolute;
    left: -10px;
    top: 0;
    width: 2px;
    height: 100%;
    background: #333;
}

练习题6:制作价格表

创建一个价格对比表,展示不同的套餐选项。

要求:
  • • 使用Grid布局
  • • 突出显示推荐套餐
  • • 悬停放大效果
  • • 响应式表格
提示代码:
.pricing-table {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 2rem;
}

CSS盒模型

盒模型组成

内容区域 (Content)
内边距 (Padding)
边框 (Border)
外边距 (Margin)

实际应用

.box {
    width: 300px;
    height: 200px;
    padding: 20px;
    border: 5px solid #333;
    margin: 10px;
    box-sizing: border-box; /* 总宽度保持300px */
}
这是一个盒模型示例

布局技术

Flexbox布局

.flex-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
项目1
项目2
项目3

Grid布局

.grid-container {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;
    gap: 10px;
}
1
2
3

响应式设计

/* 移动优先响应式 */
.container {
    width: 100%;
    padding: 0 15px;
    margin: 0 auto;
}

@media (min-width: 576px) {
    .container { max-width: 540px; }
}
@media (min-width: 768px) {
    .container { max-width: 720px; }
}
@media (min-width: 992px) {
    .container { max-width: 960px; }
}
@media (min-width: 1200px) {
    .container { max-width: 1140px; }
}
移动端
平板
桌面端
手机布局
响应式适配

案例:小美的在线商店

项目需求

小美想创建一个在线服装店,需要现代化的设计,包括:

  • • 响应式产品网格
  • • 悬停效果
  • • 颜色主题
  • • 移动端适配

实现代码

/* 产品卡片样式 */ .product-card { background: white; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); transition: transform 0.3s; } .product-card:hover { transform: translateY(-4px); box-shadow: 0 4px 16px rgba(0,0,0,0.2); } .product-image { width: 100%; height: 200px; object-fit: cover; border-radius: 8px 8px 0 0; } .product-price { color: #667eea; font-size: 1.2em; font-weight: bold; } /* 响应式网格 */ .product-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; padding: 20px; }

练习题(每个知识点10+)

练习1:基础样式设置

为个人简介页面添加基础样式,包括颜色、字体和间距。

/* 基础样式 */ body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; background-color: #f4f4f4; margin: 0; padding: 20px; } h1 { color: #667eea; text-align: center; margin-bottom: 30px; } .profile-card { background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); max-width: 600px; margin: 0 auto; }

练习2:Flexbox布局

使用Flexbox创建一个响应式导航栏。

/* 导航栏样式 */ .navbar { display: flex; justify-content: space-between; align-items: center; background: #667eea; padding: 1rem 2rem; color: white; } .nav-links { display: flex; list-style: none; gap: 2rem; } .nav-links a { color: white; text-decoration: none; transition: opacity 0.3s; } .nav-links a:hover { opacity: 0.8; } /* 移动端响应式 */ @media (max-width: 768px) { .nav-links { flex-direction: column; gap: 1rem; } }

练习3:Grid网格布局

使用CSS Grid创建产品展示网格。

/* 产品网格布局 */ .product-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 30px; padding: 20px; } .product-item { background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 6px rgba(0,0,0,0.1); transition: transform 0.3s; } .product-item:hover { transform: translateY(-5px); } .product-image { width: 100%; height: 200px; object-fit: cover; } .product-info { padding: 20px; }

练习4:动画效果

为按钮添加悬停动画效果。

/* 按钮动画 */ .btn-primary { background: #667eea; color: white; padding: 12px 24px; border: none; border-radius: 25px; cursor: pointer; transition: all 0.3s ease; position: relative; overflow: hidden; } .btn-primary:hover { background: #764ba2; transform: scale(1.05); box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4); } .btn-primary:active { transform: scale(0.95); } /* 加载动画 */ @keyframes fadeIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } } .fade-in { animation: fadeIn 0.5s ease-in; }

练习5:响应式设计

创建完全响应式的卡片组件。

/* 响应式卡片 */ .card { background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); padding: 20px; margin: 10px; } /* 桌面端 */ @media (min-width: 1024px) { .card { max-width: 400px; margin: 20px auto; } } /* 平板端 */ @media (min-width: 768px) and (max-width: 1023px) { .card { max-width: 350px; display: inline-block; margin: 10px; } } /* 手机端 */ @media (max-width: 767px) { .card { margin: 10px 5px; padding: 15px; } } /* 弹性图片 */ img { max-width: 100%; height: auto; }

练习6-10:高级样式

  • • 渐变背景效果
  • • 阴影和边框效果
  • • 文字排版技巧
  • • 颜色主题系统
  • • 图标字体集成

练习11-15:交互效果

  • • 滚动动画效果
  • • 表单验证样式
  • • 模态框动画
  • • 下拉菜单效果
  • • 图片画廊效果