CSS3 新特性全面详解
CSS3 是 CSS 规范的第三个主要版本,带来了众多强大的新特性。以下是 CSS3 核心特性的分类详解:
一、选择器增强
1. 属性选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [data-value] { color: blue; }
[type="text"] { border: 1px solid #ccc; }
[href^="https"] { color: green; }
[class*="btn-"] { padding: 8px 12px; }
[src$=".png"] { border: 1px solid #eee; }
|
2. 结构伪类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| li:first-child { font-weight: bold; }
li:last-child { border-bottom: none; }
li:nth-child(3) { color: red; }
tr:nth-child(odd) { background: #f5f5f5; }
li:nth-last-child(2) { font-style: italic; }
div:only-child { margin: 0 auto; }
|
3. UI状态伪类
1 2 3 4 5 6 7
| input:enabled { border-color: #999; } input:disabled { opacity: 0.5; } input:checked + label { font-weight: bold; }
div:empty { display: none; }
|
二、盒模型增强
1. box-sizing
1 2 3 4 5
| div { box-sizing: content-box; }
div { box-sizing: border-box; }
|
2. resize
1 2 3 4
| textarea { resize: both; overflow: auto; }
|
3. outline-offset
1 2 3 4
| button { outline: 2px dashed #3498db; outline-offset: 5px; }
|
三、背景与边框
1. 多背景图
1 2 3 4 5 6
| .hero { background: url('bg1.png') top left no-repeat, url('bg2.png') bottom right no-repeat, linear-gradient(to bottom, #fff, #000); }
|
2. background-size
1 2 3 4 5 6 7
| .banner { background-size: cover; background-size: contain; background-size: 100px 80px; }
|
3. border-radius
1 2 3 4 5 6 7 8
| .btn { border-radius: 10px; border-radius: 5px 10px 15px 20px; border-radius: 50%; }
|
4. box-shadow
1 2 3 4 5
| .card { box-shadow: 2px 2px 5px rgba(0,0,0,0.3), inset 0 0 10px #000; }
|
5. border-image
1 2 3 4
| div { border: 10px solid transparent; border-image: url('border.png') 30 round; }
|
四、文本效果
1. text-shadow
1 2 3 4 5
| h1 { text-shadow: 1px 1px 2px #000, -1px -1px 0 #fff; }
|
2. @font-face
1 2 3 4 5 6 7 8 9
| @font-face { font-family: 'MyFont'; src: url('myfont.woff2') format('woff2'), url('myfont.woff') format('woff'); }
body { font-family: 'MyFont', sans-serif; }
|
3. word-wrap & text-overflow
1 2 3 4 5 6
| .long-text { word-wrap: break-word; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
五、2D/3D变换
1 2 3 4 5 6 7 8 9
| .element { transform: rotate(15deg) scale(1.2) skew(10deg) translate(20px, 10px); transform-origin: left top; }
|
1 2 3 4 5 6 7 8 9
| .cube { transform: rotateX(45deg) rotateY(30deg) perspective(500px); transform-style: preserve-3d; backface-visibility: hidden; }
|
六、过渡与动画
1. transition
1 2 3 4 5 6 7 8 9 10
| .btn { transition: background 0.3s ease-in-out, transform 0.2s cubic-bezier(0.4, 0, 0.2, 1); }
.btn:hover { background: #3498db; transform: scale(1.05); }
|
2. animation
1 2 3 4 5 6 7 8 9 10
| @keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-20px); } }
.ball { animation: bounce 1s infinite, fadeIn 0.5s ease-out; }
|
七、Flexbox布局
1 2 3 4 5 6 7 8 9 10 11 12 13
| .container { display: flex; flex-direction: row; justify-content: center; align-items: stretch; flex-wrap: wrap; gap: 20px; }
.item { flex: 1 0 200px; align-self: flex-start; }
|
八、Grid布局
1 2 3 4 5 6 7 8 9 10 11 12 13
| .layout { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: 100px auto 60px; grid-template-areas: "header header header" "sidebar main main" "footer footer footer"; gap: 1rem; }
.header { grid-area: header; } .main { grid-area: main; }
|
九、媒体查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| .container { padding: 10px; }
@media (min-width: 768px) { .container { padding: 20px; } }
@media (min-width: 1024px) { .container { max-width: 1200px; } }
@media print { nav { display: none; } }
|
十、其他重要特性
1. 变量(Custom Properties)
1 2 3 4 5 6 7 8 9
| :root { --primary-color: #3498db; --spacing-unit: 8px; }
.element { color: var(--primary-color); margin: calc(var(--spacing-unit) * 2); }
|
2. 滤镜(filter)
1 2 3 4 5 6
| .image { filter: blur(2px) grayscale(50%) brightness(120%); }
|
3. 混合模式(blend-mode)
1 2 3 4
| .overlay { background: url('pattern.png'), #3498db; background-blend-mode: overlay; }
|
4. 裁剪与蒙版
1 2 3 4 5 6 7
| .clipped { clip-path: circle(50% at center); clip-path: polygon(0 0, 100% 0, 50% 100%); mask-image: linear-gradient(to bottom, black, transparent); }
|
十一、CSS3最佳实践
- 渐进增强:先构建基础样式,再添加CSS3增强
- 前缀处理:使用Autoprefixer自动添加厂商前缀
- 性能优化:避免过度使用耗性能的属性如box-shadow
- 回退方案:为不支持CSS3的浏览器提供替代样式
- 模块化组织:使用CSS预处理器管理代码
CSS3极大地扩展了CSS的能力边界,使开发者能够创建更丰富、更动态的网页体验,同时减少对JavaScript的依赖。随着浏览器支持度的不断提高,这些特性已成为现代Web开发的标准配置。