在css中,可以利用position屬性把導(dǎo)航欄固定住,只需要給導(dǎo)航欄元素添加“position:fixed;”樣式,將導(dǎo)航欄相對(duì)于瀏覽器窗口進(jìn)行固定定位即可,這樣被固定的導(dǎo)航欄元素就不會(huì)隨著滾動(dòng)條的拖動(dòng)而改變位置。
本教程操作環(huán)境:windows7系統(tǒng)、CSS3&&HTML5版、Dell G3電腦。
css怎么把導(dǎo)航欄固定住
可以給導(dǎo)航欄添加固定定位是導(dǎo)航來固定住。語法為“position:fixed;”。
示例如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style type="text/css"> *{margin:0;padding: 0;} ul{ list-style-type: none; overflow: hidden; background-image:url(1118.02.png); position: fixed; top: 0; width: 100%; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover{ background-color: red; } </style> </head> <body> <ul> <li><a href="#home">首頁</a></li> <li><a href="#news">新聞動(dòng)態(tài)</a></li> <li><a href="#contact">聯(lián)系我們</a></li> <li><a href="#about">關(guān)于我們</a></li> </ul> <div style="background-color:pink;height:1500px;"></div> </body> </html>
輸出結(jié)果:
(學(xué)習(xí)視頻分享:css視頻教程)