如何創建:響應式頁眉
學習如何使用 CSS 創建響應式頁眉。
響應式頁眉
根據屏幕大小來改變頁眉的設計。調整瀏覽器窗口的大小可查看效果。
創建響應式頁眉
第一步 - 添加 HTML:
<div class="header"> <a href="#default" class="logo">CompanyLogo</a> <div class="header-right"> <a class="active" href="#home">Home</a> <a href="#contact">Contact</a> <a href="#about">About</a> </div> </div>
第二步 - 添加 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 鏈接的樣式(請注意,我們將行高和字體大小設置為相同的值,以防止字體變大時頁眉增大) */ .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 導航欄