制作网页css模板
创建基础HTML结构
使用HTML5标准文档结构,包含<!DOCTYPE html>声明和基本的<head>、<body>标签。在<head>中设置字符编码、视口适配和标题。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS模板示例</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header></header>
<main></main>
<footer></footer>
</body>
</html>
编写CSS基础样式
在styles.css中定义全局样式,包括字体、颜色和布局重置。使用CSS变量管理主题色和间距。

:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--text-color: #333;
--spacing-unit: 16px;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
line-height: 1.6;
color: var(--text-color);
background-color: #f9f9f9;
}
设计响应式布局
使用Flexbox或Grid实现自适应布局,添加媒体查询适配不同屏幕尺寸。

header, footer {
padding: var(--spacing-unit);
background-color: var(--primary-color);
color: white;
text-align: center;
}
main {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: var(--spacing-unit);
padding: var(--spacing-unit);
}
@media (max-width: 768px) {
main {
grid-template-columns: 1fr;
}
}
添加交互效果
通过CSS过渡和悬停状态增强用户体验。
button {
background-color: var(--secondary-color);
color: white;
border: none;
padding: 8px 16px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: darken(var(--secondary-color), 10%);
}
.card {
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
transition: transform 0.2s;
}
.card:hover {
transform: translateY(-5px);
}
优化字体与图标
引入Google Fonts和Font Awesome提升视觉表现。
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
body {
font-family: 'Roboto', sans-serif;
}
.icon {
font-size: 24px;
margin-right: 8px;
}






