• 文本溢出显示省略号,CSS未加载时a标签仍可用处理方法


    一、文本溢出打点

    (1)单行文本

    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;

    (2)多行文本

    1. overflow : hidden;
    2. text-overflow: ellipsis;
    3. display: -webkit-box;
    4. -webkit-line-clamp: 2;
    5. -webkit-box-orient: vertical;

    适用范围:
    因使用了WebKit的CSS扩展属性,该方法适用于WebKit浏览器及移动端;

    注:

    1. -webkit-line-clamp用来限制在一个块元素显示的文本的行数。 为了实现该效果,它需要组合其他的WebKit属性。常见结合属性:
    2. display: -webkit-box; 必须结合的属性 ,将对象作为弹性伸缩盒子模型显示 。
    3. -webkit-box-orient 必须结合的属性 ,设置或检索伸缩盒对象的子元素的排列方式 。

    跨浏览器兼容的方案

    比较靠谱简单的做法就是设置相对定位的容器高度,用包含省略号(…)的元素模拟实现;

    p {
        position:relative;
        line-height:1.4em;
        /* 3 times the line-height to show 3 lines */
        height:4.2em;
        overflow:hidden;
    }
    p::after {
        content:"...";
        font-weight:bold;
        position:absolute;
        bottom:0;
        right:0;
        padding:0 20px 1px 45px;
        background:url(http://newimg88.b0.upaiyun.com/newimg88/2014/09/ellipsis_bg.png) repeat-y;
    }

    这里注意几点:

    1. height高度真好是line-height的3倍;
    2. 结束的省略号用了半透明的png做了减淡的效果,或者设置背景颜色;
    3. IE6-7不显示content内容,所以要兼容IE6-7可以是在内容中加入一个标签,比如用<span class="line-clamp">...</span>去模拟;
    4. 要支持IE8,需要将::after替换成:after
    p{
      position: relative;
      line-height: 20px;
      max-height: 40px;
      overflow: hidden;
    } p::after{
      content:
    "...";
      position: absolute;
      bottom: 0;
      right: 0;
      padding-left: 40px;   background: -webkit-linear-gradient(left, transparent, #fff 55%);   background: -o-linear-gradient(right, transparent, #fff 55%);   background: -moz-linear-gradient(right, transparent, #fff 55%);   background: linear-gradient(to right, transparent, #fff 55%); }

    适用范围:
    该方法适用范围广,但文字未超出行的情况下也会出现省略号,可结合js优化该方法。

    注:

    1. 将height设置为line-height的整数倍,防止超出的文字露出。
    2. 给p::after添加渐变背景可避免文字只显示一半。
    3. 由于ie6-7不显示content内容,所以要添加标签兼容ie6-7(如:<span>…<span/>);兼容ie8需要将::after替换成:after。

    2.jQuery插件-jQuery.dotdotdot

    这个使用起来也很方便:

    $(document).ready(function() {
        $("#wrapper").dotdotdot({
            //    configuration goes here
        });
    });

    二、CSS未加载时a标签仍可用的处理方法

    (1)利用上面的文字溢出处理方式

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>面向对象的拖拽和继承</title>
        <style>
            a{
                background: url(timg.jpg) center no-repeat;
                background-size: 100px 200px;
                display: inline-block;
                height: 100px;
                 200px;
                overflow: hidden;            
                text-indent: 200px;
                white-space: nowrap;
            }
        </style>
    </head>
    <body>
        <a href="http://www.taobao.com">淘宝网</a>
    </body>
    </html> 

    (2)利用padding

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>面向对象的拖拽和继承</title>
        <style>
            a{
                background: url(timg.jpg) center no-repeat;
                background-size: 100px 200px;
                display: inline-block;
                height: 0px;
                 200px;
                padding-top: 100px;
                overflow: hidden;            
            }
        </style>
    </head>
    <body>
        <a href="http://www.taobao.com">淘宝网</a>
    </body>
    </html> 

    三、P元素包裹块级元素会被截断

  • 相关阅读:
    Zabbix客户端日志出现(Not all processes could be identified, 解决
    zabbix 本地编译安装
    redis 配置文件解释 以及集群部署
    Unix shell范例精解 课后题
    if __name__ == '__main__':用法
    爬取猫眼电影100榜单 代码
    CentOS编译安装软件过程中遇到zlib.h: No such file or directory
    Codeigniter添加Composer支持
    CodeIgniter composer.json安装第三类库操作
    使用composer命令加载vendor中的第三方类库 的方法
  • 原文地址:https://www.cnblogs.com/phoebeyue/p/9302225.html
Copyright © 2020-2023  润新知