css页眉制作
使用CSS创建固定页眉
固定页眉会始终停留在页面顶部,即使页面滚动。以下代码实现一个简单的固定页眉:
.header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 60px;
background-color: #333;
color: white;
padding: 10px 20px;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
z-index: 1000;
}
制作响应式页眉
响应式页眉能适应不同屏幕尺寸。使用媒体查询调整布局:

.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 5%;
}
@media (max-width: 768px) {
.header {
flex-direction: column;
padding: 10px;
}
}
添加导航菜单的页眉
在页眉中整合导航菜单的样式:
.nav-menu {
display: flex;
list-style: none;
}
.nav-menu li {
margin-left: 20px;
}
.nav-menu a {
color: white;
text-decoration: none;
transition: color 0.3s ease;
}
.nav-menu a:hover {
color: #f0f0f0;
}
透明渐变效果的页眉
创建具有透明渐变效果的页眉:

.header {
background: linear-gradient(to right, rgba(0,0,0,0.8), rgba(0,0,0,0.5));
backdrop-filter: blur(5px);
transition: background 0.3s ease;
}
.header.scrolled {
background: rgba(0,0,0,0.9);
}
需要配合JavaScript监听滚动事件添加scrolled类。
带logo和搜索框的页眉
复合型页眉的样式设计:
.header-content {
display: flex;
align-items: center;
max-width: 1200px;
margin: 0 auto;
}
.logo {
height: 40px;
margin-right: 30px;
}
.search-box {
flex-grow: 1;
max-width: 500px;
}
.search-box input {
width: 100%;
padding: 8px 15px;
border-radius: 20px;
border: none;
}






