【css】overflow

overflow
<body>
    <div class="container">
        <div class="content"></div>
    </div>
</body>
        .container{
            width:100px;height:100px;
            padding:5px;
            border:5px solid darkorchid;
        }
        .content{
            width:200px;height:200px;
            background-color:lightsalmon
        }

overflow实现两栏自适应布局上发挥了作用,但它的本来用途是 裁剪,且有四个属性值。

  • visible
    默认值。
    在这里插入图片描述
  • hidden
    裁剪。从border的内边缘开始裁剪。
    在这里插入图片描述
  • scroll
    滚动条一直存在。
  • auto
    容器尺寸足够,不需要滚动时不显示滚动条;
    容器尺寸不足,需要滚动条时才显示滚动条。
计算滚动条宽度

说到滚动条,滚动条时占据页面空间的,占多少空间呢?有两种计算方法。

  • 方法1:window.innerWidth-document.documentElement.clientWidth
    17px,即 滚动条宽度是17px
<!DOCTYPE html>
<html>
<head>
    <style>
        .box{
            width:200px;
            height:1000px;
            border:1px solid transparent;
        }
    </style>
</head>
<body>
    <div class="box"></div>
</body>
</html>

在这里插入图片描述

  • 方法2:借用一个具有overflow:scroll的元素
<body>
    <script>
        var box = document.createElement('div');
        box.style.cssText = 'width:100px;height:100px;overflow:scroll;'
        document.body.appendChild(box);
        var scrollWidth = box.offsetWidth-box.clientWidth;
        console.log(scrollWidth);
        document.body.removeChild(box);
        box = null;
    </script>
</body>

得到的结果也是17px

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值