定位 position 一

目录

1.1  默认定位(position:static)

1.2  相对定位(position:relative)

1.3  绝对定位(position:absolute)

1.3.1  子绝父相定位


定位positon,相较于浮动,能够更好地调节元素的位置,其拥有五个属性值

包括默认定位 static、相对定位  relative  、绝对定位  absolute、固定定位 fixed以及粘性定位  sticky

.box{
            position: relative;     /*相对于自身原有位置移动*/
            position: absolute;     /*相对于父对象移动*/
            position: fixed;        /*固定于视窗上,不会随着页面的上下移动而移动*/
            position: static;       /*默认值,没有任何影响*/
            position: sticky;       /*完全位于视窗中时,同相对定位,但当页面移动,
                                      直至对象边 缘触及视窗的边缘,就会固定于视窗上*/
            
            /*宽高、背景、字体等其他属性*/
        }

1.1  默认定位(position:static)

元素的默认定位方式,即标准流,一般不使用,但也可以用于某些场合下,取消元素的其他定位,例如:

        .father{
            width: 150px;
            height: 150px;
            background: skyblue;
        }

        .father .son{
            position: absolute;     /*绝对定位*/
            top: 50px;              /*从父对象的顶部向下移动50px的距离*/
            width: 50px;            
            height: 50px;
            background-color: pink;
        }

        .father:hover .son{
            position: static;       /*鼠标移到.father上时,取消.son先前设置的
                                      绝对定位,使其重新回到左上角默认位置*/
        }
    <div class="father">
        <div class="son"></div>
    </div>

1.2  相对定位(position:relative)

相对于 元素原先在标准流中的位置 移动,

元素本身未“脱标”,还占据着原来的空间位置,你可以想象成仅仅是把可见的部分移动了,而基础不动。

        .father{
            width: 250px;
            height: 250px;
            background: skyblue;
        }

        .father .son1{
            position: relative;     /*相对定位*/
            top: 75px;              /*从原有位置自上而下移动75px的距离*/
            left: 75px;             /*从原有位置自左向右移动75px的距离*/
            width: 50px;            
            height: 50px;
            background-color: pink;
        }

        .son2{
            width: 50px;            
            height: 50px;
            background-color: green;
        }
    <div class="father">
        <div class="son1"></div>
        <div class="son2"></div>
    </div>

1.3  绝对定位(position:absolute)

相对于 祖先元素 移动

元素“脱标”,不再占据任何空间

        .father{
            width: 250px;
            height: 250px;
            background: skyblue;
            position: relative;     /*相对定位*/
        }

        .father .son1{
            position: absolute;     /*绝对定位*/
            bottom: 50%;            /*从父对象的底部开始向上移动相当于父元素50%宽度的距离*/
            right: 50%;             /*从父对象的右侧开始向左移动相当于父元素50%宽度的距离*/
            width: 50px;            
            height: 50px;
            background-color: pink;
        }

        .son2{
            width: 50px;            
            height: 50px;
            background-color: green;
        }
    <div class="father">
        <div class="son1"></div>
        <div class="son2"></div>
    </div>

1.3.1  子绝父相定位

绝对定位使得子元素相对于祖先元素移动,那么究竟是那个祖先元素,是父代,还是更高的祖先代?因此,绝对定位需要一个“参考物”来明确祖先元素究竟是哪一代元素,这个参考物就是

相对定位position:relative

绝对定位将层级最近的带有相对定位祖先元素作为位移参考系

如果在其祖先元素里没有找到相对定位,那么子元素默认以视窗为参考系移动,如下所示


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
CSS中的position属性用于控制元素的定位方式。根据引用\[1\]和引用\[2\],position属性有五个值:static、relative、absolute、fixed和sticky。 - static:元素按照正常的文档流进行布局,不会受到position属性的影响。 - relative:元素相对于其在正常文档流中的位置进行定位。引用\[3\]中的示例展示了相对定位的效果,其中.content_1元素相对于其在正常文档流中的位置进行定位。 - absolute:元素相对于其最近的非static定位的父元素进行定位。如果没有非static定位的父元素,则相对于文档的根元素进行定位。引用\[2\]中的示例展示了绝对定位的效果,其中.content元素相对于.container元素进行定位。 - fixed:元素相对于浏览器窗口进行定位,即使页面滚动,元素的位置也不会改变。 - sticky:元素在滚动到特定位置时变为固定定位,直到滚动回到指定位置之前保持固定定位。 总结来说,position属性用于控制元素的定位方式,可以通过设置不同的值来实现不同的效果。 #### 引用[.reference_title] - *1* *2* *3* [CSS Position 定位](https://blog.csdn.net/Coxhuang/article/details/103319551)[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 ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

John_Crown

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值