css制作网站代码


CSS制作网站代码示例
以下是一个完整的HTML和CSS代码示例,展示如何使用CSS制作一个简单的网站页面。
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>
<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>
<li><a href="#">联系我们</a></li>
</ul>
</nav>
</header>
<main>
<section class="hero">
<h1>欢迎来到我们的网站</h1>
<p>这是一个使用CSS制作的响应式网站示例</p>
<button>了解更多</button>
</section>
<section class="features">
<div class="feature">
<h2>功能一</h2>
<p>描述功能一的详细信息</p>
</div>
<div class="feature">
<h2>功能二</h2>
<p>描述功能二的详细信息</p>
</div>
<div class="feature">
<h2>功能三</h2>
<p>描述功能三的详细信息</p>
</div>
</section>
</main>
<footer>
<p>© 2023 示例网站. 版权所有.</p>
</footer>
</body>
</html>
CSS样式
/* 全局样式 */
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
line-height: 1.6;
color: #333;
}
/* 导航栏样式 */
header {
background-color: #2c3e50;
color: white;
padding: 1rem 0;
}
nav ul {
display: flex;
justify-content: center;
list-style: none;
padding: 0;
}
nav ul li {
margin: 0 1rem;
}
nav ul li a {
color: white;
text-decoration: none;
padding: 0.5rem 1rem;
}
nav ul li a:hover {
background-color: #34495e;
border-radius: 4px;
}
/* 主要内容区样式 */
.hero {
text-align: center;
padding: 4rem 2rem;
background-color: #ecf0f1;
}
.hero h1 {
font-size: 2.5rem;
margin-bottom: 1rem;
}
.hero p {
font-size: 1.2rem;
margin-bottom: 2rem;
}
.hero button {
background-color: #3498db;
color: white;
border: none;
padding: 0.8rem 1.5rem;
font-size: 1rem;
border-radius: 4px;
cursor: pointer;
}
.hero button:hover {
background-color: #2980b9;
}
/* 功能区块样式 */
.features {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
padding: 2rem;
}
.feature {
flex: 1;
min-width: 300px;
margin: 1rem;
padding: 1.5rem;
background-color: white;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
border-radius: 4px;
}
.feature h2 {
color: #2c3e50;
}
/* 页脚样式 */
footer {
text-align: center;
padding: 1rem;
background-color: #2c3e50;
color: white;
}
响应式设计
/* 响应式设计 */
@media (max-width: 768px) {
nav ul {
flex-direction: column;
align-items: center;
}
nav ul li {
margin: 0.5rem 0;
}
.features {
flex-direction: column;
}
}
动画效果
/* 添加简单动画 */
.feature {
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.feature:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
这个示例包含了网站的基本结构、导航栏、主要内容区和页脚,并实现了响应式设计和简单的交互效果。可以根据实际需求修改颜色、布局和其他样式属性。






