新聞中心
在今天的文章中,我們將一起來學習如何用 HTML 和 CSS 制作響應(yīng)式導航欄效果。

在鐵門關(guān)等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作按需策劃設(shè)計,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,全網(wǎng)整合營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),鐵門關(guān)網(wǎng)站建設(shè)費用合理。
這篇文章主要是面向初學者的,如果你是有經(jīng)驗的開發(fā)者,請直接跳過或者忽略。
在這篇文章中,我們將一起來實現(xiàn)一個響應(yīng)式導航欄效果,實現(xiàn)后,你可以在你的任何項目中使用它。
現(xiàn)在,我們就開始吧。
01、添加基本的 HTML結(jié)構(gòu)
HTML 是一門超文本標記語言。它的主要工作是給我們的項目創(chuàng)建友好的網(wǎng)頁結(jié)構(gòu)。我們將使用這種標記語言來完成我們的項目結(jié)構(gòu)。
現(xiàn)在,讓我們一起來看看我們項目的 HTML 代碼結(jié)構(gòu),具體如下:
如何用 HTML和CSS 實現(xiàn)一個響應(yīng)式導航欄效果
- 我們將從制作一個新的 HTML 文件開始。為了在多種字體樣式之間切換,我們將在頭部部分添加指向外部 CSS 文件和 Google 字體的鏈接。
- 現(xiàn)在,利用
現(xiàn)在,讓我們看看頁面的結(jié)構(gòu)輸出的效果:
02、為響應(yīng)式導航添加CSS代碼
body {
padding: 0;
margin: 0;
}
.container {
position: relative;
margin-top: 100px;
}
.container img {
display: block;
width: 100%;
}
nav {
position: fixed;
z-index: 10;
left: 0;
right: 0;
top: 0;
font-family: "Montserrat", "sans-serif";
height: 100px;
background-color: #00a6bc;
padding: 0 5%;
}
nav .logo {
float: left;
width: 40%;
height: 100%;
display: flex;
align-items: center;
font-size: 24px;
color: #fff;
}
nav .links {
float: right;
padding: 0;
margin: 0;
width: 60%;
height: 100%;
display: flex;
justify-content: space-around;
align-items: center;
}
nav .links li {
list-style: none;
}
nav .links a {
display: block;
padding: 1em;
font-size: 16px;
font-weight: bold;
color: #fff;
text-decoration: none;
position: relative;
}
nav .links a:hover {
color: white;
}
nav .links a::before {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 2px;
background-color: white;
visibility: hidden;
transform: scaleX(0);
transition: all 0.3s ease-in-out 0s;
}
nav .links a:hover::before {
visibility: visible;
transform: scaleX(1);
color: white;
}
#nav-toggle {
position: absolute;
top: -100px;
}
nav .icon-burger {
display: none;
position: absolute;
right: 5%;
top: 50%;
transform: translateY(-50%);
}
nav .icon-burger .line {
width: 30px;
height: 5px;
background-color: #fff;
margin: 5px;
border-radius: 3px;
transition: all 0.5s ease-in-out;
}
@media screen and (max-width: 768px) {
nav .logo {
float: none;
width: auto;
justify-content: center;
}
nav .links {
float: none;
position: fixed;
z-index: 9;
left: 0;
right: 0;
top: 100px;
bottom: 100%;
width: auto;
height: auto;
flex-direction: column;
justify-content: space-evenly;
background-color: rgba(0, 0, 0, 0.8);
overflow: hidden;
transition: all 0.5s ease-in-out;
}
nav .links a {
font-size: 20px;
}
nav :checked ~ .links {
bottom: 0;
}
nav .icon-burger {
display: block;
}
nav :checked ~ .icon-burger .line:nth-child(1) {
transform: translateY(10px) rotate(225deg);
}
nav :checked ~ .icon-burger .line:nth-child(3) {
transform: translateY(-10px) rotate(-225deg);
}
nav :checked ~ .icon-burger .line:nth-child(2) {
opacity: 0;
}
}添加完CSS后,我們將得到如下效果:
當我們把瀏覽器的窗口逐漸縮小時,頁面上的導航菜單也會折疊起來:
點擊折疊漢堡菜單,我們就會看到導航欄其他內(nèi)容,效果如下:
03、解釋一下CSS代碼
第 1 步:我們將使用 body 標簽將網(wǎng)頁上的邊距和填充設(shè)置為“0”。
我們將使用類選擇器 (.container) 來設(shè)計我們的圖像。我們?yōu)閳D像添加了“100px”的上邊距。我們的圖像寬度設(shè)置為 100%,其顯示設(shè)置為“block”。
body {
padding: 0;
margin: 0;
}
.container {
position: relative;
margin-top: 100px;
}
.container img {
display: block;
width: 100%;
}
第 2 步:我們現(xiàn)在將使用標簽選擇器 (nav) 自定義導航欄。
為了讓它看起來更靠近窗口,我們將位置固定在網(wǎng)頁上,并將 z-index 增加到 10。選擇了“Montesirat”字體系列。高度設(shè)置為“100px”,背景色為“#00a6bc”。
nav {
position: fixed;
z-index: 10;
left: 0;
right: 0;
top: 0;
font-family: "Montserrat", "sans-serif";
height: 100px;
background-color: #00a6bc;
padding: 0 5%;
}
第 3 步:使用類選擇器,我們現(xiàn)在將設(shè)置我們的子元素 (.logo) 的樣式。
我們指定它應(yīng)該“浮動到窗口的左側(cè)”。高度和寬度的定義分別為“100%”和“40%”。
文章居中對齊,字體顏色為“白色”。
我們?yōu)閷Ш綑谥械逆溄犹砑恿艘恍邮健K麄兊奈恢帽幻枋鰹椤跋蛴腋印?。寬度和高度的定義分別為“60%”和“100%”。這些項目居中并且顯示設(shè)置為“flex”。
nav .logo {
float: left;
width: 40%;
height: 100%;
display: flex;
align-items: center;
font-size: 24px;
color: #fff;
}
nav .links {
float: right;
padding: 0;
margin: 0;
width: 60%;
height: 100%;
display: flex;
justify-content: space-around;
align-items: center;
}
第4步:現(xiàn)在我們將樣式添加到我們的字體鏈接。
字體大小設(shè)置為 16px,文本裝飾為無,字體顏色為“白色”,我們添加了一些懸停屬性,當用戶懸停時,白色底部邊框?qū)⒊霈F(xiàn)在鏈接下方。
nav .links {
float: right;
padding: 0;
margin: 0;
width: 60%;
height: 100%;
display: flex;
justify-content: space-around;
align-items: center;
}
nav .links li {
list-style: none;
}
nav .links a {
display: block;
padding: 1em;
font-size: 16px;
font-weight: bold;
color: #fff;
text-decoration: none;
position: relative;
}
nav .links a:hover {
color: white;
}
nav .links a::before {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 2px;
background-color: white;
visibility: hidden;
transform: scaleX(0);
transition: all 0.3s ease-in-out 0s;
}
nav .links a:hover::before {
visibility: visible;
transform: scaleX(1);
color: white;
}
#nav-toggle {
position: absolute;
top: -100px;
}
nav .icon-burger {
display: none;
position: absolute;
right: 5%;
top: 50%;
transform: translateY(-50%);
}
nav .icon-burger .line {
width: 30px;
height: 5px;
background-color: #fff;
margin: 5px;
border-radius: 3px;
transition: all 0.5s ease-in-out;
}
第 5 步:現(xiàn)在窗口的屏幕尺寸已經(jīng)減小到等于窗口的規(guī)定寬度。
我們將在 CSS 中添加一個響應(yīng)性和測試功能,以添加一個切換欄來顯示菜單項。
@media screen and (max-width: 768px) {
nav .logo {
float: none;
width: auto;
justify-content: center;
}
nav .links {
float: none;
position: fixed;
z-index: 9;
left: 0;
right: 0;
top: 100px;
bottom: 100%;
width: auto;
height: auto;
flex-direction: column;
justify-content: space-evenly;
background-color: rgba(0, 0, 0, 0.8);
overflow: hidden;
transition: all 0.5s ease-in-out;
}
nav .links a {
font-size: 20px;
}
nav :checked ~ .links {
bottom: 0;
}
nav .icon-burger {
display: block;
}
nav :checked ~ .icon-burger .line:nth-child(1) {
transform: translateY(10px) rotate(225deg);
}
nav :checked ~ .icon-burger .line:nth-child(3) {
transform: translateY(-10px) rotate(-225deg);
}
nav :checked ~ .icon-burger .line:nth-child(2) {
opacity: 0;
}
}到這里,我們要實現(xiàn)的效果就算完成了,希望你也已經(jīng)學會了怎么使用 HTML 和 CSS 來實現(xiàn)一個響應(yīng)式導航欄效果了。
分享文章:如何用HTML和CSS實現(xiàn)一個響應(yīng)式導航欄效果
標題路徑:http://fisionsoft.com.cn/article/dhgcjhp.html


咨詢
建站咨詢
