input实现文字超出省略号功能

input实现文字省略号功能

普通元素实现文字超出宽度自动变成省略号很是简单,给元素加个宽度,而后再加这三句css便可:css

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

有没有想过给input="text"元素也加这个效果呢?如图:
图片描述浏览器

input实现文字超出省略号功能

<style>
    *{
        margin: 0;
        padding: 0;
    }
    .box{
        width: 200px;
        padding: 15px;
        border: 1px solid #f60;
        margin: 50px auto;
    }
    .form-control{
        height: 30px;
        line-height: 30px;
        padding: 5px 10px;
    }
    .text-ellipsis{
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
    }
    h2{
        width: 300px;
        margin: 10px auto;
    }
</style>

<h2 class="text-ellipsis">文字超出宽度自动变成省略号</h2>
<div class="box">
    <input type="text" class="form-control text-ellipsis">
</div>

其实,让input实现文字超出自动变省略号也很是简单,仍是那三段代码。经测试,目前(2018-11-23)为止,只有最新的Chrome、Firefox两个浏览器支持,移动端未测试!测试

相关文章
相关标签/搜索