css心形制作
使用CSS制作心形
通过CSS的伪元素和transform属性可以轻松实现心形效果。以下是两种常见方法:
方法一:利用旋转正方形

.heart {
width: 100px;
height: 100px;
background-color: red;
position: relative;
transform: rotate(45deg);
}
.heart::before,
.heart::after {
content: "";
width: 100px;
height: 100px;
background-color: red;
border-radius: 50%;
position: absolute;
}
.heart::before {
top: -50px;
left: 0;
}
.heart::after {
top: 0;
left: -50px;
}
方法二:使用border-radius和clip-path
.heart {
width: 100px;
height: 90px;
background: red;
position: relative;
}
.heart::after {
content: "";
position: absolute;
width: 100px;
height: 90px;
background: red;
border-radius: 50%;
top: -45px;
left: 0;
}
.heart::before {
content: "";
position: absolute;
width: 100px;
height: 90px;
background: red;
border-radius: 50%;
top: 0;
left: 45px;
}
添加动画效果
可以为心形添加跳动动画:

@keyframes heartbeat {
0% { transform: scale(1); }
25% { transform: scale(1.1); }
50% { transform: scale(1); }
75% { transform: scale(1.1); }
100% { transform: scale(1); }
}
.heart {
animation: heartbeat 1s infinite;
}
使用clip-path创建心形
现代CSS支持更简洁的clip-path方式:
.heart {
width: 100px;
height: 100px;
background-color: red;
clip-path: path(
"M10,30 A20,20,0,0,1,50,30 A20,20,0,0,1,90,30 Q90,60,50,90 Q10,60,10,30 Z"
);
}
响应式心形设计
通过vw单位使心形随视口大小变化:
.heart {
width: 10vw;
height: 10vw;
background-color: red;
position: relative;
transform: rotate(45deg);
}
这些方法都可以创建出漂亮的CSS心形,选择哪种取决于具体需求和使用场景。旋转正方形的方法兼容性最好,而clip-path方法代码最简洁但需要现代浏览器支持。





