Appearance
阴影与滤镜(提升元素立体感、质感)
22.1 盒子阴影(box-shadow)
语法
css
box-shadow: 水平偏移 垂直偏移 模糊半径 扩展半径 颜色 内阴影;参数说明
- 水平偏移:正值向右,负值向左
- 垂直偏移:正值向下,负值向上
- 模糊半径:值越大越模糊
- 扩展半径:正值扩大,负值缩小
- 颜色:阴影颜色
- 内阴影:
inset关键字
示例
css
/* 外阴影 */
.box {
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
}
/* 内阴影 */
.inner-shadow {
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.2);
}
/* 多阴影 */
.multi-shadow {
box-shadow:
0 2px 5px rgba(0, 0, 0, 0.1),
0 5px 15px rgba(0, 0, 0, 0.2);
}
/* 彩色阴影 */
.color-shadow {
box-shadow: 0 0 10px #3498db;
}22.2 文字阴影(text-shadow)
语法
css
text-shadow: 水平偏移 垂直偏移 模糊半径 颜色;示例
css
/* 基本文字阴影 */
.text-shadow {
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}
/* 多重文字阴影 */
.multi-text-shadow {
text-shadow:
1px 1px 0 #000,
2px 2px 0 #333;
}
/* 彩色文字阴影 */
.color-text-shadow {
text-shadow: 0 0 5px #3498db;
}
/* 立体感文字 */
.3d-text {
text-shadow:
1px 1px 0 #fff,
-1px -1px 0 #000;
}22.3 CSS滤镜(filter)
语法
css
filter: 滤镜函数(参数);常用滤镜函数
| 滤镜函数 | 描述 | 示例 |
|---|---|---|
| blur() | 模糊效果 | blur(5px) |
| brightness() | 亮度调整 | brightness(1.5) |
| contrast() | 对比度调整 | contrast(1.2) |
| grayscale() | 灰度效果 | grayscale(1) |
| hue-rotate() | 色相旋转 | hue-rotate(90deg) |
| invert() | 反色效果 | invert(1) |
| opacity() | 透明度调整 | opacity(0.5) |
| saturate() | 饱和度调整 | saturate(2) |
| sepia() | 复古效果 | sepia(0.8) |
示例
css
/* 模糊效果 */
.blur {
filter: blur(5px);
}
/* 灰度效果 */
.grayscale {
filter: grayscale(100%);
}
/* 多个滤镜组合 */
.combined {
filter: brightness(1.2) contrast(1.1) saturate(1.3);
}
/* 悬停效果 */
.image {
filter: grayscale(100%);
transition: filter 0.3s ease;
}
.image:hover {
filter: grayscale(0%);
}22.4 实战:制作带立体阴影的卡片、模糊背景、灰度图片hover变色
实战目标
创建带立体阴影的卡片、模糊背景和灰度图片hover变色效果。
实战步骤
- 创建HTML文件:
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS阴影与滤镜实战</title>
<style>
/* 在这里添加CSS样式 */
</style>
</head>
<body>
<div class="container">
<h1>CSS阴影与滤镜实战</h1>
<!-- 立体阴影卡片 -->
<section class="demo-section">
<h2>立体阴影卡片</h2>
<div class="card-container">
<div class="card card-1">
<h3>卡片1</h3>
<p>这是一个带有立体阴影的卡片</p>
</div>
<div class="card card-2">
<h3>卡片2</h3>
<p>这是一个带有彩色阴影的卡片</p>
</div>
<div class="card card-3">
<h3>卡片3</h3>
<p>这是一个带有内阴影的卡片</p>
</div>
</div>
</section>
<!-- 模糊背景 -->
<section class="demo-section">
<h2>模糊背景</h2>
<div class="blur-background">
<div class="content">
<h3>模糊背景效果</h3>
<p>背景图片被模糊处理,文字清晰可见</p>
</div>
</div>
</section>
<!-- 灰度图片hover变色 -->
<section class="demo-section">
<h2>灰度图片hover变色</h2>
<div class="image-container">
<div class="image-wrapper">
<img src="https://via.placeholder.com/300x200" alt="示例图片">
</div>
<div class="image-wrapper">
<img src="https://via.placeholder.com/300x200" alt="示例图片">
</div>
<div class="image-wrapper">
<img src="https://via.placeholder.com/300x200" alt="示例图片">
</div>
</div>
</section>
</div>
</body>
</html>- 添加CSS样式:
css
/* 重置样式 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "Microsoft YaHei", Arial, sans-serif;
background-color: #f5f5f5;
padding: 50px 20px;
}
.container {
max-width: 1000px;
margin: 0 auto;
}
h1 {
text-align: center;
color: #333;
margin-bottom: 40px;
}
.demo-section {
margin-bottom: 40px;
padding: 30px;
background-color: white;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.demo-section h2 {
color: #555;
margin-bottom: 20px;
padding-bottom: 10px;
border-bottom: 2px solid #ddd;
}
/* 立体阴影卡片 */
.card-container {
display: flex;
gap: 20px;
flex-wrap: wrap;
justify-content: center;
}
.card {
width: 250px;
padding: 30px;
border-radius: 10px;
text-align: center;
transition: transform 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
}
.card h3 {
color: #333;
margin-bottom: 10px;
}
.card p {
color: #666;
line-height: 1.5;
}
.card-1 {
background-color: white;
box-shadow:
0 2px 10px rgba(0, 0, 0, 0.1),
0 5px 20px rgba(0, 0, 0, 0.05);
}
.card-2 {
background-color: #f8f9fa;
box-shadow:
0 4px 15px rgba(52, 152, 219, 0.3),
0 8px 30px rgba(52, 152, 219, 0.1);
}
.card-3 {
background-color: #f0f4f8;
box-shadow: inset 0 0 15px rgba(0, 0, 0, 0.1);
}
/* 模糊背景 */
.blur-background {
position: relative;
height: 300px;
border-radius: 10px;
overflow: hidden;
}
.blur-background::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: url('https://via.placeholder.com/1000x500') center/cover;
filter: blur(5px);
z-index: 1;
}
.blur-background .content {
position: relative;
z-index: 2;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
padding: 20px;
background-color: rgba(0, 0, 0, 0.5);
color: white;
}
.blur-background h3 {
font-size: 24px;
margin-bottom: 10px;
}
.blur-background p {
font-size: 16px;
max-width: 500px;
}
/* 灰度图片hover变色 */
.image-container {
display: flex;
gap: 20px;
flex-wrap: wrap;
justify-content: center;
}
.image-wrapper {
overflow: hidden;
border-radius: 10px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.image-wrapper img {
width: 300px;
height: 200px;
object-fit: cover;
filter: grayscale(100%);
transition: all 0.3s ease;
cursor: pointer;
}
.image-wrapper:hover img {
filter: grayscale(0%);
transform: scale(1.05);
}- 运行页面:在浏览器中打开文件,观察效果
预期效果
- 立体阴影卡片:三个不同样式的卡片,带有不同的阴影效果
- 模糊背景:带有模糊背景的区域,文字清晰可见
- 灰度图片hover变色:灰度图片在鼠标悬停时恢复彩色并缩放
