css制作网站代码
基础HTML结构
使用HTML5标准文档结构,包含<!DOCTYPE>声明和基本标签。以下是一个典型的网站基础框架:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>网站标题</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#">首页</a></li>
<li><a href="#">产品</a></li>
<li><a href="#">关于</a></li>
</ul>
</nav>
</header>
<main>
<section class="hero">
<h1>欢迎来到我们的网站</h1>
</section>
</main>
<footer>
<p>© 2023 公司名称</p>
</footer>
</body>
</html>
基本CSS样式
创建styles.css文件并添加基础样式规则。这些样式包括全局重置、排版和布局设置:

/* 全局样式重置 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
line-height: 1.6;
color: #333;
background-color: #f4f4f4;
}
/* 导航栏样式 */
header {
background: #35424a;
color: #ffffff;
padding: 1rem 0;
}
nav ul {
display: flex;
justify-content: center;
list-style: none;
}
nav ul li {
margin: 0 1rem;
}
nav a {
color: white;
text-decoration: none;
font-weight: bold;
}
/* 主要内容区域 */
.hero {
text-align: center;
padding: 4rem 2rem;
background: #e8491d;
color: white;
}
/* 页脚样式 */
footer {
text-align: center;
padding: 1rem 0;
background: #35424a;
color: white;
position: fixed;
bottom: 0;
width: 100%;
}
响应式设计
添加媒体查询使网站在不同设备上都能良好显示:
@media (max-width: 768px) {
nav ul {
flex-direction: column;
align-items: center;
}
nav ul li {
margin: 0.5rem 0;
}
.hero {
padding: 2rem 1rem;
}
}
交互效果
为链接和按钮添加悬停效果,提升用户体验:

nav a:hover {
color: #e8491d;
transition: color 0.3s ease;
}
.button {
display: inline-block;
background: #35424a;
color: white;
padding: 0.5rem 1rem;
border: none;
border-radius: 5px;
cursor: pointer;
text-decoration: none;
}
.button:hover {
background: #e8491d;
transition: background 0.3s ease;
}
网格布局
使用CSS Grid创建复杂的页面布局:
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 1rem;
padding: 1rem;
}
.grid-item {
background: white;
padding: 1rem;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
动画效果
添加简单的CSS动画增强视觉效果:
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.hero h1 {
animation: fadeIn 1s ease-in;
}
.card {
transition: transform 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
}
这些代码片段提供了构建现代网站所需的基本CSS功能,可以根据具体项目需求进行扩展和修改。






