缩小/折叠溢出文本

huangapple go评论70阅读模式
英文:

Shrink / collapse overflowing text

问题

请检查附加的图像。正如您所见,文本溢出到div之外。我只想折叠溢出的文本,并在最后显示“阅读更多”。当用户点击“阅读更多”时,文本应扩展。

我正在进行一个Next.js项目。

div的屏幕截图

我的要求是将文本折叠到指定的行,而不是从指定的行中断文本。

英文:

Please check the attached image. As you can see, text is overflowing outside the div. I just want to collapse the overflowing text and display "Read more" in the end. While user click "read more" text should be expanded.

I'm working on a next js project.

Screenshot of div

My requirement is to collapse the text to specified lines not to break the text from specified lines.

答案1

得分: 2

Create a Card component like this:

export default function Card() {
  const [expand, setExpand] = useState(false);
  
  return (
    <div>
      <div className={`${style.collapsible_text} ${expanded ? '' : style.collapsed}`}>
        {/* Card text goes here */}
      </div>
      <p onClick={() => setExpand(!expand)}>{expand? "read more..." : "read less..."}</p>
    </div>
  );
}

Then, apply the following CSS to the Card component:

.collapsible_text {
  max-height: 50px;
  overflow: hidden;
}

.collapsed {
  max-height: 50px;
}
英文:

Create a Card component like this:

export default function Card() {
  const [expand, setExpand] = useState(false);
  
  return (
    &lt;div&gt;
      &lt;div className={`${style.collapsible_text} ${expanded ? &#39;&#39; : style.collapsed}`}&gt;
        {/* Card text goes here */}
      &lt;/div&gt;
      &lt;p onClick={() =&gt; setExpand(!expand)}&gt;{expand? &quot;read more...&quot; : &quot;read less...&quot;&lt;/p&gt;
    &lt;/div&gt;
  );
}

Then, apply the following css to the Card component:

.collapsible_text {
  max-height: 50px;
  overflow: hidden;
}

.collapsed {
  max-height: 50px;
}

huangapple
  • 本文由 发表于 2023年5月14日 17:56:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定