CSS之网页元素的显示与隐藏(旧土豆网遮罩案例)

网页元素的显示与隐藏。

很多网页的侧边栏都会出现广告,我们点击关闭时,广告会消失不见,但若重新刷新网页页面,则广告会重新出现。网页的广告并非是真的被删除了,而是被暂时的隐藏起来了。

• display

• visibility

• overflow

一、display

display:none; 隐藏元素

display: block; 除了将元素转化为块内元素外,还有让元素显示出来的意思

display元素被隐藏后不再占有原来的元素的位置

复制代码
// 不重要的代码块
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
    <style>
        .peppa {
            display: none;
            width: 200px;
            height: 200px;
            background-color: pink;
        }

        .gorge {
            width: 200px;
            height: 200px;
            background-color: aqua;
        }
    </style>
<body>
    <div class="peppa">
        佩奇
    </div>
    <div class="gorge">
乔治
    </div>
</body>
</html>

二、visibility

visilibity: inherit(继承上一个父亲对象的可见性) | hidden(隐藏) | visible(可见)

注意⚠️:隐藏后仍然会占有原先的位置

三、overflow

对溢出的元素进行隐藏

overflow:visible

overflow:hidden

overflow:scroll(溢出的部分显示滚动条)

overflow:auto(在需要的时候添加滚动条)

四、旧土豆网网页制作

效果演示:

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .tudou {
        position: relative;
        height: 320px;
        width: 444px;
        background-color: pink;
        margin: 30px auto;
    }

    .tudou img {
        width: 100%;
        height: 100%;
    }

    .mask {
        /* 隐藏遮罩层 */
        display: none;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, .4) url(images/arr.png) no-repeat center;
    }

    /* 当鼠标经过盒子时,就让遮罩层显示出来 */
    .tudou:hover .mask{
        display: block;
    }
</style>

<body>
    <div class="tudou">
        <div class="mask"></div>
        <img src="images/tudou.jpg" alt="">
    </div>
</body>

</html>
相关推荐
神奇的程序员9 小时前
我的软件冲进苹果商店下载榜前 50 了
前端
阳光是sunny9 小时前
别再被 worktree 绕晕了!AI 编程时代你必须掌握的 Git 隔离神器
前端·人工智能·后端
万少10 小时前
万少的博客 - 技术分享与解决方案
前端·javascript·后端
尘世中一位迷途小书童13 小时前
用 Cesium 撸了一个森林火情监控大屏,弧线、粒子、发光效果都齐了
前端·javascript
IT_陈寒13 小时前
垃圾回收器选错了,我的Java服务内存炸了
前端·人工智能·后端
月光下的丝瓜14 小时前
Flutter 国内安装指南
前端·flutter
玄星啊14 小时前
AI 编程的第 30 天,我怀念古法 Coding 了
前端·ai编程
Jolyne_14 小时前
Angular基础速通
前端·angular.js
锋行天下15 小时前
半秒开!还有谁!!!
前端·vue.js·架构
代码搬运媛16 小时前
git 下中文文件名乱码问题解决
前端