- <title>相对定位</title>
- <style>
- * {
- margin: 0;
- padding: 0;
- }
- /*
- 1. 改变位置的参照物是 自己原来的位置
- 2. 不脱标,占位
- 3. 标签显示模式特点 不变
- */
- div {
- position: relative;
- top: 100px;
- left: 200px;
- }
- </style>
复制代码
- <title>绝对定位</title>
- <style>
- * {
- margin: 0;
- padding: 0;
- }
- img {
- width: 400px;
- }
- .news {
- position: relative;
-
- margin: 100px auto;
- width: 400px;
- height: 350px;
- background-color: #f8f8f8;
- }
- /*
- 1. 脱标,不占位
- 2. 参照物:先找最近的已经定位的祖先元素;如果所有祖先元素都没有定位,参照浏览器可视区改位置
- 3. 显示模式特点改变:宽高生效(具备了行内块的特点)
- */
- .news span {
- position: absolute;
- top: 0;
- right: 0;
- /* display: block; */
- width: 92px;
- height: 32px;
- background-color: rgba(0,0,0,0.6);
- text-align: center;
- line-height: 32px;
- color: #fff;
- }
- </style>
- </head>
- <body>
- <div class="news">
- <img src="./images/news.jpg" alt="">
- <span>展会活动</span>
- <h4>2222世界移动大会</h4>
- </div>
- </body>
复制代码
- <title>绝对定位-居中</title>
- <style>
- img {
- position: absolute;
- left: 50%;
- top: 50%;
- /* margin-left: -265px;
- margin-top: -127px; */
- /* 方便: 50% 就是自己宽高的一半 */
- transform: translate(-50%, -50%);
- }
- </style>
- </head>
- <body>
- <img src="./images/login.webp" alt="">
- </body>
复制代码
- <title>固定定位</title>
- <style>
- * {
- margin: 0;
- padding: 0;
- }
- /*
- 1. 脱标,不占位
- 2. 参照物:浏览器窗口
- 3. 显示模式特点 具备行内块特点
- */
- div {
- position: fixed;
- top: 0;
- right: 0;
- width: 500px;
- }
- </style>
复制代码
|