HTML定位——绝对定位和相对定位、固定定位

一,HTML中为什么需要定位?

1、浮动可以让多个块级盒子一行没有缝隙排列显示,经常用于横向排列盒子

2、定位则是可以让盒子自由的在某个盒子内移动位置或者固定屏幕中某个位置,并且可以压住其他盒子

二,定位

1.定位的组成

定位=定位模式+边偏移

     定位模式定位模式用于指定一个元素在文档中的定位方式。

     边偏移:边偏移则决定了该元素的最终位置。

2,绝对定位

  • 绝对定位指的是通过规定HTML元素在水平和垂直方向上的位置来固定元素,基于绝对定位的元素不会占据空间。
  • 绝对定位的位置声明是相对于已定位的并且包含关系最近的祖先元素。如果当前需要被定为的元素没有已定位的祖先元素作为参考值,则相对于整个网页。

 代码展示:

<!DOCTYPE html> 
 <html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>定位和布局</title>
</head>
<style>
    .big {
        width: 900px;
        height: 600px;
        background-color: black;
        position: relative;
    }

    .box4 {
        width: 150px;
        height: 100px;
        background-color: red;
         position: absolute;
        top: 150px;
        left: 200px; 
    }
</style>
<body>
    <div class="big">
        <div class="box4"></div>
    
    </div>
</body>

</html>

效果图如下:

3、相对定位

  • 相对定位与绝对定位的区别在于它的参照点不是左上角的原点,而是该元素本身原先的起点位置。并且即使该元素偏移到了新的位置,也仍然从原始的起点处占据空间。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>定位和布局</title>
</head>
<style>
    .wrap {
        width: 900px;
        height: 600px;
        background-color: black;
        position: relative;
    }
    
    .box1 {
        width: 150px;
        height: 100px;
        background-color: aqua;
        position: relative;
        left: 100px;
        top: 10px;
    }
    
    .box2 {
        width: 150px;
        height: 100px;
        background-color: antiquewhite;
        /* position: relative; */
        left: 130px;
        bottom: 50px;
    }
    
    .box3 {
        width: 150px;
        height: 100px;
        background-color: olivedrab;
        /* position: relative; */
        left: 170px;
        bottom: 100px;
    }
    
    .box4 {
        width: 150px;
        height: 100px;
        background-color: lightcoral;
        position: absolute;
        top: 150px;
        left: 200px;
    }
 
    
    .box6 {
        width: 150px;
        height: 100px;
        background-color: rgb(27, 173, 83);
    }
</style>

<body>

    <div class="wrap">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        <div class="box4"></div>
        <div class="box5"></div>
        <div class="box6"></div>
    </div>
</body>

</html>

效果图如下:

3、固定定位 

position:fixed;

固定定位永远都会相对于浏览器窗口进行定位,固定定位会固定在浏览器的某个位置,不会随滚动条滚动。最常用的就是电脑里面是不是弹出的小广告,如果你不差掉它,当时滑动鼠标查看网页时,小广告一直会在那里,还有常用的就是网站或者APP的导航栏和底部的选择栏。

4,静态定位 (默认)

选择器 { position:static;}

5、粘性定位 

选择器 {position:sticky;}

三、定位的扩展

1、绝对定位盒子的居中算法
加了绝对定位i的盒子不能通过 margin:0 auto;水平居中,但是可以通过以下计算方法实现水平和垂直居中。
(1)left:50%; :让盒子的左侧移动到父级元素的水平中心位置。
(2)margin-left : -100px; : 让盒子向左移动自身宽度的一半。

2、定位的特殊性

绝对定位和固定定位也和浮动类似。
(1)行内元素添加绝对或者固定定位,可以直接设置高度和宽度。
(2)块级元素添加绝对或者固定定位,如果不给宽度或者高度,默认大小是内容的大小。

结语;今天的分享就到这了,有什么问题请私信,谢谢。

绝对定位是相对于元素最近的已定位的祖先元素进行定位的。如果元素没有已定位的祖先元素,那么它的位置则是相对于最初的包含块(body)进行定位。\[2\]相对定位是一种定位方式,通过设置元素的位置属性为relative,使元素相对于其正常位置进行偏移。相对定位的元素仍然占据原来的空间,不会影响其他元素的布局。\[1\]绝对定位相对定位是两种不同的定位方式,它们之间没有直接的依赖关系。绝对定位可以独立使用,也可以与相对定位结合使用,但相对定位不依赖于绝对定位。\[3\] #### 引用[.reference_title] - *1* [css中的定位以及绝对定位相对定位的区别](https://blog.csdn.net/wrx200077/article/details/108592065)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [绝对定位相对定位详解](https://blog.csdn.net/langyixuan/article/details/106124732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [HTML定位——绝对定位相对定位固定定位](https://blog.csdn.net/qq_52499703/article/details/125435762)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值