Responsive 镜像 Gallery
你将构建什么
HTML page 使用 gallery 的 6 placeholder 镜像 laid out 使用 CSS 网格. 网格 shows 1 列 on mobile, 2 on tablet, and 3 on desktop using media 查询.
学习目标
- Use CSS 网格到 创建 responsive 列布局
- Apply media 查询 for 不同 screen 大小
- Use gap 属性 for consistent spacing 之间镜像
- 制作镜像 responsive 使用对象-fit and 宽度: 100%
前置要求
- HTML 结构 and 镜像标签
- 基础 CSS 包含 selectors and 属性
- Familiarity 使用 CSS 网格 (网格-template-列)
步骤
- 1
设置 HTML 结构
创建 gallery.HTML 使用 heading and div.gallery 包含 6 img 元素 pointing 到 placeholder 镜像.
代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Image Gallery</title> <link rel="stylesheet" href="gallery.css"> </head> <body> <h1>My Gallery</h1> <div class="gallery"> <img src="https://placehold.co/300x200" alt="Gallery image 1"> <img src="https://placehold.co/300x200" alt="Gallery image 2"> <img src="https://placehold.co/300x200" alt="Gallery image 3"> <img src="https://placehold.co/300x200" alt="Gallery image 4"> <img src="https://placehold.co/300x200" alt="Gallery image 5"> <img src="https://placehold.co/300x200" alt="Gallery image 6"> </div> </body> </html>预期结果
page 使用 heading and 6 镜像 stacked vertically 使用默认样式.
- 2
创建基础网格布局 (mobile-首先)
创建 gallery.CSS. 开始使用 1 列 for mobile. 设置 display: 网格, 网格-template-列: 1fr, and gap 的 1rem.
代码.gallery { display: grid; grid-template-columns: 1fr; gap: 1rem; padding: 1rem; } .gallery img { width: 100%; border-radius: 0.5rem; }预期结果
镜像 display in 单个列 使用 consistent spacing and rounded 角落.
- 3
添加 tablet breakpoint (2 列)
添加 media 查询 for 最小值-宽度: 640px 到 show 2 列 on tablet screens.
代码@media (min-width: 640px) { .gallery { grid-template-columns: repeat(2, 1fr); } }预期结果
On screens 640px or wider, 镜像排列到 2 列.
- 4
添加 desktop breakpoint (3 列)
添加 another media 查询 for 最小值-宽度: 1024px 到 show 3 列 on desktop.
代码@media (min-width: 1024px) { .gallery { grid-template-columns: repeat(3, 1fr); } }预期结果
On desktop screens, 镜像排列到 3 equal 列.
提示
Resize 浏览器 window 到 see 布局更改 at 每个 breakpoint.
- 5
添加响应式样式
使用媒体查询为移动端调整布局:在小屏幕上将多列改为单列、缩小字号、调整间距。移动优先意味着小屏幕体验同样重要。
代码@media (max-width: 640px) { .container { flex-direction: column; padding: 1rem; } h1 { font-size: 1.5rem; } .card { width: 100%; margin-bottom: 1rem; } }预期结果
在 640px 以下视口,布局变为单列,字号和间距缩小,内容不溢出。
自己动手试试
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Gallery</title>
<link rel="stylesheet" href="gallery.css">
</head>
<body>
<h1>My Gallery</h1>
<div class="gallery">
<!-- TODO: add 6 images -->
</div>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Gallery</title>
<link rel="stylesheet" href="gallery.css">
</head>
<body>
<h1>My Gallery</h1>
<div class="gallery">
<img src="https://placehold.co/300x200" alt="Gallery image 1">
<img src="https://placehold.co/300x200" alt="Gallery image 2">
<img src="https://placehold.co/300x200" alt="Gallery image 3">
<img src="https://placehold.co/300x200" alt="Gallery image 4">
<img src="https://placehold.co/300x200" alt="Gallery image 5">
<img src="https://placehold.co/300x200" alt="Gallery image 6">
</div>
</body>
</html>
/* gallery.css */
.gallery {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
padding: 1rem;
}
.gallery img {
width: 100%;
border-radius: 0.5rem;
}
@media (min-width: 640px) {
.gallery { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 1024px) {
.gallery { grid-template-columns: repeat(3, 1fr); }
}你学到了什么
你构建 responsive 镜像 gallery 使用 CSS 网格 and mobile-首先 media 查询 — 相同模式 used 通过 photo galleries, product 网格, and card 布局跨越 web.
相关资源
相关课程
- CSS 网格
网格布局 used for gallery 列.
- CSS Responsive 设计
Media 查询 for breakpoints.
相关练习
- 网格 Template 列
测验 on 网格-template-列.
- Media 查询测验
练习使用 media 查询.
相关速查表
- CSS Cheatsheet
网格 and media 查询引用.
相关错误
- Broken 镜像
镜像不 加载 issues.
相关术语
- CSS
Cascading 样式 Sheets.
相关术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
- CSS
Cascading 样式 Sheets.
相关错误
用大白话解释常见错误——什么意思、为什么发生、如何修复。
- Broken 镜像
镜像不 加载 issues.