缩小/折叠溢出文本

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

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:

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

Then, apply the following CSS to the Card component:

  1. .collapsible_text {
  2. max-height: 50px;
  3. overflow: hidden;
  4. }
  5. .collapsed {
  6. max-height: 50px;
  7. }
英文:

Create a Card component like this:

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

Then, apply the following css to the Card component:

  1. .collapsible_text {
  2. max-height: 50px;
  3. overflow: hidden;
  4. }
  5. .collapsed {
  6. max-height: 50px;
  7. }

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:

确定