css制作开心庄园
使用CSS制作开心庄园的步骤
HTML结构设计
创建一个基本的HTML结构,包含庄园的主要元素如土地、植物、建筑等。使用div元素表示不同部分,并为它们分配相应的类名。
<div class="manor">
<div class="land"></div>
<div class="plant"></div>
<div class="building"></div>
</div>
基础样式设置
为庄园容器设置基本样式,包括宽度、高度和背景颜色。使用相对单位如vw和vh确保响应式布局。
.manor {
width: 80vw;
height: 60vh;
background-color: #87CEEB;
position: relative;
overflow: hidden;
}
土地样式设计
为土地部分添加样式,使用渐变背景模拟土壤效果。通过border-radius属性创建自然边缘。
.land {
width: 100%;
height: 40%;
background: linear-gradient(to bottom, #8B4513, #A0522D);
position: absolute;
bottom: 0;
border-radius: 20% 20% 0 0;
}
植物元素设计
为植物添加样式,使用伪元素创建叶子效果。通过transform属性实现旋转和倾斜。
.plant {
width: 30px;
height: 60px;
background-color: #228B22;
position: absolute;
bottom: 40%;
left: 50%;
}
.plant::before {
content: '';
width: 20px;
height: 20px;
background-color: #32CD32;
border-radius: 50%;
position: absolute;
top: -10px;
left: 5px;
transform: rotate(45deg);
}
建筑元素设计
为建筑添加样式,使用多个div元素组合成房屋结构。通过box-shadow添加立体效果。
.building {
width: 100px;
height: 80px;
background-color: #CD853F;
position: absolute;
bottom: 40%;
right: 20%;
}
.building::after {
content: '';
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 30px solid #A52A2A;
position: absolute;
top: -30px;
}
动画效果添加
为植物添加生长动画,使用@keyframes创建动态效果。通过animation属性控制动画时间和循环方式。
@keyframes grow {
0% { transform: scale(0.5); opacity: 0; }
100% { transform: scale(1); opacity: 1; }
}
.plant {
animation: grow 2s ease-in-out;
}
响应式设计 添加媒体查询确保在不同设备上正常显示。调整元素大小和位置以适应不同屏幕尺寸。

@media (max-width: 768px) {
.manor {
width: 95vw;
height: 50vh;
}
.building {
width: 80px;
height: 60px;
right: 10%;
}
}






