如何做 - 响应式页眉
了解如何使用 CSS 创建响应式页眉。
响应式页眉
根据屏幕大小更改页眉的设计。调整浏览器窗口大小以查看效果。
创建响应式页眉
步骤 1) 添加 HTML
示例
<div class="header">
<a href="#default" class="logo">公司 Logo</a>
<div class="header-right">
<a class="active" href="#home">首页</a>
<a href="#contact">联系</a>
<a href="#about">关于</a>
</div>
</div>
步骤 2) 添加 CSS
示例
/* 使用灰色背景和一些内边距样式化页眉 */
.header {
overflow: hidden;
background-color: #f1f1f1;
padding: 20px 10px;
}
/* 样式化页眉链接 */
.header a {
float: left;
color: black;
text-align: center;
padding: 12px;
text-decoration: none;
font-size: 18px;
line-height: 25px;
border-radius: 4px;
}
/* 样式化 Logo 链接(请注意,我们将 line-height 和 font-size 设置为相同的值,以防止字体变大时页眉也随之增大) */
.header a.logo {
font-size: 25px;
font-weight: bold;
}
/* 鼠标悬停时更改背景颜色 */
.header a:hover {
background-color: #ddd;
color: black;
}
/* 样式化活动/当前链接 */
.header a.active {
background-color: dodgerblue;
color: white;
}
/* 将链接部分浮动到右侧 */
.header-right {
float: right;
}
/* 添加媒体查询以实现响应式 - 当屏幕宽度为 500px 或更小时,将链接堆叠在彼此之上 */
@media screen and (max-width: 500px) {
.header a {
float: none;
display: block;
text-align: left;
}
.header-right {
float: none;
}
}
自己动手试一试 »
提示:访问我们的 CSS 导航栏教程 了解更多关于导航栏的信息。