美文网首页
overflow:hidden的三个作用

overflow:hidden的三个作用

作者: 徐慵仙 | 来源:发表于2020-05-31 12:33 被阅读0次

一、清除浮动

直接上代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .xiong1{
            float: left;
            background-color: pink;
            height: 100px;
            width: 100px;
        }
        .xiong2{
            float: left;
            background-color: skyblue;
            height: 100px;
            width: 100px;
        }
        .xiongbaba{
            background-color: black;
        }
    </style>
</head>
<body>
    <div class="xiongbaba">
        <div class="xiong1">这是熊大</div>
        <div class="xiong2">这是熊二</div>
    </div>
    <div class="qiang">光头强</div>
</body>
</html>

效果:

image.png

看懂此处需要有浮动知识的基础。我们看到熊大和熊二,没有在熊爸爸里面,熊爸爸还是一条横线在上方,因为浮动脱离了标准流,此时我们只需要在熊爸爸里面加上这个overflow:hidden。

变身效果

image.png

注意!!! 清除浮动,最常用的方式是伪元素和双伪元素的方式。

二、隐藏溢出

这是最原始的意思了,就是溢出部分不显示。
代码改成这样婶儿的:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .xiong1{
            background-color: pink;
            height: 100px;
            width: 100px;
        }
        .xiong2{
            background-color: skyblue;
            height: 100px;
            width: 100px;
        }
        .xiongbaba{
            background-color: black;
            border: solid 1px red;
            
            height: 150px;
        }
    </style>
</head>
<body>
    <div class="xiongbaba">
        <div class="xiong1">这是熊大</div>
        <div class="xiong2">这是熊二</div>
    </div>
    <div class="qiang">光头强</div>
</body>
</html>

效果

妈呀,熊二跑出来了,不好看。


image.png

变身效果

image.png

加上以后,熊二就规规矩矩在它应该在的位置了。

三、避免塌陷

what? 啥是塌陷。

给第一个div一个上边距,就会整体塌陷。

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .xiong1 {
            background-color: pink;
            height: 100px;
            width: 100px;
            margin-top: 50px;
        }

        .xiong2 {
            background-color: skyblue;
            height: 100px;
            width: 100px;
        }

        .xiongbaba {
            background-color: black;
        }
    </style>
</head>

<body> 
    <div class="xiongbaba">
        <div class="xiong1">这是熊大</div>
        <div class="xiong2">这是熊二</div>
    </div>
    <div class="qiang">光头强</div>
</body>

</html>

效果

image.png

不好啦!熊爸爸也跟着熊孩子下来了,也有了一个50上边距。上大招(加overflow:hidden)。

变身效果

 .xiongbaba {
            background-color: black;
            border: solid 1px red;
            overflow: hidden;
        }
image.png

完美~-~

相关文章

  • 有关overflow:hidden的解释

    overflow:hidden的意思 超出的部分要裁剪 overflow:hidden的作用 清除浮动float ...

  • overflow:hidden的三个作用

    一、清除浮动 直接上代码 效果: 看懂此处需要有浮动知识的基础。我们看到熊大和熊二,没有在熊爸爸里面,熊爸爸还是一...

  • overflow:hidden意外作用

    web中插入背景图片 这两个css区别是有没有 至于为什么呢?我看到有的地方写的是插入图片时会撑开父元素几像素,我...

  • 关于overflow:hidden的作用

    前段时间去做了金山的宣讲会的笔试题,其中一道题是说overflow:hidden的作用。后来认真查了些资料才知道o...

  • overflow: hidden 属性的作用

    1.当父元素设置了高度时,子元素的内容超出父元素会被隐藏。 2.父元素没有设置高度,且子元素设置了浮动。如果我们给...

  • 小程序-webkit-box-orient丢失

    解决方法 1.hidden{ overflow: hidden; text-overflow: ell...

  • overflow:hidden

    作用:overflow:hidden可以隐藏超出部分,清除浮动,解决塌陷 参考:https://www.jians...

  • overflow 移动

    元素加上overflow: hidden;后位置移动 加了overflow: hidden;‘审批部门’这个元素上...

  • 文字溢出省略号

    添加overflow:hidden; text-overflow:ellipsis;

  • 34、overflow超出部分省略号

    overflow 溢出: overflow:visible/hidden/scroll/auto/inherit;...

网友评论

      本文标题:overflow:hidden的三个作用

      本文链接:https://www.haomeiwen.com/subject/kwulzhtx.html