博主信息
博文 9
粉丝 0
评论 0
访问量 9432
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
实例演示flex容器中的四个属性的功能,参数,以及作用
唯蓝
原创
702人浏览过

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. }
显示图

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学