css制作小猪
CSS绘制小猪的步骤
通过CSS可以绘制简单的小猪形象,主要利用border-radius、box-shadow等属性实现圆润造型。以下是一个基础示例:
<div class="pig">
<div class="ear left-ear"></div>
<div class="ear right-ear"></div>
<div class="face">
<div class="nose">
<div class="nostril left-nostril"></div>
<div class="nostril right-nostril"></div>
</div>
<div class="eye left-eye"></div>
<div class="eye right-eye"></div>
</div>
</div>
.pig {
position: relative;
width: 200px;
height: 180px;
background: pink;
border-radius: 50%;
margin: 50px auto;
}
.ear {
position: absolute;
width: 40px;
height: 60px;
background: #ffb6c1;
border-radius: 50%;
}
.left-ear {
top: -20px;
left: 30px;
transform: rotate(-30deg);
}
.right-ear {
top: -20px;
right: 30px;
transform: rotate(30deg);
}
.face {
position: absolute;
top: 50px;
left: 50%;
transform: translateX(-50%);
}
.nose {
width: 80px;
height: 50px;
background: #ff69b4;
border-radius: 50%;
position: relative;
}
.nostril {
width: 15px;
height: 15px;
background: #d94d9b;
border-radius: 50%;
position: absolute;
top: 50%;
}
.left-nostril {
left: 15px;
}
.right-nostril {
right: 15px;
}
.eye {
width: 15px;
height: 15px;
background: #333;
border-radius: 50%;
position: absolute;
top: -30px;
}
.left-eye {
left: 15px;
}
.right-eye {
right: 15px;
}
进阶优化技巧
添加动画效果使小猪更生动:
.nose {
animation: sniff 2s infinite alternate;
}
@keyframes sniff {
0% { transform: scale(1); }
100% { transform: scale(1.1); }
}
增加立体感:
.pig {
box-shadow:
inset -10px -10px 20px rgba(0,0,0,0.1),
5px 5px 10px rgba(0,0,0,0.2);
}
响应式调整
通过CSS变量控制大小:
.pig {
--size: 200px;
width: var(--size);
height: calc(var(--size) * 0.9);
}
@media (max-width: 600px) {
.pig {
--size: 150px;
}
}
完整实现会呈现粉色圆脸、三角形耳朵和椭圆鼻子的卡通小猪,可通过调整颜色、阴影和比例参数获得不同风格效果。







