position属性值static、relative、absolute、fixed作用

代码实例:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8">
        <title>example</title>
        <style type="text/css">
            img{
                width: 40px;
                height: 40px;
                top: 5px;
                left: 150px;
                border: thin solid black;
            }
            
        </style>
    </head>
    <body>
        <p>
            there are lots of differnt kinds of fruit.there are over 500 varieties of bananas alone.by the time we add the countless types of apples,oranges,and other well-known fruit,we are faced with thousands of choices.
        </p>
        <p>
            there are lots of differnt kinds of fruit.there are over 500 varieties of bananas alone.by the time we add the countless types of apples,oranges,and other well-known fruit.
        </p>
        <img id="caomei" src="img/4d0df8064383e78d74e07014e7ccd0ee.jpg" />
        <p>
            there are lots of differnt kinds of fruit.there are over 500 varieties of bananas alone.
        </p>
        <p>
            there are lots of differnt kinds of fruit.
        </p>
        <button>static</button>
        <button>relative</button>
        <button>abosolute</button>
        <button>fixed</button>
        <script type="text/javascript">
            var buttons=document.getElementsByTagName("button");
            for(var i=0;i<buttons.length;i++){
                buttons[i].οnclick=function(e){
                    document.getElementById("caomei").style.position=e.target.innerHTML;
                }
            }
        </script>

    </body>

</html>

按下static时图片的位置:

按下relative按钮时图片的位置

按下fixed按钮后图片的位置:

按下absolute按钮时,图片先前位置是什么,之后位置不变。

结论:由这个例子,可以看出,static的位置在第三,四两个段落中间,在程序所写的位置。

reletive的位置是按照static所处的位置,根据css而改变位置。

fixed则是根据浏览器左上角位置,根据css而改变位置。

至于absolute,由于没有参照物,会和之前的位置保持不变。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值