Html学习笔记---position,relative,overflow

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .pg-header{
            position:fixed;
            top:0;
            right:0;
            left:0;
            background-color: red;
            color:blue;
            height:50px;
            line-height:50px;
        }
        .pg-body{
            height:5000px;
            background-color:#dddddd;
            margin-top:52px;
        }
    </style>
</head>
<body>
    <!--采用position:fixed使其一直固定在底部-->
    <div class="pg-header">
        这是头部
    </div>
    <!--position的relative和absolute应用,使带有absolute的div永远固定在relative的div。跟clear:both一样,要在同一个div下才生效-->
    <div class="pg-body">
        position
            <div style="position:relative;border:1px solid red;height:200px;width:500px;margin:0 auto">
                <div style="position:absolute;background-color:red;width:30px;height:30px;left:0;bottom:0"></div>
            </div>
            <div style="position:relative; border:1px solid red;height:200px;width:500px;margin:0 auto">
                <div style="position:absolute;right:0;bottom:0;background-color:red;width:30px;height:30px"></div>
            </div>
            <div style="position:relative;border:1px solid red;height:200px;width:500px;margin:0 auto">
                <div style="position:absolute;left:250px;bottom:0;background-color:red;width:30px;height:30px"></div>
            </div>
    </div>
    <!--采用position:fixed使其一直固定在底部-->
    <div οnclick="GoTop();" style="width:50px;height:50px;background-color:blue;color:white;position:fixed;bottom:20px;right:20px">返回顶部</div>
    <!-- 模态对话框-->
    <!-- z-index值大的div放在最前面,先设置模态框500-350px;以百分比先定位,再margin-left调整;overflow是显示图片的状态,有hidden和auto属性-->
    <div style="z-index:9;background-color:white;position:fixed;left:50%;top:50%;width:500px;height:350px;margin-left:-250px;margin-top:-250px;overflow:auto">
        <img src="image/bz.jpg"/>
    </div>
    <div style="z-index:8;position:fixed;top:0;right:0;bottom:0;left:0;background-color:green;opacity:0.6"></div>
    <!--js和返回顶部的div搭配,点击返回顶部,不知为啥在chrome和ie不能用,在360能用-->
    <script>
        function GoTop(){
            document.body.scrollTop = 0;
        }
    </script>
</body>
</html>






ps:

经常要将一个div1始终固定在另一个div2的一边,那么使用relative和absolute,且absolute一定要写在relative的里面

<div style="position:relative;width:180px;left:200px;border:1px solid red;">
    <img src="1.jpg" alt="图片" style="width:150px;height: 100px;" />
    <div style="position:absolute;left:180px;top:0">11111111111111111111111111</div>
</div>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值