css設(shè)置側(cè)邊欄的方法:首先創(chuàng)建一個HTML示例文件;然后在body中設(shè)置導(dǎo)航欄內(nèi)容;最后通過設(shè)置css樣式為“#sidemenu:checked + aside {left: 0;}…”來實現(xiàn)側(cè)邊欄效果即可。
本文操作環(huán)境:windows7系統(tǒng)、HTML5&&CSS3版、Dell G3電腦。
css怎么設(shè)置側(cè)邊欄?
CSS實現(xiàn)側(cè)邊欄導(dǎo)航
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> /*隱藏checked復(fù)選框*/ #sidemenu{ display: none; } #sidemenu:checked + aside { /*為被選中的sidemenu后的aside設(shè)置屬性(緊鄰)*/ left: 0; /*點擊按鈕即選中checked后,側(cè)邊欄位置變?yōu)橘N著左邊,配合ease-out使用,有漸變滑出的效果*/ } #sidemenu:checked ~ #wrap { /*為被選中的sidemenu后的wrap設(shè)置屬性(非緊鄰)*/ padding-left: 220px; } aside { /*側(cè)邊欄,初始位置為-200px,即隱藏效果*/ position: absolute; top: 0; bottom: 0; left: -200px; width: 200px; background: black; transition: 0.2s ease-out; /*動畫效果的執(zhí)行方式是ease-out,即側(cè)邊欄滑動效果為漸變式,而不是生硬的突然變化*/ } h2 { color: white; text-align: center; font-size: 2em; } /*控制側(cè)邊欄進出的按鈕(外部包裹)*/ #wrap { margin-left: 20px; padding: 10px; transition: 0.2s ease-out; } /*控制側(cè)邊欄進出的按鈕(內(nèi)部文字樣式)*/ label { /*控制側(cè)邊欄進出的按鈕*/ background: white; border-radius: 70px; color: orange; cursor: pointer; display: block; font-family: Courier New; font-size: 2em; width: 1.5em; height: 1.5em; line-height: 1.5em; text-align: center; display: inline-block; } label:hover { background: #000; } #sideul li { list-style: none; color: white; width: 100%; height: 1.8em; font-size: 1.5em; text-align: center; } a { text-decoration: none; } #sideul li:hover { color: orange; } </style> </head> <body> <input type='checkbox' id='sidemenu'> <aside> <h2>主菜單</h2> <br /> <ul id="sideul"> <a href="##"> <li>首頁</li> </a> <a href="##"> <li style="color: orange;">導(dǎo)航1</li> </a> <a href="##"> <li>導(dǎo)航2</li> </a> <a href="##"> <li>導(dǎo)航3</li> </a> <a href="##"> <li>導(dǎo)航4</li> </a> <a href="##"> <li>導(dǎo)航5</li> </a> <a href="##"> <li>導(dǎo)航6?</li> </a> </ul> </aside> <p id='wrap'> <label id='sideMenuControl' for='sidemenu'>≡</label> <!--for 屬性規(guī)定 label 與哪個表單元素綁定,即將這個控制側(cè)邊欄進出的按鈕與checkbox綁定--> </p> </body></html>
【推薦學(xué)習(xí):《css視頻教程》】