PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > 网站布局之:浮动(float)和定位(position)详解及运用

网站布局之:浮动(float)和定位(position)详解及运用

Jason Pu?
Jason Pu? 原创
2020年12月22日 14:16:57 1731浏览

一:浮动float

浮动(float)元素可以脱离文档流进行水平方向(left/right)移动,后面的元素会填充它原来的空间,任何一个元素浮动后都会具有块元素特征,但是父元素在计算高度时,会忽略内部的浮动元素,引起高度塌陷,我们可以使用伪元素和BFC两种途径解决此问题。
例如下面是一只因子元素浮动而高度塌陷的盒子,我们使用两种方法来拯救它:

拯救代码:

  1. <style>
  2. .box{
  3. border:1px solid #000;
  4. }
  5. .content {
  6. width: 10em;
  7. height: 10em;
  8. text-align: center;
  9. line-height: 10em;
  10. background-color: skyblue;
  11. float: left;
  12. }
  13. /* 方法1:使用伪元素 */
  14. /* .box:after{
  15. content:'';
  16. display: block;
  17. clear: both;
  18. } */
  19. /* 方法2:BFC */
  20. /* .box{
  21. overflow: hidden;
  22. } */
  23. </style>
  24. </head>
  25. <body>
  26. <div class="box">
  27. <div class="content">我是一个浮动元素</div>
  28. </div>
  29. </body>

二:定位属性(position)

position属性有四个可选值,分别为static、relative、absolute、fixed。
1.static :默认值,不是定位元素。

2.relative :相对定位,相对自身位置定位,可通过设置left、top、right、bottom的值来设置位置,并且它原本所占的空间不变,不会影响其他元素布局,作用:微调元素,作为绝对定位的参考。

3.absolute :绝对定位,相对于它的祖先中离它最近的”定位元素”的位置发生偏移,static不算,如果没有,则相对于html定位,脱离标准文档流

4.fixed :固定定位 始终相对于html进行定位,不会随页面滚动而变,作用:固定广告位窗口,侧边客服窗口,模态框

三:定位实战:写一个模态框

要求:点击“登陆”显示模态框,点击“X”关闭模态框
实现效果如下:
点击前:

点击后:

html代码:

  1. <title>模态框</title>
  2. <link rel="stylesheet" href="test2.css">
  3. </head>
  4. <body>
  5. <header>
  6. <h2>php中文网</h2>
  7. <button>登陆</button>
  8. </header>
  9. <div class="modal">
  10. <!-- 半透明蒙版: -->
  11. <div class="cover"></div>
  12. <!-- 主体 -->
  13. <div class="modal-body">
  14. <button class="close">X</button>
  15. <form action="" method="post">
  16. <table>
  17. <caption>用户登陆</caption>
  18. <tr>
  19. <td> <label for="account">账号:</label> </td>
  20. <td> <input type="text" name="account" id="account"> </td>
  21. </tr>
  22. <tr>
  23. <td> <label for="password">密码:</label> </td>
  24. <td> <input type="password" name="password" id="password"> </td>
  25. </tr>
  26. <tr>
  27. <td> </td>
  28. <td> <input type="submit" value="登陆" id="submit" > </td>
  29. </tr>
  30. </table>
  31. </form>
  32. </div>
  33. </div>
  34. <script src="test2.js"></script>
  35. </body>
  36. </html>

css代码:

  1. *{
  2. margin:0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. /* 蒙版: */
  7. .cover {
  8. background-color:rgb(0,0,0,0.5);
  9. position: fixed;
  10. top: 0;
  11. bottom: 0;
  12. left: 0;
  13. right: 0;
  14. }
  15. /* 页眉: */
  16. header {
  17. background-color:#031F47;
  18. padding:.5em 2em;
  19. overflow: hidden;
  20. }
  21. header h2 {
  22. float: left;
  23. font-style:oblique;
  24. color:#208EFF;
  25. }
  26. header button {
  27. float: right;
  28. width:6em;
  29. height: 2.5em;
  30. background-color: #08E4F6;
  31. border-radius: 1em;
  32. }
  33. header button:hover{
  34. cursor: pointer;
  35. background-color: #0DFFE9;
  36. }
  37. .modal .modal-body{
  38. padding:1em;
  39. width:20em;
  40. box-shadow:2px 2px 5px #000; ;
  41. background:linear-gradient(to right bottom,#fff,#208EFF);
  42. border-radius: 0.5em;
  43. /* 固定定位: */
  44. position: fixed;
  45. top:5em;
  46. left: 50%;
  47. margin-left: -10em;
  48. }
  49. .modal form table {
  50. width: 80%;
  51. }
  52. .modal form table caption {
  53. font-weight: bold;
  54. margin-bottom:1em;
  55. }
  56. .modal form table td {
  57. padding:.5em
  58. }
  59. .modal form table td:first-of-type {
  60. width:5em;
  61. }
  62. /* 定位父级 */
  63. .modal-body{
  64. position: relative;
  65. }
  66. .modal .close {
  67. position: absolute;
  68. width:2em;
  69. height: 2em;
  70. top: 1em;
  71. right: 1em;
  72. }
  73. .modal .close:hover{
  74. cursor: pointer;
  75. background-color: #ff0000;
  76. color: #fff;
  77. }
  78. /* 模态框默认隐藏 */
  79. .modal{
  80. display: none;
  81. }

JS代码:

  1. const btn = document.querySelector('header button');
  2. const modal = document.querySelector('.modal');
  3. const close = document.querySelector('.close');
  4. btn.onclick=function(){
  5. modal.style.display ='block';
  6. };
  7. close.onclick=function(){
  8. modal.style.display ='none';
  9. };
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议