前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >css 关于图片上显示文字

css 关于图片上显示文字

作者头像
陨石坠灭
发布2018-10-19 15:20:38
4.5K0
发布2018-10-19 15:20:38
举报
文章被收录于专栏:全栈之路全栈之路

图片上显示文字,现在有很多主流的方法,最常用的就是position设置为relative或者absolute。

本文带来了一种新的实现方式:采用height为0+overflow为visible的方式实现。

上图:

QQ20180705-104410@2x.png
QQ20180705-104410@2x.png

为什么采用这个方式呢?首先就是为了适配问题,因为各种屏幕大小不一致,如果采用relative的方式调试起来会很麻烦,当然我也用到了relative,其作用是为了让z-index生效。

其次是高度定死了,起作用是为了方便调整位置,还有就是图片可以自动缩放,非常强大。

还有就是做了一个遮罩,可以用于加载图片,同时在移动端显示效果是一致的。

最后要讲一点:#00000044这种透明度的方式在移动端无法正常显示,应该改为: rgba(0,0,0,0.4),这个可以自己调整

优化提示: 使用rem作为单位比px更好,可以适应不同屏幕的大小,位置和字体大小不会错乱和太小。

关门,放代码:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>super-box实验</title>
    <style>
        .super-box div,.super-box img,.super-box span{
            padding: 0px;
            margin: 0px;
        }
        .super-box{
            width: 30%;
            overflow: hidden;
            background: #696969;
            display: inline-block;
            text-align: left;;
        }

        .super-box .content{
            margin: 10px;
            text-align: center;
            vertical-align: middle;
            height: 160px;
            line-height: 160px;
            overflow: hidden;
        }

        .super-box .content img{
            max-width: 100%;
            max-height: 100%;
            vertical-align: middle;
        }

        .super-box .head,.super-box .foot{
            height: 0px;
            overflow: visible;
            width: 100%;
        }


        .super-box .banner{
            width: 100%;
            height: 30px;
            line-height: 30px;
            margin-top: -30px;
            background-color: #00000029;
            position: relative;
        }

        .super-box .foot{
            text-align: center;
            float: left;
        }

        .super-box .tag{
            padding: 0px;
            padding-left: 6px;
            padding-right: 6px;
            color: white;
            position: relative;
            font-size: 12px;
        }

        .super-box .title{
            color: white;
            width: 100%;
            display: block;
        }

        .super-box .mask{
            height: 0px;
        }

        .super-box .mask>div{
            width: 100%;
            height: 200px;
            line-height: 200px;
            margin-top: -200px;
            text-align: center;
            background: #00000085;
            position: relative;
            color: red;
            display: none;
        }

        .show{
            display: block !important;
        }

        
    </style>
</head>
<body style="text-align: center">
    <div class="super-box">
        <div class="head">
            <span class="tag">hello</span>
        </div>
        <div class="content">
            <img src="https://www.google.de/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" />
        </div>
        <div class="foot">
            <div class="banner">
                <span class="title">hello</span>
            </div>
        </div>
        <div class="mask">
            <div></div>
        </div>
    </div>

    <div class="super-box">
        <div class="head">
            <span class="tag">hello</span>
        </div>
        <div class="content">
            <img src="https://www.google.de/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" />
        </div>
        <div class="foot">
            <div class="banner">
                <span class="title">hello</span>
            </div>
        </div>
        <div class="mask">
            <div></div>
        </div>
    </div>

    <div class="super-box">
        <div class="head">
            <span class="tag">hello</span>
        </div>
        <div class="content">
            <img src="https://www.google.de/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
        </div>
        <div class="foot">
            <div class="banner">
                <span class="title">hello</span>
            </div>
        </div>
        <div class="mask" >
            <div class="show">
                hello word
            </div>
        </div>
    </div>
</body>
</html>

本代码是采用vscode编写的,然后运行使用的是vscode的插件:Live Server

QQ20180705-105617.png
QQ20180705-105617.png
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/09/02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档