3個簡單的頁面布局示例


本文內容:

  1.利用HTML5及float布局

  2.利用inline block布局

  3.利用CSS表格(table)布局


 

                  示例代碼 -1 (利用HTML5標簽及浮動布局)

  1. <style>
  2. header nav ul {
  3. margin:15px;
  4. list-style: none;
  5. height:50px;
  6. }
  7. header nav ul li {
  8. font-size:1.5em;
  9. color: coral;
  10. margin:10px;
  11. float: left;
  12. }
  13. header nav ul li a {
  14. text-decoration: none;
  15. }
  16. aside {
  17. margin-right:50px;
  18. float: left;
  19. width:200px;
  20. }
  21. aside ul {
  22. list-style: none;
  23. }
  24. article {
  25. float: left;
  26. }
  27. footer {
  28. font-family:Arial;
  29. clear: both;
  30. text-align: center;
  31. }
  32. </style>
  33. <!--定義頁眉-->
  34. <header>
  35. <nav>
  36. <ul>
  37. <li><ahref="#">主鏈接一</a></li>
  38. <li><ahref="JavaScript:void (0)">主鏈接二</a></li>
  39. <li><aonclick="return false"href="index.html">主鏈接三</a></li>
  40. </ul>
  41. </nav>
  42. </header>
  43. <hr/>
  44. <!--定義左邊側欄導航-->
  45. <aside>
  46. <ul>
  47. <li>左側導航欄鏈接1</li>
  48. <li>左側導航欄鏈接2</li>
  49. </ul>
  50. </aside>
  51. <!--定義右邊內容顯示區-->
  52. <article>
  53. <section>
  54. <h2>主題內容標題1</h2>
  55. <p>
  56. ......
  57. </p>
  58. </section>
  59. <section>
  60. <h2>主題內容標題2</h2>
  61. <p>
  62. .......
  63. </p>
  64. </section>
  65. <section>
  66. <h2>主題內容標題3</h2>
  67. <p>
  68. .......
  69. </p>
  70. </section>
  71. </article>
  72. <!--定義頁腳-->
  73. <footer>
  74. <hr/>
  75. <!--footer的樣式設置為clear:both,以便使其始終居於底部,並占滿整行。-->
  76. &copy;Jener_Yan <spanid="DateSpan"></span>
  77. </footer>
  78. <script>
  79. /*獲取當前年份*/
  80. var d = document.getElementById("DateSpan");
  81. varNowYear=newDate();
  82. d.innerHTML =NowYear.getFullYear();
  83. </script>
網頁效果圖
注意點:浮動的設置
******************************************************************************************************
 
               示例代碼 -2 (利用inline block布局)
  1. <style>
  2. body {
  3. text-align: center;
  4. }
  5. div {
  6. display:inline-block;
  7. min-height:200px;
  8. padding:5px;
  9. margin:0;
  10. }
  11. header, footer {
  12. background-color: aquamarine;
  13. padding:5px;
  14. margin:5px;
  15. }
  16. main {
  17. width:100%;
  18. margin:0;
  19. }
  20. #main-l {
  21. background-color: chartreuse;
  22. width:20%;
  23. }
  24. #main-m {
  25. background-color: bisque;
  26. width:50%;
  27. /*此處的寬度占比不取60%是因為,還得留些空間給內邊距(Padding)和外邊距(Margin)*/
  28. }
  29. #main-r {
  30. background-color: coral;
  31. width:20%;
  32. }
  33. </style>
  34. <h2>inline block布局</h2>
  35. <header>
  36. header
  37. </header>
  38. <main>
  39. <divid="main-l">左側欄寬 20%</div>
  40. <divid="main-m">中部欄寬 50%</div>
  41. <divid="main-r">右側欄寬 20%</div>
  42. </main>
  43. <footer>&copy;Jener_Yan &nbsp;<spanid="DateSpan"></span></footer>
  44. <script>
  45. /*獲取當前年份*/
  46. var d = document.getElementById("DateSpan");
  47. varNowYear=newDate();
  48. d.innerHTML =NowYear.getFullYear();
  49. </script>
網頁效果圖
注意點:width中對margin的預留空間。
******************************************************************************************************
               示例代碼 -3 (利用CSS表格式布局)
示例代碼
  1. <style>
  2. /*全局設置*/
  3. div {
  4. min-height:100px;
  5. margin:0auto;
  6. padding:6px;
  7. }
  8. header, footer {
  9. padding:3px;
  10. background-color: darkgray;
  11. margin:4px0;
  12. width:100%;
  13. }
  14. .container {
  15. text-align: center;
  16. }
  17. .content {
  18. display: table;
  19. /*將div轉化為表格模式顯示*/
  20. width:100%;
  21. }
  22. /*上部三個欄目*/
  23. .upContent {
  24. display: table-row;
  25. /*將div轉換為行顯示*/
  26. }
  27. .upContentL {
  28. background-color:#fffefe;
  29. width:20%;
  30. display: table-cell;
  31. /*將div轉化為單元格顯示*/
  32. }
  33. .upContentM {
  34. background-color: aqua;
  35. width:60%;
  36. display: table-cell;
  37. }
  38. .upContentR {
  39. display: table-cell;
  40. background-color: skyblue;
  41. width:20%;
  42. }
  43. /*下部兩個欄目*/
  44. .downContent {
  45. display: table-row;
  46. width:100%;
  47. }
  48. .downContentL {
  49. background-color: aquamarine;
  50. display: table-cell;
  51. width:30%;
  52. }
  53. .downContentR {
  54. background-color: lightcyan;
  55. width:70%;
  56. display: table-cell;
  57. }
  58. </style>
  59. <divclass="container">
  60. <h2>
  61. CSS表格式布局
  62. </h2>
  63. <header>
  64. header
  65. </header>
  66. <divclass="content">
  67. <divclass="upContent">
  68. <divclass="upContentL">上部左欄 20%</div>
  69. <divclass="upContentM">上部中欄 60%</div>
  70. <divclass="upContentR">上部右欄 20%</div>
  71. </div>
  72. </div>
  73. <divclass="content">
  74. <divclass="downContent">
  75. <divclass="downContentL">下部左欄 20%</div>
  76. <divclass="downContentR">下部右欄 80%</div>
  77. </div>
  78. </div>
  79. <footer>&copy;Jener_Yan &nbsp;<spanid="DateSpan"></span></footer>
  80. </div>
  81. <script>
  82. var date =newDate();
  83. document.getElementById("DateSpan").innerHTML = date.getFullYear();
  84. </script>
網頁效果圖
注意點:采用表格式布局注意顯示(display)屬性值的設置,table → table-row → table-cell 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM