
HTML与CSS基础实战
大约 9 分钟
HTML与CSS基础实战
HTML就像是房子的"骨架",CSS则是"装修设计师"——HTML搭建结构,CSS负责美化。作为测试开发工程师,掌握HTML和CSS就像学会了"看图纸"和"做装修",不仅能理解页面是如何构建的,还能自己动手搭建漂亮的测试工具界面。
一、HTML基础:网页的骨架
HTML是什么?
HTML(HyperText Markup Language)是网页的"建筑图纸",它告诉浏览器:
- 这里放一个标题
- 那里放一个按钮
- 这块区域是一个表格
基础HTML结构
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的第一个测试工具</title>
</head>
<body>
<h1>欢迎使用API测试工具</h1>
<p>这是一个简单而强大的API测试平台</p>
</body>
</html>常用HTML标签详解
1. 文本标签
<!-- 标题标签:h1最大,h6最小 -->
<h1>测试平台主标题</h1>
<h2>功能模块标题</h2>
<h3>子功能标题</h3>
<!-- 段落和文本 -->
<p>这是一个段落,用来描述测试用例的详细信息。</p>
<span>这是行内文本</span>
<strong>重要提示</strong>
<em>强调内容</em>
<!-- 列表:测试步骤的好帮手 -->
<ul>
<li>准备测试数据</li>
<li>执行测试用例</li>
<li>验证测试结果</li>
</ul>
<ol>
<li>第一步:环境准备</li>
<li>第二步:用例执行</li>
<li>第三步:结果分析</li>
</ol>2. 表单标签:测试工具的核心
<form action="/api/test" method="POST">
<!-- 文本输入框 -->
<label for="api-url">API地址:</label>
<input type="text" id="api-url" name="url" placeholder="请输入API地址" required>
<!-- 选择框 -->
<label for="method">请求方法:</label>
<select id="method" name="method">
<option value="GET">GET</option>
<option value="POST">POST</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
</select>
<!-- 多行文本 -->
<label for="request-body">请求体:</label>
<textarea id="request-body" name="body" rows="5" cols="50"></textarea>
<!-- 复选框 -->
<input type="checkbox" id="save-history" name="save">
<label for="save-history">保存到历史记录</label>
<!-- 提交按钮 -->
<button type="submit">发送请求</button>
</form>3. 表格标签:展示测试结果
<table>
<thead>
<tr>
<th>用例名称</th>
<th>执行状态</th>
<th>响应时间</th>
<th>执行时间</th>
</tr>
</thead>
<tbody>
<tr>
<td>用户登录接口测试</td>
<td>通过</td>
<td>120ms</td>
<td>2024-01-15 10:30:00</td>
</tr>
<tr>
<td>获取用户信息接口</td>
<td>失败</td>
<td>2000ms</td>
<td>2024-01-15 10:31:00</td>
</tr>
</tbody>
</table>二、CSS基础:网页的化妆师
CSS是什么?
CSS(Cascading Style Sheets)是网页的"化妆师",负责:
- 颜色搭配(让页面好看)
- 布局排版(让内容有序)
- 动画效果(让交互生动)
CSS基础语法
/* 选择器 { 属性: 值; } */
h1 {
color: #2c3e50; /* 文字颜色 */
font-size: 24px; /* 字体大小 */
text-align: center; /* 文字居中 */
}
/* 类选择器:可重复使用 */
.test-button {
background-color: #3498db; /* 背景色 */
color: white; /* 文字色 */
padding: 10px 20px; /* 内边距 */
border: none; /* 无边框 */
border-radius: 5px; /* 圆角 */
cursor: pointer; /* 鼠标样式 */
}
/* ID选择器:唯一标识 */
#main-container {
max-width: 1200px; /* 最大宽度 */
margin: 0 auto; /* 居中对齐 */
padding: 20px; /* 内边距 */
}实战案例:测试工具界面样式
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API测试工具</title>
<style>
/* 全局样式重置 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: #f5f5f5;
color: #333;
}
/* 头部样式 */
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px 0;
text-align: center;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
/* 主容器 */
.container {
max-width: 800px;
margin: 30px auto;
background: white;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
overflow: hidden;
}
/* 表单样式 */
.form-section {
padding: 30px;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.form-group input,
.form-group select,
.form-group textarea {
width: 100%;
padding: 12px;
border: 2px solid #ddd;
border-radius: 5px;
font-size: 14px;
transition: border-color 0.3s ease;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
outline: none;
border-color: #667eea;
}
/* 按钮样式 */
.btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 12px 30px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: transform 0.2s ease;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}
/* 结果展示区域 */
.result-section {
background: #f8f9fa;
padding: 20px;
border-top: 1px solid #eee;
}
.result-box {
background: white;
border: 1px solid #ddd;
border-radius: 5px;
padding: 15px;
font-family: 'Courier New', monospace;
white-space: pre-wrap;
max-height: 300px;
overflow-y: auto;
}
</style>
</head>
<body>
<header class="header">
<h1>🚀 API测试工具</h1>
<p>简单、快速、专业的接口测试平台</p>
</header>
<div class="container">
<div class="form-section">
<form>
<div class="form-group">
<label for="api-url">🔗 API地址</label>
<input type="text" id="api-url" placeholder="https://api.example.com/users">
</div>
<div class="form-group">
<label for="method">📋 请求方法</label>
<select id="method">
<option value="GET">GET</option>
<option value="POST">POST</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
</select>
</div>
<div class="form-group">
<label for="headers">📝 请求头 (JSON格式)</label>
<textarea id="headers" rows="3" placeholder='{"Content-Type": "application/json"}'></textarea>
</div>
<div class="form-group">
<label for="body">📦 请求体</label>
<textarea id="body" rows="5" placeholder='{"username": "test", "password": "123456"}'></textarea>
</div>
<button type="submit" class="btn">🚀 发送请求</button>
</form>
</div>
<div class="result-section">
<h3>📊 响应结果</h3>
<div class="result-box" id="result">
点击"发送请求"按钮查看结果...
</div>
</div>
</div>
</body>
</html>三、CSS布局技术
1. Flexbox布局:现代布局的首选
/* Flexbox容器 */
.flex-container {
display: flex;
justify-content: space-between; /* 水平对齐 */
align-items: center; /* 垂直对齐 */
gap: 20px; /* 间距 */
}
/* 实战:测试工具的工具栏 */
.toolbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px;
background: #f8f9fa;
border-bottom: 1px solid #dee2e6;
}
.toolbar-left {
display: flex;
gap: 10px;
}
.toolbar-right {
display: flex;
gap: 15px;
align-items: center;
}<div class="toolbar">
<div class="toolbar-left">
<button class="btn btn-primary">新建测试</button>
<button class="btn btn-secondary">导入用例</button>
</div>
<div class="toolbar-right">
<span>总计: 25个用例</span>
<button class="btn btn-success">批量执行</button>
</div>
</div>2. Grid布局:二维布局神器
/* Grid容器:测试结果卡片布局 */
.test-results {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
padding: 20px;
}
.result-card {
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
border-left: 4px solid #28a745;
}
.result-card.failed {
border-left-color: #dc3545;
}
.result-card.pending {
border-left-color: #ffc107;
}3. 响应式设计:适配不同设备
/* 移动端优先的响应式设计 */
.container {
width: 100%;
padding: 15px;
}
/* 平板设备 */
@media (min-width: 768px) {
.container {
max-width: 750px;
margin: 0 auto;
padding: 20px;
}
}
/* 桌面设备 */
@media (min-width: 1024px) {
.container {
max-width: 1200px;
padding: 30px;
}
.form-row {
display: flex;
gap: 20px;
}
.form-row .form-group {
flex: 1;
}
}四、实战项目:完整的测试用例管理界面
让我们把学到的知识整合起来,创建一个完整的测试用例管理界面:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试用例管理系统</title>
<style>
/* 基础样式 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #f5f7fa;
color: #2c3e50;
}
/* 导航栏 */
.navbar {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 1rem 2rem;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.navbar h1 {
font-size: 1.5rem;
font-weight: 600;
}
/* 主要内容区域 */
.main-content {
max-width: 1200px;
margin: 2rem auto;
padding: 0 1rem;
}
/* 操作栏 */
.action-bar {
background: white;
padding: 1.5rem;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
margin-bottom: 2rem;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1rem;
}
.search-box {
display: flex;
gap: 1rem;
flex: 1;
max-width: 500px;
}
.search-box input {
flex: 1;
padding: 0.75rem;
border: 2px solid #e1e8ed;
border-radius: 6px;
font-size: 14px;
}
.search-box input:focus {
outline: none;
border-color: #667eea;
}
/* 按钮样式 */
.btn {
padding: 0.75rem 1.5rem;
border: none;
border-radius: 6px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
transition: all 0.2s ease;
text-decoration: none;
display: inline-block;
}
.btn-primary {
background: #667eea;
color: white;
}
.btn-primary:hover {
background: #5a6fd8;
transform: translateY(-1px);
}
.btn-success {
background: #28a745;
color: white;
}
.btn-danger {
background: #dc3545;
color: white;
}
.btn-secondary {
background: #6c757d;
color: white;
}
/* 测试用例表格 */
.test-table {
background: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
overflow: hidden;
}
.table {
width: 100%;
border-collapse: collapse;
}
.table th,
.table td {
padding: 1rem;
text-align: left;
border-bottom: 1px solid #e1e8ed;
}
.table th {
background: #f8f9fa;
font-weight: 600;
color: #495057;
}
.table tbody tr:hover {
background: #f8f9fa;
}
/* 状态标签 */
.status-badge {
padding: 0.25rem 0.75rem;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
}
.status-passed {
background: #d4edda;
color: #155724;
}
.status-failed {
background: #f8d7da;
color: #721c24;
}
.status-pending {
background: #fff3cd;
color: #856404;
}
/* 操作按钮组 */
.action-buttons {
display: flex;
gap: 0.5rem;
}
.btn-sm {
padding: 0.375rem 0.75rem;
font-size: 12px;
}
/* 响应式设计 */
@media (max-width: 768px) {
.action-bar {
flex-direction: column;
align-items: stretch;
}
.search-box {
max-width: none;
}
.table-responsive {
overflow-x: auto;
}
.action-buttons {
flex-direction: column;
}
}
</style>
</head>
<body>
<nav class="navbar">
<h1>🧪 测试用例管理系统</h1>
</nav>
<div class="main-content">
<!-- 操作栏 -->
<div class="action-bar">
<div class="search-box">
<input type="text" placeholder="搜索测试用例..." id="search-input">
<button class="btn btn-primary">🔍 搜索</button>
</div>
<div style="display: flex; gap: 1rem;">
<button class="btn btn-primary">➕ 新建用例</button>
<button class="btn btn-success">▶️ 批量执行</button>
<button class="btn btn-secondary">📤 导出报告</button>
</div>
</div>
<!-- 测试用例表格 -->
<div class="test-table">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>
<input type="checkbox" id="select-all">
</th>
<th>用例名称</th>
<th>所属模块</th>
<th>优先级</th>
<th>执行状态</th>
<th>最后执行时间</th>
<th>执行人</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"></td>
<td>用户登录功能测试</td>
<td>用户管理</td>
<td>高</td>
<td><span class="status-badge status-passed">通过</span></td>
<td>2024-01-15 14:30:25</td>
<td>张三</td>
<td>
<div class="action-buttons">
<button class="btn btn-primary btn-sm">▶️ 执行</button>
<button class="btn btn-secondary btn-sm">✏️ 编辑</button>
<button class="btn btn-danger btn-sm">🗑️ 删除</button>
</div>
</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>商品搜索接口测试</td>
<td>商品管理</td>
<td>中</td>
<td><span class="status-badge status-failed">失败</span></td>
<td>2024-01-15 13:45:12</td>
<td>李四</td>
<td>
<div class="action-buttons">
<button class="btn btn-primary btn-sm">▶️ 执行</button>
<button class="btn btn-secondary btn-sm">✏️ 编辑</button>
<button class="btn btn-danger btn-sm">🗑️ 删除</button>
</div>
</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>订单创建流程测试</td>
<td>订单管理</td>
<td>高</td>
<td><span class="status-badge status-pending">待执行</span></td>
<td>-</td>
<td>-</td>
<td>
<div class="action-buttons">
<button class="btn btn-primary btn-sm">▶️ 执行</button>
<button class="btn btn-secondary btn-sm">✏️ 编辑</button>
<button class="btn btn-danger btn-sm">🗑️ 删除</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>五、总结与下一步
通过这篇文章,我们学习了:
✅ HTML基础结构和常用标签
✅ CSS样式设计和布局技术
✅ 响应式设计原理
✅ 完整的测试工具界面开发
学习要点回顾
- HTML是结构:定义页面内容和层次
- CSS是样式:控制页面外观和布局
- 语义化很重要:使用合适的标签表达内容含义
- 响应式是必须:适配不同设备屏幕
- 用户体验优先:界面要美观、易用、直观
实践建议
- 多写代码:每个知识点都要动手实践
- 模仿优秀设计:学习知名网站的布局和样式
- 关注细节:间距、颜色、字体都很重要
- 测试兼容性:在不同浏览器和设备上测试
🎯 下一步预告:掌握了HTML和CSS,我们就有了"静态"的页面。下一篇文章将学习JavaScript,让页面"动"起来,实现真正的交互功能!
准备好让你的测试工具界面变得更加智能和动态了吗?让我们继续前进!🚀
