ASCII码 ASCII码

CSS:模态框定位实战

发布于:2022-05-20 17:57:06  栏目:技术文档

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>模态框定位实战</title>
  8. </head>
  9. <body>
  10. <header>
  11. <h2 class="title">朱老师的blog</h2>
  12. <button onclick="document.querySelector('.modal').style.display='block'" >登录</button>
  13. </header>
  14. <!-- 模态框 -->
  15. <div class="modal">
  16. <!-- 1 半透明遮罩 -->
  17. <div class="modal-bg" onclick="this.parentNode.style.display='none'"></div>
  18. <!-- 2 弹层表单 -->
  19. <form action="" class="modal-form">
  20. <fieldset>
  21. <legend>用户登录</legend>
  22. <input type="email" name="email" placeholder="usename@emial.com">
  23. <input type="password" name="password" placeholder="***********">
  24. </fieldset>
  25. </form>
  26. </div>
  27. </body>
  28. <style>
  29. /* 初始化 */
  30. *{
  31. margin:0;
  32. padding:0;
  33. box-sizing: border-box;
  34. }
  35. /* 头部样式*/
  36. header{
  37. background-color: seagreen;
  38. display: flex;
  39. padding: 0.5em 1em;
  40. }
  41. header .title{
  42. font-weight: lighter;
  43. font-style: italic;
  44. color:white;
  45. text-shadow: 1px 1px 1px;
  46. letter-spacing: 1px;
  47. }
  48. header button{
  49. margin-left:auto;
  50. width:5em;
  51. border: none;
  52. border-radius: 8px;
  53. }
  54. header button:hover{
  55. cursor: pointer;
  56. background-color: coral;
  57. color: white;
  58. box-shadow: 0 0 5px white;
  59. transition: 0.3s;
  60. }
  61. /* 模态框 */
  62. .modal .modal-form fieldset legend{
  63. padding: 5px 1em;
  64. background-color: rebeccapurple;
  65. color: white;
  66. }
  67. /* 表单的固定定位:正中间 */
  68. .modal .modal-form{
  69. position: fixed;
  70. top:10em;
  71. left:20em;
  72. right: 20em;
  73. /* background-color: lightpink; */
  74. }
  75. /* 半透明的遮罩 */
  76. .modal .modal-bg{
  77. position: fixed;
  78. /* height: 100px; */
  79. background-color: rgb(0,0,0,0.5);
  80. top:0;
  81. left:0;
  82. right:0;
  83. bottom:0;
  84. }
  85. </style>
  86. </html>

相关推荐
阅读 +