現在很多網頁的布局是左邊是導航,右邊是正文,這樣看起來簡單、大方,我們公司的網站就是這樣設計的,有兩種錯誤的布局如下:
圖一和圖二兩種都是錯誤的情況,圖一沒有給左邊導航設置高度,圖二給左邊設置了高度為屏幕的高度,當出現滾動條的時候,顯示的也很丑。
正確的情況應該是,左邊和右邊的高度總是相等
代碼如下:
<div class="container"> <div class="left"> <div>1導航</div> <div>2導航</div> <div>3導航</div> <div>4導航</div> </div> <div class="right"> <div>sss</div> <div>test</div> <div>test</div> <div>test</div> </div> </div> <style> body { margin: 0; padding: 0; text-align: center; } .container { overflow: hidden; } .left { width: 20%; float: left; background-color: #e2e2e2; min-height: 100%; margin-bottom: -99999px; padding: 0 0 99999px; } .left div { border-bottom: 1px solid #ccc; color: #6f6f6f; display: block; padding: 20px 2px; text-decoration: none; } .right { width: 80%; float: left; } .right div { height: 100px; background-color: #ddd; } </style>
或者需要左邊導航高度最少為屏幕的高度,只要給導航設置最小高度為100%,前提是它父親的高度為100%;
代碼如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div class="container"> <div class="left"> <div>1導航</div> <div>2導航</div> <div>3導航</div> <div>4導航</div> </div> <div class="right"> <div>sss</div> <div>test</div> <div>test</div> <div>test</div> </div> <div></div> </div> </body> <style> html, body { margin: 0; padding: 0; text-align: center; height: 100%; } .container { overflow: hidden; height: 100%; } .left { width: 20%; float: left; background-color: #e2e2e2; min-height: 100%; margin-bottom: -99999px; padding: 0 0 99999px; } .left div { border-bottom: 1px solid #ccc; color: #6f6f6f; display: block; padding: 20px 2px; text-decoration: none; } .right { width: 80%; float: left; } .right div { height: 100px; background-color: #ddd; } </style> </html>