css制作相册
使用CSS Grid布局制作相册
创建一个响应式的相册布局,使用CSS Grid可以轻松实现多列排列。以下是一个基础示例:
.photo-gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
grid-gap: 15px;
padding: 20px;
}
.photo-item {
position: relative;
overflow: hidden;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.photo-item img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.3s ease;
}
.photo-item:hover img {
transform: scale(1.05);
}
HTML结构:
<div class="photo-gallery">
<div class="photo-item">
<img src="photo1.jpg" alt="照片描述">
</div>
<!-- 更多照片项 -->
</div>
添加悬停效果
为相册图片添加更丰富的悬停效果,可以在照片上叠加文字描述:

.photo-caption {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: rgba(0,0,0,0.7);
color: white;
padding: 15px;
transform: translateY(100%);
transition: transform 0.3s ease;
}
.photo-item:hover .photo-caption {
transform: translateY(0);
}
实现灯箱效果
点击图片后全屏显示的大图效果:
.lightbox {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.9);
display: none;
align-items: center;
justify-content: center;
z-index: 1000;
}
.lightbox img {
max-width: 90%;
max-height: 90%;
}
.lightbox.active {
display: flex;
}
需要配合JavaScript实现点击事件处理。

响应式调整
针对不同屏幕尺寸调整相册布局:
@media (max-width: 768px) {
.photo-gallery {
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
grid-gap: 10px;
}
}
@media (max-width: 480px) {
.photo-gallery {
grid-template-columns: 1fr;
}
}
添加动画效果
使用CSS动画让相册加载更有趣:
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.photo-item {
animation: fadeIn 0.5s ease forwards;
opacity: 0;
}
.photo-item:nth-child(1) { animation-delay: 0.1s; }
.photo-item:nth-child(2) { animation-delay: 0.2s; }
/* 继续为其他子项设置延迟 */






