简体   繁体   English

使用省略号截断文本,使插入符号始终在末尾

[英]Truncate Text using ellipsis keeping the caret always at the end

I want to make this button text to truncate without hiding the caret. 我想使此按钮文本截断而不隐藏插入符号。

When text is long it hides the caret. 当文本较长时,它将隐藏插入符号。 I want to keep the caret at the end and truncate the text only. 我想将插入符号保留在最后,仅截断文本。

This is my code Sample Text 这是我的代码示例文本

 .btn-default { background-color: #F0F0F0; border-color: #cccccc; max-width: 16em; overflow: hidden; text-overflow: ellipsis; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <button id="L1.900Level" class="btn btn btn-default dropdown-toggle dropdown-toggle btn-xl-toggle" type="button" data-toggle="dropdown" aria-expanded="false" aria-haspopup="true"><span> short text </span><span class="caret"></span></button> <button id="L1.900Level" class="btn btn btn-default dropdown-toggle dropdown-toggle btn-xl-toggle" type="button" data-toggle="dropdown" aria-expanded="false" aria-haspopup="true"><span> loooooooooooooooooooooooong text </span><span class="caret"></span></button> 

@NOTE this solution is for bootstrap 4 where icon is not added by .caret but from :after selector nonetheless same logic may work on older versions as well. @注意此解决方案适用于引导程序4,该图标不是.caret添加的,而是从:after选择器添加的,尽管如此,相同的逻辑也可以在较旧的版本上使用。 For this to work you need an additional span tag. 为此,您需要一个额外的span标签。

HTML 的HTML

<button class="btn w-100 dropdown-toggle">
    <span>Alle Termine</span>
</button>

CSS 的CSS

.dropdown-toggle {
    span {
           white-space: nowrap;
           text-overflow: ellipsis;
           overflow: hidden;
           display: inline-block;
           max-width: calc(100% - 5px); // prevents icon from touching right border
           vertical-align: middle;
     }
}

Notice button should have a certain width. 注意按钮应具有一定的宽度。 In my case button gets 100% width of the parent container but you may have another width in your case. 在我的情况下,按钮的宽度为父容器的100%,但在您的情况下,您可能会有另一个宽度。

This worked well on my end. 这对我来说效果很好。 I hope it helps other people as well! 我希望它也对其他人有帮助!

You can do this by giving position absolute to the caret element and place end of your button 您可以通过在插入符号元素上赋予绝对位置并放置按钮的末端来做到这一点

.btn-default span.caret {
    position: absolute;
    right: 4px;
    top: 15px;
} 

And you should make the parent element position as relative 并且您应该将父元素位置设为相对

.btn-default {
    background-color: #F0F0F0;
    border-color: #cccccc;
    max-width: 16em;
    overflow: hidden;
    text-overflow: ellipsis;
    position: relative:
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM