0


HTML--浮动布局练习

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. /* 整个浏览器页面 */
  9. html,
  10. body {
  11. background-color: #00005d;
  12. height: 100%;
  13. margin: 0;
  14. }
  15. .page {
  16. /* 定宽 */
  17. width: 1100px;
  18. margin-top: 0;
  19. margin-bottom: 0;
  20. margin-right: auto;
  21. margin-left: auto;
  22. /* margin:0,auto; 不支持margin的多个属性值,F12显示属性值无效*/
  23. height: 100%;
  24. }
  25. header {
  26. background-color: #869dc7;
  27. height: 150px;
  28. /* 原图像素254*150 */
  29. background-image: url(images/lighthouselogo.jpg);
  30. background-repeat: no-repeat;
  31. }
  32. h1 {
  33. color: #00005d;
  34. font-size: 55px;
  35. font-family: Verdana, Geneva, Tahoma, sans-serif;
  36. font-weight: bold;
  37. text-align: center;
  38. line-height: 150px;
  39. margin: 0;
  40. }
  41. aside {
  42. background-color: #b3c7e6;
  43. width: 180px;
  44. margin: 0;
  45. /* height: 620px; */
  46. height: calc(100% - 150px - 50px);
  47. float: left;
  48. }
  49. main {
  50. background-color: white;
  51. width: 920px;
  52. /* height: 620px; */
  53. height: calc(100% - 150px - 50px);
  54. float: right;
  55. }
  56. h2 {
  57. color: #90a5cb;
  58. padding-top: 10px;
  59. padding-left: 20px;
  60. font-weight: bold;
  61. font-size: 25px;
  62. }
  63. main p {
  64. padding-left: 20px;
  65. font-family: Verdana, Geneva, Tahoma, sans-serif;
  66. font-size: 18px;
  67. }
  68. footer {
  69. background-color: #869dc7;
  70. height: 50px;
  71. /* 如果不加clear属性,会受到中间元素float影响移动到上边 */
  72. clear: both;
  73. /* 水平居中 */
  74. text-align: center;
  75. /* 无法利用margin:0,auto;属性 借助line-height=height */
  76. line-height: 50px;
  77. font-family: Arial, Helvetica, sans-serif;
  78. font-size: 14px;
  79. color: #00005d;
  80. }
  81. </style>
  82. </head>
  83. <body>
  84. <section class="page">
  85. <header>
  86. <h1>Lighthouse Island Bistro</h1>
  87. </header>
  88. <!-- <div class="middle"> -->
  89. <aside>
  90. <ul
  91. style="list-style: none;font-family: Arial, Helvetica, sans-serif;font-weight: bold;color: white;font-size: 20px;">
  92. <li style="color: #eae9ea;">Home</li>
  93. <li>Menu</li>
  94. <li>Directions</li>
  95. <li>Contact</li>
  96. </ul>
  97. </aside>
  98. <main>
  99. <div style="float: right;clear: both;">
  100. <img src="images/lighthouseisland.jpg" style="padding: 20px;padding-top:40px;height: 400px;">
  101. </div>
  102. <h2 style="padding-top: 20px;">Locally Roasted Free-Trade Coffee</h2>
  103. <p>Indulge in the aroma of freshly ground roast coffee. Specialty drinks are available hot or cold.
  104. </p>
  105. <h2>Specialty Pastries</h2>
  106. <p>Enjoy a selection of our fresh-baked, organic pastries, including fresh-fruit muffins, scones,
  107. croissants, and cinammon rolls.
  108. </p>
  109. <h2>Lunchtime is Anytime</h2>
  110. <p>Savor delicious wraps and sandwiches on hearty, whole-grain breads with locally-grown salad, fruit,
  111. and vegetables.
  112. </p>
  113. <h2>Panoramic View</h2>
  114. <p>Take in some scenery! The top of our lighthouse offers a panoramic view of the countryside.
  115. Challenge your friends to climb our 100-stair tower.
  116. </p>
  117. </main>
  118. <!-- </div> -->
  119. <footer>Copyright © 2021
  120. </footer>
  121. </section>
  122. </body>
  123. </html>

定宽浮动布局,设计页面在整个浏览器页面水平居中位置,浏览器页面缩小时,仍水平居中,出现水平滚动条,显示不到的页面内容可以通过滚动条调节。

已知header高度、footer高度,使得aside的高度处于页面中间,并且不出现垂直滚动条?

cal(相对高度百分比 - 同级别元素高度1 - 同级别元素高度2);

相对高度100%--是相对父元素的高度,要求向上追溯父元素的时候有一个确切的数值(html的高度设置为100%时就是整个浏览器页面高度);另外还需要注意margin等高度,相对高度是相对父元素内容区的高度,所以要排除元素margin、padding等高度影响。

标签: 前端 javascript css

本文转载自: https://blog.csdn.net/weixin_73223235/article/details/143254661
版权归原作者 xixihaha_dddddd 所有, 如有侵权,请联系我们删除。

“HTML--浮动布局练习”的评论:

还没有评论