元素显示与隐藏的几种方式

元素显示与隐藏的几种方式

1.display:none;(常用)
元素隐藏且在文档中部不占位置,不会影响其它结构
transition(过渡),没有过渡效果

<!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>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            margin: 100px;
            width: 200px;
            height: 200px;
            line-height: 200px;
            color: white;
            background-color: tomato;
        }

        .box:hover {
            display: none;

        }
    </style>
</head>

<body>
    <div class="box">鼠标移动到我这我就会消失</div>
</body>

</html>
  1. visibility: hidden;
    元素隐藏且在文档中部占位置,会影响其它结构,仍具有高度、宽度等属性值
    transition(过渡),没有过渡效果
<!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>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            margin: 100px;
            width: 200px;
            height: 200px;
            color: white;
            background-color: tomato;
        }

        .box:hover {
            /* 它的位置还在,鼠标移动的时候会出现抖动 */
            visibility: hidden;
        }
    </style>
</head>

<body>
    <div class="box">你只是看不见我其实我还在</div>
    <div class="son">我怎么在这里,原来是你还在噢,占着茅坑不拉屎啊你</div>
</body>

</html>
  1. overflow: hidden;
    元素里面的内容超出部分隐藏
<!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>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            margin: 100px;
            width: 200px;
            height: 200px;
            /* line-height: 200px; */
            /* color: black; */
            background-color: tomato;
        }

        .box:hover {
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div class="box">
        鼠标移动到我这里,超出部分隐藏
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Recusandae in modi ducimus, et numquam minus vero totam
        asperiores pariatur non sint! Amet enim, distinctio numquam explicabo nulla et eveniet dolore?
    </div>

</body>

</html>
  1. opacity: 0;
    元素的透明度为0,在文档中部占位置,仍具有高度、宽度等属性值
    transition(过渡),有过渡效果
<!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>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            margin: 100px;
            width: 200px;
            height: 200px;
            color: white;
            background-color: tomato;
        }

        .box:hover {
            opacity: 0;
        }
    </style>
</head>

<body>
    <div class="box">鼠标移动到我这里,我的透明度改为0你看不见我哈哈</div>
</body>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值