回顾挑战
本课属于 Coddy HTML 学习旅程的“前端实践”单元。第 11 课,共 35 课。
挑战
简单你拿到一个用户个人资料页面的 HTML。你的任务是应用移动优先设计原则,让页面完全响应式。请按以下说明操作:
1. 移动优先导航
- 在较大的屏幕上显示导航列表,并隐藏菜单按钮。
2. 移动优先排版
- 创建 CSS 变量
--body-font-size: 16px,并将其应用于 body。它将作为小屏幕上的基础字号。
3. 移动优先图片
- 使用
<picture>标签为移动设备和较大屏幕提供不同尺寸的图片。 - 使用
<source>标签,并让srcset指向较大的图片,例如:https://upload.wikimedia.org/wikipedia/commons/1/1f/Woman_1.jpg。 - 添加
media="(min-width: 768px)",使其在较大的屏幕上生效。 - 确保个人资料图片能根据屏幕宽度正确缩放。
4. 移动优先表单
- 将所有输入框和提交按钮的宽度设为
100%,让它们占满宽度,方便点击。
页面在手机和桌面屏幕上都应简洁、易读且易于使用。调整间距、对齐方式和响应式尺寸,让所有内容看起来协调。
亲手试试
<!DOCTYPE html>
<html>
<head>
<title>Mobile-First Profile</title>
<style>
:root {
--primary-font: Arial, sans-serif;
--heading-font: 'Helvetica', sans-serif;
--h1-size: 2rem;
--h2-size: 1.5rem;
--primary-color: #4a90e2;
--secondary-color: #f0f4f8;
}
body {
font-family: var(--primary-font);
margin: 0;
padding: 20px;
background-color: var(--secondary-color);
}
h1 {
font-family: var(--heading-font);
font-size: var(--h1-size);
color: var(--primary-color);
margin-bottom: 5px;
}
h2 {
font-family: var(--heading-font);
font-size: var(--h2-size);
color: #333;
margin-top: 0;
}
nav {
margin-bottom: 20px;
}
.menu-button {
display: block;
padding: 10px 15px;
background-color: var(--primary-color);
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin-bottom: 10px;
}
.nav-list {
display: none;
list-style: none;
padding: 0;
margin: 0;
}
.nav-list li {
margin-bottom: 10px;
}
.nav-list li a {
text-decoration: none;
color: var(--primary-color);
}
.profile-img {
width: 100%;
max-width: 250px;
border-radius: 50%;
margin-bottom: 20px;
display: block;
}
section p {
line-height: 1.6;
margin-bottom: 20px;
}
form {
display: flex;
flex-direction: column;
gap: 10px;
}
input, textarea, button {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 1rem;
}
button {
background-color: var(--primary-color);
color: white;
cursor: pointer;
}
/* Media queries for larger screens */
@media (min-width: 768px) {
.nav-list {
gap: 20px;
}
.nav-list li {
margin-bottom: 0;
}
form {
max-width: 500px;
}
}
</style>
</head>
<body>
<nav>
<button class="menu-button">Menu</button>
<ul class="nav-list">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<header>
<h1>Jane Doe</h1>
<h2>Web Developer & Designer</h2>
</header>
<section>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/Woman_1.jpg/250px-Woman_1.jpg" alt="Profile Image" class="profile-img">
</section>
<section>
<p>Welcome to my profile! I specialize in creating mobile-first, responsive websites that look great on any device. My focus is on performance, accessibility, and clean design.</p>
</section>
<section>
<form>
<input type="text" placeholder="Your Name">
<input type="email" placeholder="Your Email">
<textarea placeholder="Your Message"></textarea>
<button type="submit">Send Message</button>
</form>
</section>
</body>
</html>
前端实践中的所有课
自己练习: Web 在线编辑器