找回密码
 立即注册

css3 定位

[复制链接]
admin 发表于 2025-5-20 15:26:01 | 显示全部楼层 |阅读模式

1.jpg

2.jpg

1.jpg

  1. <title>相对定位</title>
  2.   <style>
  3.     * {
  4.       margin: 0;
  5.       padding: 0;
  6.     }

  7.     /*
  8.       1. 改变位置的参照物是 自己原来的位置
  9.       2. 不脱标,占位
  10.       3. 标签显示模式特点 不变
  11.     */

  12.   div {
  13.     position: relative;
  14.     top: 100px;
  15.     left: 200px;
  16.   }   
  17.   </style>
复制代码
1.jpg
  1. <title>绝对定位</title>
  2.   <style>
  3.     * {
  4.       margin: 0;
  5.       padding: 0;
  6.     }

  7.     img {
  8.       width: 400px;
  9.     }

  10.     .news {
  11.       position: relative;
  12.    
  13.       margin: 100px auto;
  14.       width: 400px;
  15.       height: 350px;
  16.       background-color: #f8f8f8;
  17.     }

  18.     /*
  19.       1. 脱标,不占位
  20.       2. 参照物:先找最近的已经定位的祖先元素;如果所有祖先元素都没有定位,参照浏览器可视区改位置
  21.       3. 显示模式特点改变:宽高生效(具备了行内块的特点)
  22.     */

  23.     .news span {
  24.       position: absolute;
  25.       top: 0;
  26.       right: 0;

  27.       /* display: block; */
  28.       width: 92px;
  29.       height: 32px;
  30.       background-color: rgba(0,0,0,0.6);
  31.       text-align: center;
  32.       line-height: 32px;
  33.       color: #fff;
  34.     }
  35.   </style>
  36. </head>
  37. <body>
  38.   <div class="news">
  39.     <img src="./images/news.jpg" alt="">
  40.     <span>展会活动</span>
  41.     <h4>2222世界移动大会</h4>
  42.   </div>
  43. </body>
复制代码
1.jpg

  1. <title>绝对定位-居中</title>
  2.   <style>
  3.     img {
  4.       position: absolute;
  5.       left: 50%;
  6.       top: 50%;

  7.       /* margin-left: -265px;
  8.       margin-top: -127px; */

  9.       /* 方便: 50% 就是自己宽高的一半 */
  10.       transform: translate(-50%, -50%);
  11.     }
  12.   </style>
  13. </head>
  14. <body>
  15.   <img src="./images/login.webp" alt="">
  16. </body>
复制代码
2.jpg
3.jpg

1.jpg

  1. <title>固定定位</title>
  2.   <style>
  3.     * {
  4.       margin: 0;
  5.       padding: 0;
  6.     }

  7.     /*
  8.       1. 脱标,不占位
  9.       2. 参照物:浏览器窗口
  10.       3. 显示模式特点 具备行内块特点
  11.     */

  12.     div {
  13.       position: fixed;
  14.       top: 0;
  15.       right: 0;

  16.       width: 500px;
  17.     }
  18.   </style>
复制代码


QQ|网站地图|Archiver|手机版|金黑网络 ( 粤ICP备2021124338号 )

网站建设,微信公众号小程序制作,商城系统开发,高端系统定制,app软件开发,智能物联网开发,直播带货系统等

Powered by Www.Jinhei.Cn

Copyright © 2013-2024 深圳市金黑网络技术有限公司 版权所有

快速回复 返回顶部 返回列表