使用CSS省略号在悬停上显示截断的文本会覆盖其下方的文本

使用CSS省略号在悬停上显示截断的文本会覆盖其下方的文本

本文介绍了使用CSS省略号在悬停上显示截断的文本会覆盖其下方的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在侧边栏中有一个name标记,如果长文本后跟三点(lorem ipsum ...),则应该显示单行并截断,并且悬停时应该显示全文.

I have a name tag in the sidebar which should display single line and truncate if long text follow by triple dots (lorem ipsum...) and should show full text on hover.

我可以使用CSS来实现此目的,但是我的问题是当显示全文时,它与下面的文本重叠. (附有图片)

I am able to achieve this using css but my problem is when full text is displayed it overlaps the text below it. (Images attached)

HTML

<p class="name">
    Lorem ipsum lorem ipsum lorem ipsum
</p>

CSS

.name{
    color: #0079c1;
    height: 2em;
    line-height: 1em;
    font-size: 20px;
    font-weight: 400;
    text-overflow: ellipsis;
    margin-bottom: 12px;
    cursor: pointer;
    word-break: break-all;
    overflow:hidden;
    white-space: nowrap;
}

.name:hover{
    overflow: visible;
    white-space: normal;
}

这是> JSFiddle

悬停时文字重叠.预期的行为是它应该将内容推送到其下方.

Text overlapping on hover. Expected behaviour is it should push the content below it.

推荐答案

您只需将height:auto添加到hover状态,它就可以正常工作:

You can just add height:auto to the hover state and it'll work just fine:

JS小提琴

.name{
    width:120px;
    color: #0079c1;
    height: 2em;
    line-height: 1em;
    font-size: 20px;
    font-weight: 400;
    text-overflow: ellipsis;
    margin-bottom: 12px;
    cursor: pointer;
    word-break: break-all;
    overflow:hidden;
    white-space: nowrap;
}
.name:hover{
    overflow: visible;
    white-space: normal;
    height:auto;  /* just added this line */
}
<p class="name">
Lorem ipsum lorem ipsum lorem ipsum ipsum lorem ipsum
</p>
<span>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem voluptate deserunt consequatur velit, alias ullam fuga aspernatur, ut doloremque eos fugiat quo a accusamus minus distinctio quasi, recusandae excepturi molestiae.
</span>

这篇关于使用CSS省略号在悬停上显示截断的文本会覆盖其下方的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:07