css制作画廊
CSS制作画廊的方法
使用CSS可以创建多种画廊效果,以下是几种常见的方法:
网格布局画廊
使用CSS Grid布局可以轻松创建响应式画廊:
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 16px;
padding: 16px;
}
.gallery img {
width: 100%;
height: auto;
object-fit: cover;
border-radius: 8px;
transition: transform 0.3s ease;
}
.gallery img:hover {
transform: scale(1.05);
}
Flexbox布局画廊
Flexbox也是创建画廊的有效方式:

.gallery {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 15px;
padding: 15px;
}
.gallery-item {
flex: 1 1 300px;
max-width: 350px;
}
.gallery-item img {
width: 100%;
height: 250px;
object-fit: cover;
}
灯箱效果
为画廊添加点击放大的灯箱效果:
.lightbox {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.9);
}
.lightbox-content {
display: block;
max-width: 90%;
max-height: 90%;
margin: auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.close {
position: absolute;
top: 20px;
right: 30px;
color: white;
font-size: 35px;
cursor: pointer;
}
响应式画廊

确保画廊在不同设备上都能良好显示:
@media (max-width: 768px) {
.gallery {
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.gallery-item {
flex: 1 1 150px;
}
}
动画效果
添加悬停动画增强用户体验:
.gallery-item {
overflow: hidden;
position: relative;
}
.gallery-caption {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: rgba(0,0,0,0.7);
color: white;
padding: 10px;
transform: translateY(100%);
transition: transform 0.3s ease;
}
.gallery-item:hover .gallery-caption {
transform: translateY(0);
}
这些CSS技术可以组合使用,根据需求创建各种风格的照片画廊。通过调整网格、间距、过渡效果和响应式断点,可以打造出既美观又实用的画廊展示效果。






