Blogger Information
Blog 9
fans 0
comment 0
visits 7459
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示flex容器中的四个属性的功能,参数,以及作用
唯蓝
Original
519 people have browsed it

flex 模态框弹窗浮动垂直水平居中

  • position:fixed 定位
  • 元素的位置相对于浏览器窗口是固定位置。
  • 即使窗口是滚动的它也不会移动;

HTML 代码实例

  1. <div class="modal">
  2. <!-- 半透明的蒙板遮罩盖住模态框弹层的后面内容 -->
  3. <div class="modal-backdrop"></div>
  4. <!-- 主体 -->
  5. <div class="modal-body">
  6. <!-- 关闭 -->
  7. <button class="close">关闭</button>
  8. <form action="" method="post">
  9. <table>
  10. <caption>
  11. 用户登录
  12. </caption>
  13. <tr>
  14. <td><label for="email">邮箱:</label></td>
  15. <td><input type="email" name="email" id="email" /></td>
  16. </tr>
  17. <tr>
  18. <td><label for="password">密码:</label></td>
  19. <td><input type="password" name="password" id="password" /></td>
  20. </tr>
  21. <tr>
  22. <td></td>
  23. <td><button>登录</button></td>
  24. </tr>
  25. </table>
  26. </form>
  27. </div>
  28. </div>

CSS 实例

  1. /* 模态框 */
  2. /* 遮罩 */
  3. .modal .modal-backdrop {
  4. /* 蒙板必须将弹层的后面全部盖住,意味着与容器大小一样 */
  5. position: fixed;
  6. top: 0;
  7. left: 0;
  8. right: 0;
  9. bottom: 0;
  10. background-color: rgb(0, 0, 0, 0.5);
  11. }
  12. /* 模态框主体 */
  13. .modal .modal-body {
  14. border: 1px solid #000;
  15. padding: 1em;
  16. min-width: 20em;
  17. /* background-color: lightcyan; */
  18. background: linear-gradient(to right, lightcyan, #fff);
  19. /* 将一个块在别一个块中垂直水平居中 */
  20. position: fixed;
  21. left: 50%;
  22. top: 50%;
  23. -webkit-transform: translate(-50%, -50%);
  24. transform: translate(-50%, -50%);
  25. }

fixed 原理

背景为灰色实现遮罩

<div class="modal-backdrop"></div>

  1. /* 蒙板必须将弹层的后面全部盖住,意味着与容器大小一样 */
  2. position: fixed;
  3. top: 0;
  4. left: 0;
  5. right: 0;
  6. bottom: 0;
  7. background-color: rgb(0, 0, 0, 0.5);
主体显示部份

<div class="modal-body"> ... </div>

  1. * 模态框主体 */ .modal .modal-body {
  2. border: 1px solid #000;
  3. padding: 1em;
  4. min-width: 20em;
  5. /* 背景色为渐变色 */
  6. background: linear-gradient(to right, lightcyan, #fff);
  7. /* 自动垂直水平居中 */
  8. position: fixed;
  9. left: 50%;
  10. top: 50%;
  11. -webkit-transform: translate(-50%, -50%);
  12. transform: translate(-50%, -50%);
  13. }
显示图

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post