如何用HTML加css构建一个介绍书籍的网页(好看)要代码复杂
时间: 2024-03-17 12:42:30 浏览: 8
好的,以下是一个更复杂一点的书籍介绍网页的HTML和CSS代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>书籍介绍</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-8XuBb16zN6ajN7vzq9c5cKJZQ3Z9C01Wq8/pwPZsUt76Jt6BcYxqP8YU8OJyM5MkxZrL8UZ32E7BvO9JtL1x+w==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
padding: 20px;
display: flex;
align-items: center;
justify-content: space-between;
}
header a {
color: #fff;
text-decoration: none;
font-size: 24px;
margin-right: 20px;
}
header a:hover {
text-decoration: underline;
}
header i {
cursor: pointer;
font-size: 24px;
display: none;
}
header nav {
display: none;
position: absolute;
top: 80px;
right: 0;
background-color: #fff;
box-shadow: 0 2px 5px rgba(0,0,0,0.3);
padding: 20px;
border-radius: 5px;
z-index: 2;
}
header nav a {
display: block;
color: #333;
font-size: 18px;
margin-bottom: 10px;
text-decoration: none;
transition: color 0.3s ease;
}
header nav a:hover {
color: #666;
}
.container {
margin: 20px;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.book {
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.3);
margin-bottom: 20px;
width: calc(33.33% - 10px);
padding: 20px;
box-sizing: border-box;
position: relative;
overflow: hidden;
}
.book::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.3);
opacity: 0;
transition: opacity 0.3s ease;
}
.book:hover::before {
opacity: 1;
}
.book img {
display: block;
margin: 0 auto;
max-width: 100%;
height: auto;
margin-bottom: 20px;
}
.book h2 {
font-size: 24px;
margin: 0;
margin-bottom: 10px;
color: #fff;
position: absolute;
bottom: 20px;
left: 20px;
z-index: 1;
}
.book p {
font-size: 16px;
margin: 0;
margin-bottom: 20px;
}
.book a {
display: block;
background-color: #333;
color: #fff;
padding: 10px;
text-align: center;
border-radius: 5px;
text-decoration: none;
transition: background-color 0.3s ease;
}
.book a:hover {
background-color: #444;
}
@media only screen and (max-width: 768px) {
header i {
display: block;
}
header nav {
display: block;
}
header nav a {
font-size: 24px;
}
.container {
margin: 20px auto;
max-width: 800px;
}
.book {
width: 100%;
margin-right: 0;
margin-left: 0;
}
}
</style>
</head>
<body>
<header>
<a href="#">书籍介绍</a>
<i class="fas fa-bars"></i>
<nav>
<a href="#">首页</a>
<a href="#">推荐</a>
<a href="#">分类</a>
<a href="#">关于我们</a>
</nav>
</header>
<div class="container">
<div class="book">
<img src="book1.jpg" alt="书籍1">
<h2>书籍1</h2>
<p>这是一本非常好看的书籍1。</p>
<a href="#">阅读更多</a>
</div>
<div class="book">
<img src="book2.jpg" alt="书籍2">
<h2>书籍2</h2>
<p>这是一本非常好看的书籍2。</p>
<a href="#">阅读更多</a>
</div>
<div class="book">
<img src="book3.jpg" alt="书籍3">
<h2>书籍3</h2>
<p>这是一本非常好看的书籍3。</p>
<a href="#">阅读更多</a>
</div>
</div>
<script>
document.querySelector("header i").addEventListener("click", function() {
document.querySelector("header nav").classList.toggle("active");
});
</script>
</body>
</html>
```
这个代码包含了响应式设计和一个下拉式菜单,可以在移动设备上和桌面设备上自适应。你可以将上面的代码复制到一个HTML文件中,并将书籍的图片替换为你自己的图片。然后将HTML和图片文件上传到Web服务器,以便在浏览器中查看你的网页。