先上效果图
该例子来自 CSS世界 的书中项目
总结技巧如下:
- 巧用 border 和 伪元素 来 绘制层叠效果。
- 使用 transform-origin 来改变元素的轴心
- 使用 filter:drop-shadow 绘制 物体 阴影
代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>铅笔</title> <style> .pencil { display: flex; width: 50em; height: 3.5em; font-size: 12px; margin: 0 auto; padding-top: 100px; filter: drop-shadow(0.4em 0.8em 0.3em grey); } .taper { width: 4em; /* background-color: burlywood; */ } .taper::before { content: ''; position: absolute; border-style: solid; border-width: calc(3.5em / 2) 4em calc(3.5em / 2) 0; border-color: transparent; border-right-color: burlywood; } .taper::after { content: ''; position: absolute; border-style: solid; border-width: calc(3.5em / 2) 4em calc(3.5em / 2) 0; border-color: transparent; border-right-color: black; transform-origin: left; transform: scale(0.3); } .barrel { width: 40em; background-color: green; border-top: 1em solid forestgreen; border-bottom: 1em solid darkgreen; color: silver; line-height: 1.5em; text-align: center; text-transform: uppercase; letter-spacing: 0.1em; } .eraser { position: relative; width: 6em; background-color: lightpink; border-top:1em solid pink; border-bottom:1em solid lightcoral; border-radius: 0 0.5em 0.5em 0 ; } .eraser::before { content: ''; position: absolute; width: 1.5em; height: 1.5em; background-color: silver; border-top:1em solid lightgrey; border-bottom:1em solid grey; top: -1em; } </style> </head> <body> <div class="pencil"> <div class="taper"></div> <div class="barrel">一蓑烟雨任平生</div> <div class="eraser"></div> </div> </body> </html>